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.DriverManagerDataSource">
- <property name="driverClassName"
- value="net.sourceforge.jtds.jdbc.Driver" />
- <property name="url"
- value="jdbc:jtds:sqlserver://localhost:1433/gjxt;SelectMethod=cursor;charset=GBK;tds=8.0;lastupdatecount=true" />
- <property name="username" value="sa" />
- <property name="password" value="sa" />
- </bean>-->
- <!-- 通过proxool来配置数据源-->
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName"
- value="org.logicalcobwebs.proxool.ProxoolDriver" />
- <property name="url"
- value="proxool.qxgldb" />
- </bean>
- <!--
- Alternative local DataSource that works in any environment, and offers much better performance.
- Uses Apache Commons DBCP for connection pooling. See Commons DBCP documentation
- for the required JAR files. See the PetStore sample application also shipped with
- Spring, for an example of Commons DBCP usage and the necessary build script.
- Alternatively you can use another connection pool such as C3P0, similarly configured
- using Spring.
- A standalone connection pool such as Commons DBCP is a good choice for use outside an
- application server environment, including web applications running in a web container without
- JTA, or integration testing using the org.springframework.test package.
- -->
- <!--
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <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>
- -->
- <!-- JNDI DataSource for J2EE environments -->
- <!--
- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
- <property name="jndiName" value="java:comp/env/jdbc/petclinic"/>
- </bean>
- -->
- <!-- 本地jdbc连接抽取器和oracle lobhandler类 用于存取流程定义文件 -->
- <bean id="nativeJdbcExtractor"
- class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"
- lazy-init="true" />
- <bean id="oracleLobHandler"
- class="org.springframework.jdbc.support.lob.OracleLobHandler"
- lazy-init="true">
- <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor" />
- </bean>
- <bean id="defaltLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"
- lazy-init="true">
- </bean>
- <!-- session factory for sql server, 指定了lobHandler -->
- <!-- -->
- <bean id="MyHibernateSessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- <property name="lobHandler">
- <ref bean="defaltLobHandler" />
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.SQLServerDialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- </props>
- </property>
- <property name="mappingJarLocations">
- <list>
- <value>
- WEB-INF/lib/fireflow*.jar
- </value>
- </list>
- </property>
- <property name="mappingLocations">
- <list>
- <value>
- WEB-INF/classes/org/fireflow/example/**/*.hbm.xml
- </value>
- </list>
- </property>
- </bean>
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref bean="MyHibernateSessionFactory" />
- </property>
- </bean>
- <!-- Transaction manager for a single JDBC DataSource (alternative to JTA)
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>-->
- <!-- Transaction manager that delegates to JTA (for a transactional JNDI DataSource) -->
- <!--
- <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
- -->
- <bean id="baseTransactionProxy" abstract="true"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <property name="transactionManager">
- <ref bean="transactionManager" />
- </property>
- <property name="transactionAttributes">
- <props>
- <prop key="create*">
- PROPAGATION_REQUIRED,-Exception
- </prop>
- <prop key="update*">
- PROPAGATION_REQUIRED,-Exception
- </prop>
- <prop key="delete*">
- PROPAGATION_REQUIRED,-Exception
- </prop>
- <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="retrieve*">
- PROPAGATION_REQUIRED,readOnly
- </prop>
- <prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
- </props>
- </property>
- </bean>
- <!--
- 通过BeanNameAutoProxyCreator来实现spring的事务控制-->
- <bean
- class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
- <property name="transactionInterceptor"
- ref="transactionInterceptor" />
- </bean>
- <bean id="transactionInterceptor"
- class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <property name="transactionManager">
- <ref bean="transactionManager" />
- </property>
- <property name="transactionAttributes">
- <props>
- <prop key="create*">
- PROPAGATION_REQUIRED,-Exception
- </prop>
- <prop key="update*">
- PROPAGATION_REQUIRED,-Exception
- </prop>
- <prop key="delete*">
- PROPAGATION_REQUIRED,-Exception
- </prop>
- <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="retrieve*">
- PROPAGATION_REQUIRED,readOnly
- </prop>
- <prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
- </props>
- </property>
- </bean>
- <bean
- class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="beanNames">
- <value>*ServiceDao</value>
- </property>
- <property name="interceptorNames">
- <list>
- <value>transactionInterceptor</value>
- </list>
- </property>
- </bean>
- <!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
- </beans>