Transaction Management for multiple data sources in spring and Hibernate

Source: Internet
Author: User

1. Preparations

My project is based on the struts2 + spring + hibernate architecture, and Tomcat is used for Web Services;

The problem is to connect multiple databases to one Oracle database and one SQL Server. Now I will share my configuration process with you!

Use JTA transactions and use Tomcat + jotm to provide the Transaction Manager
Freight expert

Download the corresponding jar package of jotm and put it in the Lib package of the project.

2. Configure the hibernate configuration file. There are several databases with such files.

My sqlserver database configuration files are as follows:

Example: hibernate_sqlserver.cfg.xml

<Hibernate-configuration>

<Session-factory>

<Property name = "connection. username"> SA </property>

<Property name = "connection. url"> JDBC: Microsoft: sqlserver: // 192.168.0.100: 1433; databasename = prisonsoftweb1

</Property>

<Property name = "dialect"> org. hibernate. dialect. sqlserverdialect

</Property>

<Property name = "myeclipse. Connection. Profile"> test </property>

<Property name = "connection. Password"> server </property>

<Property name = "connection. driver_class"> com. Microsoft. JDBC. sqlserver. sqlserverdriver

</Property>

</Session-factory>

<Hibernate-configuration>

Other databases are similar to them!

3. Configure sping: add the corresponding session factory to applicationcontext. xml.

(1) The following two beans are used for spring to initialize jotm.

<Bean id = "jotm" class = "org. springframework. transaction. JTA. jotmfactorybean"/>

<Bean id = "transactionmanager" class = "org. springframework. transaction. JTA. jtatransactionmanager">

<Property name = "usertransaction"> <ref local = "jotm"/> </property>

</Bean>

If your Program To process BLOB data, add the following information:

<! -- Blob processing -->

<Bean id = "nativejdbcextractor" lazy-init = "true"
Class = "org. springframework. JDBC. Support. nativejdbc. simplenativejdbcextractor"/>

<Bean id = "lobhandler" lazy-init = "true" class = "org. springframework. JDBC. Support. lob. oraclelobhandler">

<Property name = "nativejdbcextractor">

<Ref bean = "nativejdbcextractor"/>

</Property>

</Bean>

The following information is added by the programmer or designer based on the actual situation:

(2) Add a session Factory (Note that there must be a factory id = "sessionfactory ")

<! -- Oracle Database sessionfactory -->

<Bean id = "sessionfactory" class = "org. springframework. Orm. hibernate3.localsessionfactorybean">

<Property name = "configlocation"

Value = "classpath: hibernate. cfg. xml">

</Property>

<Property name = "hibernateproperties">

<Props>

<Prop key = "hibernate. dialect"> org. hibernate. dialect. oracle9dialect </prop>

<Prop key = "hibernate. current_session_context_class"> JTA </prop>

<Prop key = "hibernate. Connection. release_mode"> after_statement </prop>

<Prop key = "hibernate. show_ SQL"> true </prop>

<Prop key = "hibernate. format_ SQL"> true </prop>

<Prop key = "hibernate. order_updates"> true </prop>

</Props>

</Property>

<! -- Handle declaration for processing BLOB fields -->

<Property name = "lobhandler">

<Ref local = "lobhandler"/>

</Property> <! -- This is the handle declared above -->

<Property name = "jtatransactionmanager">

<Ref bean = "jotm"/>

</Property>

</Bean>

<! -- SQL Server database sessionfactory -->

<Bean id = "sessionfactoryforsqlserver" class = "org. springframework. Orm. hibernate3.localsessionfactorybean">

<Property name = "configlocation"

Value = "classpath: hibernate_sqlserver.cfg.xml">

</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 = "jtatransactionmanager">

<Ref bean = "jotm"/>

</Property>

</Bean>

(3) inject the corresponding session factory for Dao

<Bean id = "sqlserverbasedao" class = "com. gkzx. Dao. basedaoimpl">

<Property name = "sessionfactory">

<Ref bean = "sessionfactoryforsqlserver"/>

</Property>

</Bean>

4. Manual commit: manual commit and rollback of transactions are adopted in DAO implementation to avoid deadlock caused by the database's inability to submit transactions in a timely manner.

This. getsession (). clear ();
transaction Tx = NULL;
try {
Tx = This. getsessionfactory (). getcurrentsession (). begintransaction ();
// write the Code of your database operation: example:
This. gethibernatetemplate (). update (objbean);
Tx. commit ();
} catch (runtimeexception e) {
If (TX! = NULL) Tx. rollback ();
throw E;

}finally {
// This. getsession (). close ();

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.