在没有加入Spring来管理Hibernate事务之前,Hibernate对事务的管理的顺序是:
- 开始事务
- 提交事务
- 关闭事务
这样做的原因是Hibernate对事务默认是手动提交,如果不想手动提交,可在hibernate.cfg.xml中设置自动提交:
<propertyname="hibernate.connection.autocommit">true</property>
当我们Spring对我们的Hibernate进行整合之后,我们的代码又出现了什么变化呢?整合,之后,我们不再是每次都去拿Session进行数据操作了,也不需要每次都开启事务,提交事务了,我们只需要Spring给我们提供的一个HibernateTemplate,我们直接用这个类里面给我们提供的数据操作方法就可以操作数据了。我们在也看不到关于事务的代码了,那Spring究竟有没有在他的操作方法里面封装事务处理呢?有的人直接 HibernateTemplate里面提供的方法操作数据,成功了,有的人却又失败了,这到底是怎么回事呢?
其实这里要看我们是怎样集成我们的 Hibernate和Spring,如果在集成的过程中,我们抛弃了hibernate.cfg.xml文件,直接在Spring的的配置文件中进行配置数据源的话,那你直接用HibernateTemplate里面提供的方法是可以成功操作数据的,如果你还是用hibernate.cfg.xml来配置数据源,在Spring的配置文件中引用hibernate.cfg.xml文件,那么你不能成功,这其中的原因就是因为如果你用 hibernate.cfg.xml文件配置数据源,就像我们前面说的,Hibernate默认是手动提交事务,而HibernateTemplatel 提供的方法里面并没有提供事务提交,而如果你用Spring的配置文件来配置数据源,Sping默认是自动提交的,所以就会成功,如果你想把Spring 设置为手动提交你可以在起配置文件中进行配置:
<!-- 配置SessionFactory 根据实体类JPA注解自动生成数据库表结构 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="packagesToScan"> <value>com.blog.*.module,com.blog.*.*.module</value> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.default_schema">blog</prop> <prop key="hibernate.generate_statistics">true</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory </prop> <!-- Hibernate3 --> <!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> --> <!-- <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext </prop> --> </props> </property> </bean>
<!-- 配置事务管理器 指定其作用的sessionFactory把事务交给Spring去处理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean>
<!-- 配置事务特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice>
<!-- 配置那些类的方法进行事务管理 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution (* com.blog.*.services.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" /> </aop:config>
我们在配置事务的时候,我们一般是把事务边界设置到service层,也就是你的业务逻辑层,因为我们很多时候都是在我们的业务逻辑层来完成我们一些列的数据操作,如果放到Dao数据层,其粒度太小了。另外,如果我们把事务配置在业务逻辑层的话,对我们的二级缓存也是有好处的,这个大家以后实际操作的时候会发现。