JTA
通过webwork+spring+hibernatep实现分布式事务(JTA),采用容器必须应用服务器(weblogic,webspere,jboss,jonas等)。实现JTA主要有三个步骤,1.设置数据源,2.配置srping,及hiberante相应配置文件,3.在程序中编写代码。测试时候采用二个数据库,全是采用mysql +weblogic4.0版本。
1.在weblogic设置数据源
首先配置二个连接池:mysqlpool1、mysqlpool2,分别对应二个两个数据库 test1,test2
然后配置二个txDataSource:ds1、ds2
配置如下:
<JDBCConnectionPool DriverName="org.gjt.mm.mysql.Driver"
Name=" mysqlpool1"
PasswordEncrypted="{3DES}9hRoxaA7+106d9AbXZ8e2g=="
Properties="user=root" Targets="myserver"
TestTableName="SQL SELECT 1" URL="jdbc:mysql://localhost:3306/test1"/>
<JDBCConnectionPool DriverName="org.gjt.mm.mysql.Driver"
Name=" mysqlpool2"
PasswordEncrypted="{3DES}9hRoxaA7+106d9AbXZ8e2g=="
Properties="user=root" Targets="myserver"
TestTableName="SQL SELECT 1" URL="jdbc:mysql://localhost:3306/test1"/>
<JDBCTxDataSource JNDIName="ds1" Name=" mysqlpool1"
PoolName="mysqlpool1" Targets="myserver"/>
<JDBCTxDataSource JNDIName="ds2" Name=" mysqlpool1"
PoolName="mysqlpool2" Targets="myserver"/>
2.配置srping,及hiberante相应配置文件 配置二个相对应的 hibernate.cfg..xml文件:Test1.cfg.xml、Test2.cfg.xml配置文件 主要是引用应用服务器的数据源及Mapping files相关配置
<!-- following is c3p0 connection pool setting-->
<property name="hibernate.c3p0.min_size">
5
</property>
<property name="hibernate.c3p0.max_size">
500
</property>
<property name="hibernate.c3p0.timeout">
1800
</property>
<property name="hibernate.c3p0.max_statements">
50
</property>
<!--dialect-->
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<!-- show sql -->
<property name="hibernate.show_sql">
True
</property>
<!-- use out join-->
<property name="hibernate.use_outer_join">
True
</property>
<!--use cache-->
<property name="hibernate.cache.use_query_cache">
</property>
<!-- Transaction Manager Type, here use JDBC Transaction
<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
-->
<property name="transaction.factory_class">
net.sf.hibernate.transaction.JTATransactionFactory
</property>
<property name="jta.UserTransaction">
java:comp/UserTransaction
</property>
<!-- Mapping files -->
<mapping resource="com/eastsun/model/Test2.hbm.xml"/>
<mapping resource="com/eastsun/model/Test.hbm.xml"/>
</session-factory>
另外一个配置文件就同基本上一样的。只修改hibernate.connection.datasource的属性值为ds2
Spring.xml配置文件
主要是配置二个数据源相对应的sessionFactory,再配置一个JTA(myTransactionManager),然后再配置bean。在两个Bean后引用对应的sessonFactory及JTA(myTransactionManager)
<?xml version="1.0" encoding="UTF-8" ?>
</bean>
<bean id="sessionFactory2" class="org.springframework.orm.hibernate.LocalSessionFactoryBean" singleton="true">
<property name="configLocation">
<value>classpath:spring2.cfg.xml</value>
</property>
</bean>
<bean id="myTransactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager" />
<bean id="entityDao1"
class="com.eastsun.common.persistence.EntityDaoImpl"
singleton="true">
<property name="sessionFactory">
<ref bean="sessionFactory1" />
</property>
</bean>
<bean id="entityDao2"
class="com.eastsun.common.persistence.EntityDaoImpl"
singleton="true">
<property name="sessionFactory">
<ref bean="sessionFactory2" />
</property>
</bean>
<bean id="spring1"
class="com.eastsun.businesslogic.test.Spring1Impl" singleton="true">
<property name="sessionFactory">
<ref bean="sessionFactory1" />
</property>
<property name="transactionManager">
<ref bean="myTransactionManager" />
</property>
</bean>
<bean id="spring11"
class="com.eastsun.businesslogic.test.Spring11Impl" singleton="true">
<property name="sessionFactory">
<ref bean="sessionFactory1" />
</property>
<property name="transactionManager">
<ref bean="myTransactionManager" />
</property>
</bean>
<bean id="spring2"
class="com.eastsun.businesslogic.test.Spring2Impl" singleton="true">
<property name="sessionFactory">
<ref bean="sessionFactory2" />
</property>
<property name="transactionManager">
<ref bean="myTransactionManager" />
</property>
</bean>
-
在程序中编写代码
在第一数据库插入数据库能正常插入,在另外一个数据库插入的时候没有加设置主键ID,这样就会出异常,如果第一次插入的数据也能回滚,不会持久化到数据库,这样就说明JTA事务成功了。
public String insertData(String data){
Session session = null;
Transaction tx = null;
Test test = new Test();
try{
AppLog.message("...........insertData...............................");
session = getSession(true);
Spring2 spring2 =(Spring2)ServiceLocatorOfTest.getInstance("spring1.xml").getService("spring2");
spring2.insertData(UUIDGen.getUUID(),UUIDGen.getUUID());
tx = session.beginTransaction();
test.setTestId(UUIDGen.getUUID());
test.setName(data);
session.save(test);
tx.commit();
}catch(Exception e){
AppLog.message("...........insertData.....Exception..........................");
try{ if(tx!=null)tx.rollback();}catch(Exception ex){};
e.printStackTrace();
}finally{
try{
if(session!=null){
session.close();
}
}
catch(Exception e1){
e1.printStackTrace();
}
}
return "success";
}
首先配置二个连接池:mysqlpool1、mysqlpool2,分别对应二个两个数据库 test1,test2 8)