FileDocCategorySizeDatePackage
CMTTransaction.javaAPI DocHibernate 3.2.54478Wed Mar 22 16:47:32 GMT 2006org.hibernate.transaction

CMTTransaction

public class CMTTransaction extends Object implements org.hibernate.Transaction
Implements a basic transaction strategy for CMT transactions. All work is done in the context of the container managed transaction.
author
Gavin King

Fields Summary
private static final Log
log
protected final org.hibernate.jdbc.JDBCContext
jdbcContext
protected final TransactionFactory.Context
transactionContext
private boolean
begun
Constructors Summary
public CMTTransaction(org.hibernate.jdbc.JDBCContext jdbcContext, TransactionFactory.Context transactionContext)


	     
		this.jdbcContext = jdbcContext;
		this.transactionContext = transactionContext;
	
Methods Summary
public voidbegin()

		if (begun) {
			return;
		}

		log.debug("begin");
		
		boolean synchronization = jdbcContext.registerSynchronizationIfPossible();

		if ( !synchronization ) {
			throw new TransactionException("Could not register synchronization for container transaction");
		}

		begun = true;
		
		jdbcContext.afterTransactionBegin(this);
	
public voidcommit()

		if (!begun) {
			throw new TransactionException("Transaction not successfully started");
		}

		log.debug("commit");

		boolean flush = !transactionContext.isFlushModeNever() &&
		        !transactionContext.isFlushBeforeCompletionEnabled();

		if (flush) {
			transactionContext.managedFlush(); //if an exception occurs during flush, user must call rollback()
		}

		begun = false;

	
public javax.transaction.TransactiongetTransaction()

		return transactionContext.getFactory().getTransactionManager().getTransaction();
	
public booleanisActive()


		if (!begun) return false;

		final int status;
		try {
			status = getTransaction().getStatus();
		}
		catch (SystemException se) {
			log.error("Could not determine transaction status", se);
			throw new TransactionException("Could not determine transaction status: ", se);
		}
		if (status==Status.STATUS_UNKNOWN) {
			throw new TransactionException("Could not determine transaction status");
		}
		else {
			return status==Status.STATUS_ACTIVE;
		}
	
public voidregisterSynchronization(javax.transaction.Synchronization sync)

		try {
			getTransaction().registerSynchronization(sync);
		}
		catch (Exception e) {
			throw new TransactionException("Could not register synchronization", e);
		}
	
public voidrollback()

		if (!begun) {
			throw new TransactionException("Transaction not successfully started");
		}

		log.debug("rollback");

		try {
			getTransaction().setRollbackOnly();
		}
		catch (SystemException se) {
			log.error("Could not set transaction to rollback only", se);
			throw new TransactionException("Could not set transaction to rollback only", se);
		}

		begun = false;

	
public voidsetTimeout(int seconds)

		throw new UnsupportedOperationException("cannot set transaction timeout in CMT");
	
public booleanwasCommitted()


		if ( !begun ) return false;

		final int status;
		try {
			status = getTransaction().getStatus();
		}
		catch (SystemException se) {
			log.error("Could not determine transaction status", se);
			throw new TransactionException("Could not determine transaction status: ", se);
		}
		if (status==Status.STATUS_UNKNOWN) {
			throw new TransactionException("Could not determine transaction status");
		}
		else {
			return status==Status.STATUS_COMMITTED;
		}
	
public booleanwasRolledBack()


		if (!begun) return false;

		final int status;
		try {
			status = getTransaction().getStatus();
		}
		catch (SystemException se) {
			log.error("Could not determine transaction status", se);
			throw new TransactionException("Could not determine transaction status", se);
		}
		if (status==Status.STATUS_UNKNOWN) {
			throw new TransactionException("Could not determine transaction status");
		}
		else {
			return JTAHelper.isRollback(status);
		}