Hibernate and JdbcTemplate shared transaction management

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.