In the spring and Hibernate configuration files, we can control the transaction of the methods in the class, that is, a method contains multiple database writes, we can create a Hibernatetransactionmanager instance in spring, The corresponding sessionfactory is injected into its sessionfactory attribute, and the transaction is controlled by the way it is declared. Examples are as follows:
<bean id= "Oatm" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "sessionfactory" ><ref bean= "Oasessionfactory"/></property>
</bean>
However, this approach has a limitation, if the method is called Hibernate to store, but also through the JdbcTemplate to the database write operations, if during the execution of the method, the write data occurs when the exception, Only data that is stored through hibernate that is included in transaction management is rolled back, and data that is manipulated by JdbcTemplate is not rolled back.
The reason is that hibernate and JdbcTemplate use different dbconnection, and JdbcTemplate does not declare the corresponding transaction management, so in order to use Hibernate, JdbcTemplate also uses the method of database storage operations for transaction management, need to make the appropriate changes in the spring configuration file, the sample is as follows:
<bean id= "Oatm" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "sessionfactory" ><ref bean= "Oasessionfactory"/></property>
<property name= "DataSource" ><ref bean= "Oadatasource"/></property>
</bean>
JdbcTemplate and hibernate can be wrap in the same transaction. The establishment requires several conditions:
1, the use of the same datasource;
2, the affairs are entrusted to Hibernatetransactionmanager management;
3, the relevant DAO and service need to use the runtime exception system, using spring provides exception can, their own package design runtime exception system also line.
Reprint please indicate the source Http://blog.csdn.net/shimiso
Technology Exchange Group: 361579846