hibernate和jdbc事务统一控制
hibernate和jdbc事务统一控制
“Hibernate与JDBC(iBATIS)??都使用 DataSourceTransactionManager 同样可以保证事务
原理就是保证了 connection 的唯一性。
jdbc我是调spring的jdbcTemplate来操作,
经过测试。在同一个数据源的情况下直接使用Hibernate的TxManager可以同步事务,问题解决。
”
Rod Johnson的话:
引用
It is possible--and sometimes useful--to have coordinated transactions for both. Your JDBC transactions will be managed by the HibernateTransactionManager if you work with the same JDBC DataSource in the same transaction. That is, create the SessionFactory using Spring's SessionFactoryBean using the same DataSource that your JdbcTemplates use.
The only issue to watch, of course, is that you may be invalidating your Hibernate cache by JDBC changes. Generally I find it best to use JDBC to update only tables that don't have Hibernate mappings.
Juergen Hoeller的话:
引用
As Rod said, simply keep using HibernateTransactionManager, which auto-detects the DataSource used by Hibernate and seamlessly exposes Hibernate transactions as JDBC transactions for that DataSource. JDBC code that accesses the same DataSource via Spring will automatically participate in such transactions.
Note that you must specify the DataSource for Hibernate via LocalSessionFactoryBean's "dataSource" property to allow HibernateTransactionManager to auto-detect it. Alternatively, you can explicitly pass the DataSource to HibernateTransactionManager's "dataSource" property.
由此可见要保证事务的一致性,在spring下面的配置还是很方便的。
详细实例如下:(这是我自己机器上的例子,已经测试通过)
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
- <!--
- ? ? ? ? - Application context definition for PetClinic on JDBC.
- -->
- <beans>
- ? ? ? ? <!-- ========================= RESOURCE DEFINITIONS ========================= -->
- ? ? ? ? <!-- Configurer that replaces ${...} placeholders with values from a properties file -->
- ? ? ? ? <!-- (in this case, JDBC-related settings for the dataSource definition below) -->
- ? ? ? ? <!--??
- ? ? ? ? ? ? ? ? <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- ? ? ? ? ? ? ? ? <property name="location" value="/WEB-INF/jdbc.properties"/>
- ? ? ? ? ? ? ? ? </bean>
- ? ? ? ? -->
- ? ? ? ? <!--
- ? ? ? ? ? ? ? ? Simple local DataSource that works in any environment.
- ? ? ? ? ? ? ? ? This uses the JDBC DriverManager to obtain connections, and does NOT perform connection
- ? ? ? ? ? ? ? ? pooling. Connection pooling is essential to all real-world applications.
- ? ? ? ? ? ? ? ? This definition is good for getting started, as it introduces no dependencies beyond
- ? ? ? ? ? ? ? ? the JDK, but DriverManagerDataSource is not intended for production usage.
- ? ? ? ? -->
- ? ? ? ? <!--
- ? ? ? ? ? ? ? ? <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- ? ? ? ? ? ? ? ? <property name="driverClassName" value="${jdbc.driverClassName}"/>
- ? ? ? ? ? ? ? ? <property name="url" value="${jdbc.url}"/>
- ? ? ? ? ? ? ? ? <property name="username" value="${jdbc.username}"/>
- ? ? ? ? ? ? ? ? <property name="password" value="${jdbc.password}"/>
- ? ? ? ? ? ? ? ? </bean>
- ? ? ? ? -->
- ? ? ? ? <!--??在spring中直接配置jdbc链接??测试的时候可以使用!
- ? ? ? ? <bean id="dataSource"
- ? ? ? ? ? ? ? ? class="org.springframework.jdbc.datasource.Driv