For a variety of reasons, sometimes you may want to connect to other people's database, or different databases can not be automatically converted, refactoring the amount of data is too large, we have to connect multiple data sources in a project. Looking for all kinds of information from the Internet, only the solution given by the great God succeeded in a sudden. Http://www.cnblogs.com/hoojo/p/dynamic_switch_sessionfactory_ Mulitesessionfactory.html, but as the blog finally points out, the operation of different databases in a method, there will be a transaction problem, this time need to manually do transaction management. Although I have manually operated the database, there will be a session shutdown every time the server restarts or when the user first accesses it. The following error will be reported: No value for key [[e-mail protected]] bound to thread[xxx], this error is the meaning of the session unexpectedly closed. I did not find the right solution in the first place and had to run the program again as it was, which is obviously a palliative.
Later, I changed a kind of question, put forward "session unexpectedly closed what to do?" , and found some information about transaction management, and learned that JTA technology could solve this problem. JTA, the Java Transaction API, is used to resolve application distributed transaction processing. It solves the problem of accessing and updating data on two or more network computer resources, which in fact is a bit overqualified.
Here is my configuration file. This is the use of Atomikos implementation of distributed transaction management, to introduce the relevant jar package.
1. Multi-data source connection pool configuration
1<!--data source 1:mysql database--2<bean id= "Datasourcemysql"class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close" >3<property name= "Jdbcurl" value= "${JDBCURL1}" ></property>4<property name= "Driverclass" value= "${driverclass1}" ></property>5<property name= "user" value= "${user1}" ></property>6<property name= "Password" value= "${password1}" ></property>7<property name= "initialpoolsize" value= "${initialpoolsize1}" ></property>8<property name= "Minpoolsize" value= "3" ></property>9<property name= "maxpoolsize" value= "${maxpoolsize1}" ></property>Ten<property name= "Acquireincrement" value= "3" ></property> One<property name= "MaxIdleTime" value= "1800" ></property> A</bean> -<!--data source 2:sqlserver database-- -<bean id= "Datasourcesqlserver"class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close" > the<property name= "Jdbcurl" value= "${jdbcurl2}" ></property> -<property name= "Driverclass" value= "${driverclass2}" ></property> -<property name= "user" value= "${user2}" ></property> -<property name= "Password" value= "${password2}" ></property> +<property name= "initialpoolsize" value= "${initialpoolsize2}" ></property> -<property name= "Minpoolsize" value= "3" ></property> +<property name= "maxpoolsize" value= "${maxpoolsize2}" ></property> A<property name= "Acquireincrement" value= "3" ></property> at<property name= "MaxIdleTime" value= "1800" ></property> -</bean>
2. Configure two Sessionfactory objects, all of which are objects of the Org.springframework.orm.hibernate3.LocalSessionFactoryBean class.
1<bean id= "Mysqlsessionfactory"class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" lazy-init= "false" >2<property name= "DataSource" ref= "Datasourcemysql"/>3<property name= "Hibernateproperties" >4<props>5<prop key= "Hibernate.dialect" >org.hibernate.dialect.MySQL5Dialect</prop>6<prop key= "Hibernate.connection.release_mode" >after_transaction</prop>7<prop key= "Hibernate.show_sql" >true</prop>8<prop key= "Hibernate.format_sql" >true</prop>9<prop key= "Javax.persistence.validation.mode" >none</prop>Ten</props> One</property> A<property name= "Mappinglocations" > -<list> -<value>classpath:edu/cau/warning/entity/*.hbm.xml</value> the <value>classpath:edu/cau/base/entity/*.hbm.xml</value>6 - </list> - </property> + </bean> + <bean id= "sqlserversessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" lazy-init= "Default" > A <property name= "DataSource" ref= "Datasourcesqlserver"/> at <property name= "Hibernateproperties" > - <props> - <prop key= "Hibernate.dialect" >org.hibernate.dialect.SQLServer2008Dialect</prop> - <prop key= "Hibernate.show_sql" >true</prop> - <prop key= "Hibernate.format_sql" >true</prop> - <prop key= "Javax.persistence.validation.mode" >none</prop> in </props> - </property> to <property name= "Mappinglocations" > + <list> * <value>classpath:edu/cau/base/entity/*.hbm.xml</value> $ <value>classpath:edu/cau/diagnosis/entity/*.hbm.xml</value> - </list> the </property> + </bean>
3, sessionfactory configuration, Dynamicsessionfactoryimpl class is a what ghost please refer to the article at the beginning of the great God.
1 class= "Edu.cau.common.dbaccess.impl.DynamicSessionFactoryImpl" >2 <property name= " Defaulttargetsessionfactory "ref=" Sqlserversessionfactory "></property>3 <property name= "Targetsessionfactorys" >4 <map>5 <entry value-ref= "Mysqlsessionfactory" key= "MYSQL" ></entry>6 <entry value-ref= "sqlserversessionfactory" key= "SQL Server" > </entry>7 </map>8 </property>9 </bean>
4, configuration transaction management.
1 <bean id= "Atomikostransactionmanager" class = " Com.atomikos.icatch.jta.UserTransactionManager "init-method=" Init "destroy-method=" Close "> 2 <description >UserTransactionManager</description> 3 <property name= "Forceshutdown" > 4 <value>true </value> 5 </property> 6 </bean> 7 <bean id= "atomikosusertransaction" class = "COM.ATOMIKOS.ICATCH.JTA.USERTRANSACTIONIMP" > 8 <property name= "TransactionTimeout" value= "300 "></property> 9 </bean>13 <bean id=" Mytxmanager "class =" Org.springframework.transaction.jta.JtaTransactionManager ">14 <property name=" TransactionManager "ref=" Atomikostransactionmanager "></property>15 <property name=" usertransaction "ref=" Atomikosusertransaction "></property>16 </bean>
5, configure the transaction notification and so on.
<!--transaction Notifications-
<tx:advice id= "Txadvice" transaction-manager= "Mytxmanager" > <tx:attributes> <tx:method Name= "find*" read-only= "true"/> <tx:method name= "get*" read-only= "true"/> <tx:method name= " list* "read-only=" true "/> <tx:method name=" * "rollback-for=" throwable "/> </tx:attributes> </tx:advice>
<!--AOP configuration classes controlled by transactions--><aop:config> <aop:pointcut id= "serviceoperation" expression= "Beans (*service ) "/> <!--scan the bean with the end of service-- <aop:advisor advice-ref=" Txadvice "pointcut-ref=" Serviceoperation "/></aop:config>
Java Project Multi-data source configuration