spring HibernateTransactionManager

spring事物配置:

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
//org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
        <property name="sessionFactory" ref="base_SessionFactory"/>
        <property name="entityInterceptor" >
            <bean id="daoInterceptor" class="com.test.DaoInterceptor"></bean>
        </property>
    </bean>

DaoInterceptor继承org.hibernate.EmptyInterceptor,可以在onSave、onFlushDirty、onLoad、afterTransactionCompletion自定义一些操作。

AnnotationSessionFactoryBean继承了父类AbstractSessionFactoryBean,父类实现了FactoryBean接口,因此HibernateTransactionManager注入的不是AnnotationSessionFactoryBean,而是SessionFactoryImpl

HibernateTransactionManager通过doGetTransaction、doBegin、doCommit等方法包装了hibernate的HibernateTransactionManager.HibernateTransactionObject和hibernate的session等类的方法。

  • doGetTransaction返回的是HibernateTransactionManager.HibernateTransactionObject实体作为txObject
  • doBegin为txObject配置Session并开启事务
//配置session
if (txObject.getSessionHolder() == null || txObject.getSessionHolder().isSynchronizedWithTransaction()) {
                    Interceptor entityInterceptor = this.getEntityInterceptor();
                    Session newSession = entityInterceptor != null ? this.getSessionFactory().openSession(entityInterceptor) : this.getSessionFactory().openSession();
                    if (this.logger.isDebugEnabled()) {
                        this.logger.debug("Opened new Session [" + SessionFactoryUtils.toString(newSession) + "] for Hibernate transaction");
                    }

                    txObject.setSession(newSession);
                }

                session = txObject.getSessionHolder().getSession();
//开启事务
 int timeout = this.determineTimeout(definition);
 Transaction hibTx;
 if (timeout != -1) {
     hibTx = session.getTransaction();
     hibTx.setTimeout(timeout);
     hibTx.begin();
 } else {
     hibTx = session.beginTransaction();
 }

 txObject.getSessionHolder().setTransaction(hibTx);
  • doRollback回滚事务
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值