📄 cmttransaction.java
字号:
//$Id: CMTTransaction.java,v 1.3 2005/04/15 13:58:28 oneovthafew Exp $package org.hibernate.transaction;import javax.transaction.Status;import javax.transaction.Synchronization;import javax.transaction.SystemException;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.hibernate.HibernateException;import org.hibernate.Transaction;import org.hibernate.TransactionException;import org.hibernate.jdbc.JDBCContext;import org.hibernate.util.JTAHelper;/** * Implements a basic transaction strategy for CMT transactions. All work is done * in the context of the container managed transaction. * @author Gavin King */public class CMTTransaction implements Transaction { private static final Log log = LogFactory.getLog(CMTTransaction.class); private final JDBCContext jdbcContext; private final TransactionFactory.Context transactionContext; private boolean begun; private javax.transaction.Transaction transaction; public CMTTransaction(JDBCContext jdbcContext, TransactionFactory.Context transactionContext) { this.jdbcContext = jdbcContext; this.transactionContext = transactionContext; } public void begin() throws HibernateException { log.debug("begin"); boolean synchronization = jdbcContext.registerSynchronizationIfPossible(); if ( !synchronization ) { throw new TransactionException("Could not register synchronization for container transaction"); } begun = true; } public void commit() throws HibernateException { 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() } } public void rollback() throws HibernateException { if (!begun) { throw new TransactionException("Transaction not successfully started"); } log.debug("rollback"); try { transaction.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); } } public boolean wasRolledBack() throws TransactionException { if (!begun) return false; final int status; try { status = transaction.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); } } public boolean wasCommitted() throws TransactionException { if ( !begun ) return false; final int status; try { status = transaction.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 void registerSynchronization(Synchronization sync) throws HibernateException { if (transaction!=null) { try { transaction.registerSynchronization(sync); } catch (Exception e) { throw new TransactionException("Could not register synchronization", e); } } else { throw new IllegalStateException("JTA TransactionManager not available"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -