spring+hibernate+JTA 分布式事务的例子

对于横跨多个Hibernate SessionFacotry的分布式事务,只需简单地将 JtaTransactionManager 同多个 LocalSessionFactoryBean 的定义结合起来作为事务策略。你的每一个DAO通过bean属性得到各自的 SessionFactory 引用。如果所有的底层JDBC数据源都是支持事务的容器,那么只要业务对象使用了 JtaTransactionManager 作为事务策略,它就可以横跨多个DAO和多个session factories来划分事务,而不需要做任何特殊处理。

 1 <beans>
 2 
 3   <bean id="myDataSource1" class="org.springframework.jndi.JndiObjectFactoryBean">
 4     <property name="jndiName" value="java:comp/env/jdbc/myds1"/>
 5   </bean>
 6 
 7   <bean id="myDataSource2" class="org.springframework.jndi.JndiObjectFactoryBean">
 8     <property name="jndiName" value="java:comp/env/jdbc/myds2"/>
 9   </bean>
10 
11   <bean id="mySessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
12     <property name="dataSource" ref="myDataSource1"/>
13     <property name="mappingResources">
14       <list>
15         <value>product.hbm.xml</value>
16       </list>
17     </property>
18     <property name="hibernateProperties">
19       <value>
20         hibernate.dialect=org.hibernate.dialect.MySQLDialect
21         hibernate.show_sql=true
22       </value>
23     </property>
24   </bean>
25 
26   <bean id="mySessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
27     <property name="dataSource" ref="myDataSource2"/>
28     <property name="mappingResources">
29       <list>
30         <value>inventory.hbm.xml</value>
31       </list>
32     </property>
33     <property name="hibernateProperties">
34       <value>
35         hibernate.dialect=org.hibernate.dialect.OracleDialect
36       </value>
37     </property>
38   </bean>
39 
40   <bean id="myTxManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
41 
42   <bean id="myProductDao" class="product.ProductDaoImpl">
43     <property name="sessionFactory" ref="mySessionFactory1"/>
44   </bean>
45 
46   <bean id="myInventoryDao" class="product.InventoryDaoImpl">
47     <property name="sessionFactory" ref="mySessionFactory2"/>
48   </bean>
49 
50   <!-- this shows the Spring 1.x style of declarative transaction configuration -->
51   <!-- it is totally supported, 100% legal in Spring 2.x, but see also above for the sleeker, Spring 2.0 style -->
52   <bean id="myProductService"
53       class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
54     <property name="transactionManager" ref="myTxManager"/>
55     <property name="target">
56       <bean class="product.ProductServiceImpl">
57         <property name="productDao" ref="myProductDao"/>
58         <property name="inventoryDao" ref="myInventoryDao"/>
59       </bean>
60     </property>
61     <property name="transactionAttributes">
62       <props>
63         <prop key="increasePrice*">PROPAGATION_REQUIRED</prop>
64         <prop key="someOtherBusinessMethod">PROPAGATION_REQUIRES_NEW</prop>
65         <prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
66       </props>
67     </property>
68   </bean>
69 
70 </beans>

 

posted @ 2014-09-30 10:47  LeonGo  阅读(599)  评论(0编辑  收藏  举报