欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TransactionFactoryFactory TransactionFactory

程序员文章站 2022-04-22 07:58:54
...
三种事务的创建由对应的JDBCTransactionFactory,JTATransactionFactroy,CMTTransactionFactory完成
这三者区别在
connectionReleaseMode
JTATransactionFactroy和CMTTransactionFactory连接释放模式执行每条语句后释放连接,JDBCTransactionFactory事务提交后释放,因此jta事务连接的重用高
TransactionManager
JDBCTransactionFactory和JTATransactionFactroy不需要TransactionManager,CMTTransactionFactory必须
isTransactionInProgress
JDBCTransactionFactory确定执行JDBCTransaction begin方法,但没提交或回滚过视为正在进行
JTATransactionFactroy
通过缓存的JTATransaction.getUserTransaction状态确定
如果找不到,通过transactionManager状态确定
如果找不到,通过JNDI查找userTransaction状态确定
CMTTransactionFactory
通过transactionManager.getTransaction状态确定
factory通过org.hibernate.transaction.TransactionFactoryFactory.buildTransactionFactory确定,简单表述下
通过hibernate.transaction.factory_class确定,如果为空,返回JDBCTransactionFactory
否则实例化hibernate.transaction.factory_class指定类,并调用configure方法

package org.hibernate.transaction;

import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;
import org.hibernate.util.ReflectHelper;

/**
* @author Gavin King
*/
public final class TransactionFactoryFactory {

private static final Log log = LogFactory.getLog(TransactionFactoryFactory.class);

/**
* Obtain a TransactionFactory with the transaction handling strategy
* specified by the given Properties.
*
* @param transactionProps transaction properties
* @return TransactionFactory
* @throws HibernateException
*/
public static TransactionFactory buildTransactionFactory(Properties transactionProps) throws HibernateException {

String strategyClassName = transactionProps.getProperty(Environment.TRANSACTION_STRATEGY);
if (strategyClassName==null) {
log.info("Using default transaction strategy (direct JDBC transactions)");
return new JDBCTransactionFactory();
}
log.info("Transaction strategy: " + strategyClassName);
TransactionFactory factory;
try {
factory = (TransactionFactory) ReflectHelper.classForName(strategyClassName).newInstance();
}
catch (ClassNotFoundException e) {
log.error("TransactionFactory class not found", e);
throw new HibernateException("TransactionFactory class not found: " + strategyClassName);
}
catch (IllegalAccessException e) {
log.error("Failed to instantiate TransactionFactory", e);
throw new HibernateException("Failed to instantiate TransactionFactory: " + e);
}
catch (java.lang.InstantiationException e) {
log.error("Failed to instantiate TransactionFactory", e);
throw new HibernateException("Failed to instantiate TransactionFactory: " + e);
}
factory.configure(transactionProps);
return factory;
}

private TransactionFactoryFactory() {}
}