//此处的参数 this.transactionManager.getTransaction(transAtt) 即是调用各具体平台的 transactionManager 来获取她的事务属性,在获取事务属性的同时她会更具具体的事务属性 来决定是否开始和怎么开始一个事务;见类AbstractPlatformTransactionManager 结构。
public abstract class AbstractPlatformTransactionManager implements PlatformTransactionManager, Serializable {
。。。。。。。。。。。。。。。。。。。。。
public final TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
if (isExistingTransaction(transaction)) {
//下面即是 更具具体的配置事务属性 来决定事务
if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NEVER) {
throw new IllegalTransactionStateException(
"Transaction propagation 'never' but existing transaction found");
}
if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
if (debugEnabled) {
logger.debug("Suspending current transaction");
}
Object suspendedResources = suspend(transaction);
boolean newSynchronization = (this.transactionSynchronization == SYNCHRONIZATION_ALWAYS);
return newTransactionStatus(
null, false, newSynchronization, definition.isReadOnly(), debugEnabled, suspendedResources);
}
else if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW) {
if (debugEnabled) {
logger.debug("Creating new transaction, suspending current one");
}
Object suspendedResources = suspend(transaction);
//此处的doBegin 方法给更具具体的平台和配置事务属性来启动一个事务
doBegin(transaction, definition);
boolean newSynchronization = (this.transactionSynchronization != SYNCHRONIZATION_NEVER);
return newTransactionStatus(
transaction, true, newSynchronization, definition.isReadOnly(), debugEnabled, suspendedResources);
}
。。。。。。。。。。。。。。。。。。。。。
}
// We always bind the TransactionInfo to the thread, even if
// we didn't create a new transaction here.
// This guarantees that the TransactionInfo stack will be
// managed correctly even if no transaction was created by
// this aspect.
txInfo.bindToThread();
return txInfo;
}
//下面是事务的提交操作,回滚类似
protected void doCommitTransactionAfterReturning(TransactionInfo txInfo) {
if (txInfo != null && txInfo.hasTransaction()) {
if (logger.isDebugEnabled()) {
logger.debug("Invoking commit for transaction on " + txInfo.joinpointIdentification());
}
//这里的transactionManager 就是Spring配置文件里面配置的事务 如:org.springframework.orm.hibernate3.HibernateTransactionManager 。
this.transactionManager.commit(txInfo.getTransactionStatus());
}
}
//
protected void doFinally(TransactionInfo txInfo) {
if (txInfo != null) {
txInfo.restoreThreadLocalStatus();
}
}
private void restoreThreadLocalStatus() {
// Use stack to restore old transaction TransactionInfo.
// Will be null if none was set.
currentTransactionInfo.set(oldTransactionInfo);
}
//下面以 HibernateTransactionManager 例,说说事务的开始和提交/回滚
//此dobegin()方法即是开始一个事务
protected void doBegin(Object transaction, TransactionDefinition definition) {
HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;
if (txObject.getSessionHolder() == null) {
Session session = SessionFactoryUtils.getSession(
getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator(), false);
if (logger.isDebugEnabled()) {
logger.debug("Opened new session [" + session + "] for Hibernate transaction");
}
txObject.setSessionHolder(new SessionHolder(session), true);
}
txObject.getSessionHolder().setSynchronizedWithTransaction(true);
Session session = txObject.getSessionHolder().getSession();
try {
Connection con = session.connection();
Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
txObject.setPreviousIsolationLevel(previousIsolationLevel);
if (definition.isReadOnly() && txObject.isNewSessionHolder()) {
// just set to NEVER in case of a new Session for this transaction
session.setFlushMode(FlushMode.NEVER);
}
if (!definition.isReadOnly() && !txObject.isNewSessionHolder()) {
// we need AUTO or COMMIT for a non-read-only transaction
FlushMode flushMode = session.getFlushMode();
if (FlushMode.NEVER.equals(flushMode)) {
session.setFlushMode(FlushMode.AUTO);
txObject.getSessionHolder().setPreviousFlushMode(flushMode);
}
}
// add the Hibernate transaction to the session holder
//此处即是真正的调用了hibernate的session开始一个事务session.beginTransaction() 。
txObject.getSessionHolder().setTransaction(session.beginTransaction());
// register transaction timeout
if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
txObject.getSessionHolder().setTimeoutInSeconds(definition.getTimeout());
}
// register the Hibernate Session's JDBC Connection for the DataSource, if set
if (getDataSource() != null) {
ConnectionHolder conHolder = new ConnectionHolder(con);
if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
conHolder.setTimeoutInSeconds(definition.getTimeout());
}
if (logger.isDebugEnabled()) {
logger.debug("Exposing Hibernate transaction as JDBC transaction [" +
conHolder.getConnection() + "]");
}
TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);
txObject.setConnectionHolder(conHolder);
}
// bind the session holder to the thread
if (txObject.isNewSessionHolder()) {
TransactionSynchronizationManager.bindResource(getSessionFactory(), txObject.getSessionHolder());
}
}
catch (Exception ex) {
SessionFactoryUtils.closeSessionIfNecessary(session, getSessionFactory());
throw new CannotCreateTransactionException("Could not create Hibernate transaction", ex);
}
}
//回滚
protected void doCommit(DefaultTransactionStatus status) {
HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction();
。。。。。。。。。。。。。。。。。。。。。。。。。。
try {
txObject.getSessionHolder().getTransaction().commit();
}
catch (net.sf.hibernate.TransactionException ex) {
// assumably from commit call to the underlying JDBC connection
throw new TransactionSystemException("Could not commit Hibernate transaction", ex);
}
catch (JDBCException ex) {
// assumably failed to flush changes to database
throw convertJdbcAccessException(ex.getSQLException());
}
catch (HibernateException ex) {
// assumably failed to flush changes to database
throw convertHibernateAccessException(ex);
}
}
}