日期:2014-05-16  浏览次数:20479 次

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下面的配置还是很方便的。

详细实例如下:(这是我自己机器上的例子,已经测试通过)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

  3. <!--
  4. ? ? ? ? - Application context definition for PetClinic on JDBC.
  5. -->
  6. <beans>

  7. ? ? ? ? <!-- ========================= RESOURCE DEFINITIONS ========================= -->

  8. ? ? ? ? <!-- Configurer that replaces ${...} placeholders with values from a properties file -->
  9. ? ? ? ? <!-- (in this case, JDBC-related settings for the dataSource definition below) -->
  10. ? ? ? ? <!--??
  11. ? ? ? ? ? ? ? ? <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  12. ? ? ? ? ? ? ? ? <property name="location" value="/WEB-INF/jdbc.properties"/>
  13. ? ? ? ? ? ? ? ? </bean>
  14. ? ? ? ? -->
  15. ? ? ? ? <!--
  16. ? ? ? ? ? ? ? ? Simple local DataSource that works in any environment.
  17. ? ? ? ? ? ? ? ? This uses the JDBC DriverManager to obtain connections, and does NOT perform connection
  18. ? ? ? ? ? ? ? ? pooling. Connection pooling is essential to all real-world applications.
  19. ? ? ? ? ? ? ? ? This definition is good for getting started, as it introduces no dependencies beyond
  20. ? ? ? ? ? ? ? ? the JDK, but DriverManagerDataSource is not intended for production usage.
  21. ? ? ? ? -->
  22. ? ? ? ? <!--
  23. ? ? ? ? ? ? ? ? <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  24. ? ? ? ? ? ? ? ? <property name="driverClassName" value="${jdbc.driverClassName}"/>
  25. ? ? ? ? ? ? ? ? <property name="url" value="${jdbc.url}"/>
  26. ? ? ? ? ? ? ? ? <property name="username" value="${jdbc.username}"/>
  27. ? ? ? ? ? ? ? ? <property name="password" value="${jdbc.password}"/>
  28. ? ? ? ? ? ? ? ? </bean>
  29. ? ? ? ? -->
  30. ? ? ? ? <!--??在spring中直接配置jdbc链接??测试的时候可以使用!
  31. ? ? ? ? <bean id="dataSource"
  32. ? ? ? ? ? ? ? ? class="org.springframework.jdbc.datasource.Driv