For distributed transactions that span multiple hibernate sessionfacotry, simply combine the definition of Jtatransactionmanager with multiple Localsessionfactorybean as a transaction strategy. Each of your DAO gets its own sessionfactory references through the bean properties. If all of the underlying JDBC data sources are containers that support transactions, as long as the business object uses Jtatransactionmanager as a transaction policy, it can divide the transaction across multiple DAO and multiple session factories without any special processing required.
<beans> <bean id= "MyDataSource1" class= "Org.springframework.jndi.JndiObjectFactoryBean" > <property n Ame= "Jndiname" value= "java:comp/env/jdbc/myds1"/> </bean> <bean id= "MyDataSource2" Org.springframework.jndi.JndiObjectFactoryBean "> <property name=" jndiname "value=" Java:comp/env/jdbc/myds2 " /> </bean> <bean id= "MySessionFactory1" class= " Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <property name=" dataSource "ref=" MyDataSource1
"/> <property name=" mappingresources "> <list> <value>product.hbm.xml</value> </list> </property> <property name= "hibernateproperties" > <value> hibe Rnate.dialect=org.hibernate.dialect.mysqldialect hibernate.show_sql=true </value> </property>
; </bean> <bean id= "MySessionFactory2" class= "org.springframework.orm.hibernate3.LocalSessionFactorybean "> <property name=" dataSource "ref=" MyDataSource2 "/> <property" name= "Mappingresources" > <list> <value>inventory.hbm.xml</value> </list> </property> <
Property name= "Hibernateproperties" > <value> hibernate.dialect=org.hibernate.dialect.oracledialect </value> </property> </bean> <bean id= "Mytxmanager" class= "Org.springframework.transac Tion.jta.JtaTransactionManager "/> <bean id=" Myproductdao "class=" product. Productdaoimpl "> <property name=" sessionfactory "ref=" MySessionFactory1 "/> </bean> <bean id=" my Inventorydao "class=" product. Inventorydaoimpl "> <property name=" sessionfactory "ref=" MySessionFactory2 "/> </bean> <!--this Shows the Spring 1.x style of declarative transaction configuration--> <!--It is totally supported, 100% legal I n Spring 2.x, but also above foR the sleeker, Spring 2.0 style--> <bean id= "Myproductservice" class= Ceptor. Transactionproxyfactorybean "> <property name=" TransactionManager "ref=" Mytxmanager "/> <property-name=" Target "> <bean class=" product. Productserviceimpl "> <property name=" Productdao ref= "Myproductdao"/> <property name= "inventor Ydao "ref=" Myinventorydao "/> </bean> </property> <property name=" Transactionattributes "
; <props> <prop key= "increaseprice*" >PROPAGATION_REQUIRED</prop> <prop key= "Someotherbu" Sinessmethod ">PROPAGATION_REQUIRES_NEW</prop> <prop key=" * ">propagation_supports,readonly</
prop> </props> </property> </bean> </beans>
<bean id= "MyDataSource1" class= "Org.springframework.jndi.JndiObjectFactoryBean" > <property name= "Jndiname" "Value=" java:comp/env/jdbc/myds1 "/> </bean> <bean id=" MyDataSource2 "class=" Org.springframework.jn Di.
Jndiobjectfactorybean "> <property name=" jndiname "value=" Java:comp/env/jdbc/myds2 "/> </bean> <bean id= "MySessionFactory1" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" > <proper
Ty name= "DataSource" ref= "MyDataSource1"/> <property name= "mappingresources" > <list> <value>product.hbm.xml</value> </list> </property> <property name= "Hibern Ateproperties "> <value> hibernate.dialect=org.hibernate.dialect.mysqldialect Hiberna Te.show_sql=true </value> </property> </bean> <bean id= "MySessionFactory2" C Lass= "Org.springframewOrk.orm.hibernate3.LocalSessionFactoryBean "> <property name=" dataSource "ref=" MyDataSource2 "/> <PR Operty name= "Mappingresources" > <list> <value>inventory.hbm.xml</value> ;/list> </property> <property name= "hibernateproperties" > <value> hiber
Nate.dialect=org.hibernate.dialect.oracledialect </value> </property> </bean> <bean id= "Mytxmanager" class= "Org.springframework.transaction.jta.JtaTransactionManager"/> <bean id= "MYPR" Oductdao "class=" product. Productdaoimpl "> <property name=" sessionfactory "ref=" MySessionFactory1 "/> </bean> <bea n id= "Myinventorydao" "class=" product. Inventorydaoimpl "> <property name=" sessionfactory "ref=" MySessionFactory2 "/> </bean> < Bean id= "Myproductservice" class= "Product.service.myProductServiceImpl" >;p roperty name= "Productdao" ref= "Myproductdao"/> <property name= "Inventorydao" ref= "MyInventoryDao"/> & lt;/bean> <aop:config> <aop:pointcut id= "Managertx expression=" Execution (* product.service. *service.* (..)) " /> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "Managertx"/> </aop:config> <tx:advice id= "txAd
Vice "transaction-manager=" Mytxmanager > <tx:attributes> <tx:method name= "get*" read-only= "true"/> <tx:method name= "find*" read-only= "true"/> <tx:method name= "save*" propagation= "REQUIRED"/> <tx:me Thod name= "remove*" propagation= "REQUIRED"/> <tx:method name= "*"/> </tx:attributes> </tx:advice&
Gt