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 ();
}