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.DriverManagerDataSource">
  33.                 <property name="driverClassName"
  34.                         value="net.sourceforge.jtds.jdbc.Driver" />
  35.                 <property name="url"
  36.                         value="jdbc:jtds:sqlserver://localhost:1433/gjxt;SelectMethod=cursor;charset=GBK;tds=8.0;lastupdatecount=true" />
  37.                 <property name="username" value="sa" />
  38.                 <property name="password" value="sa" />
  39.         </bean>-->
  40.          
  41.          <!-- 通过proxool来配置数据源-->
  42.         <bean id="dataSource"
  43.                 class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  44.                 <property name="driverClassName"
  45.                         value="org.logicalcobwebs.proxool.ProxoolDriver" />
  46.                 <property name="url"
  47.                         value="proxool.qxgldb" />
  48.         </bean>          
  49.         <!--
  50.                 Alternative local DataSource that works in any environment, and offers much better performance.
  51.                 Uses Apache Commons DBCP for connection pooling. See Commons DBCP documentation
  52.                 for the required JAR files. See the PetStore sample application also shipped with
  53.                 Spring, for an example of Commons DBCP usage and the necessary build script.
  54.                 Alternatively you can use another connection pool such as C3P0, similarly configured
  55.                 using Spring.
  56.                 A standalone connection pool such as Commons DBCP is a good choice for use outside an
  57.                 application server environment, including web applications running in a web container without
  58.                 JTA, or integration testing using the org.springframework.test package.
  59.         -->
  60.         <!--
  61.                 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  62.                 <property name="driverClassName" value="${jdbc.driverClassName}"/>
  63.                 <property name="url" value="${jdbc.url}"/>
  64.                 <property name="username" value="${jdbc.username}"/>
  65.                 <property name="password" value="${jdbc.password}"/>
  66.                 </bean>
  67.         -->
  68.         <!-- JNDI DataSource for J2EE environments -->
  69.         <!--
  70.                 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  71.                 <property name="jndiName" value="java:comp/env/jdbc/petclinic"/>
  72.                 </bean>
  73.         -->
  74.         <!-- 本地jdbc连接抽取器和oracle lobhandler类 用于存取流程定义文件 -->
  75.         <bean id="nativeJdbcExtractor"
  76.                 class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"
  77.                 lazy-init="true" />
  78.         <bean id="oracleLobHandler"
  79.                 class="org.springframework.jdbc.support.lob.OracleLobHandler"
  80.                 lazy-init="true">
  81.                 <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor" />
  82.         </bean>
  83.         <bean id="defaltLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"
  84.                 lazy-init="true">
  85.         </bean>       
  86.         <!--  session factory for sql server, 指定了lobHandler -->
  87.         <!--          -->
  88.         <bean id="MyHibernateSessionFactory"
  89.                 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  90.                 <property name="dataSource">
  91.                         <ref bean="dataSource" />
  92.                 </property>
  93.                 <property name="lobHandler">
  94.                         <ref bean="defaltLobHandler" />
  95.                 </property>                 
  96.                 <property name="hibernateProperties">
  97.                         <props>
  98.                                 <prop key="hibernate.dialect">
  99.                                         org.hibernate.dialect.SQLServerDialect
  100.                                 </prop>
  101.                                 <prop key="hibernate.show_sql">true</prop>
  102.                         </props>
  103.                 </property>
  104.                 <property name="mappingJarLocations">
  105.                         <list>
  106.                                 <value>
  107.                                         WEB-INF/lib/fireflow*.jar
  108.                                 </value>
  109.                         </list>
  110.                 </property>
  111.                 <property name="mappingLocations">
  112.                         <list>
  113.                                 <value>
  114.                                         WEB-INF/classes/org/fireflow/example/**/*.hbm.xml
  115.                                 </value>
  116.                         </list>
  117.                 </property>
  118.         </bean>
  119.          
  120.         <bean id="transactionManager"
  121.                 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  122.                 <property name="sessionFactory">
  123.                         <ref bean="MyHibernateSessionFactory" />
  124.                 </property>
  125.         </bean>
  126.        
  127.         <!-- Transaction manager for a single JDBC DataSource (alternative to JTA)
  128.         <bean id="transactionManager"
  129.                 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  130.                 <property name="dataSource" ref="dataSource" />
  131.         </bean>-->
  132.         <!-- Transaction manager that delegates to JTA (for a transactional JNDI DataSource) -->
  133.         <!--  
  134.                 <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
  135.         -->
  136.         <bean id="baseTransactionProxy" abstract="true"
  137.                 class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  138.                 <property name="transactionManager">
  139.                         <ref bean="transactionManager" />
  140.                 </property>
  141.                 <property name="transactionAttributes">
  142.                         <props>
  143.                                 <prop key="create*">
  144.                                         PROPAGATION_REQUIRED,-Exception
  145.                                 </prop>
  146.                                 <prop key="update*">
  147.                                         PROPAGATION_REQUIRED,-Exception
  148.                                 </prop>
  149.                                 <prop key="delete*">
  150.                                         PROPAGATION_REQUIRED,-Exception
  151.                                 </prop>
  152.                                 <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
  153.                                 <prop key="retrieve*">
  154.                                         PROPAGATION_REQUIRED,readOnly
  155.                                 </prop>
  156.                                 <prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
  157.                         </props>
  158.                 </property>
  159.         </bean>
  160.         <!--  
  161.         通过BeanNameAutoProxyCreator来实现spring的事务控制-->
  162.         <bean
  163.                 class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
  164.                 <property name="transactionInterceptor"
  165.                         ref="transactionInterceptor" />
  166.         </bean>
  167.                
  168.         <bean id="transactionInterceptor"
  169.                 class="org.springframework.transaction.interceptor.TransactionInterceptor">
  170.                 <property name="transactionManager">
  171.                         <ref bean="transactionManager" />
  172.                 </property>
  173.                 <property name="transactionAttributes">
  174.                         <props>
  175.                                 <prop key="create*">
  176.                                         PROPAGATION_REQUIRED,-Exception
  177.                                 </prop>
  178.                                 <prop key="update*">
  179.                                         PROPAGATION_REQUIRED,-Exception
  180.                                 </prop>
  181.                                 <prop key="delete*">
  182.                                         PROPAGATION_REQUIRED,-Exception
  183.                                 </prop>
  184.                                 <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
  185.                                 <prop key="retrieve*">
  186.                                         PROPAGATION_REQUIRED,readOnly
  187.                                 </prop>
  188.                                 <prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
  189.                         </props>
  190.                 </property>
  191.         </bean>
  192.        
  193.         <bean
  194.                 class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  195.                 <property name="beanNames">
  196.                         <value>*ServiceDao</value>
  197.                 </property>
  198.                 <property name="interceptorNames">
  199.                         <list>
  200.                                 <value>transactionInterceptor</value>
  201.                         </list>
  202.                 </property>
  203.         </bean>
  204.         <!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
  205. </beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值