论坛首页 Java企业应用论坛

Spring JTA应用JOTM & Atomikos II JOTM

浏览 8776 次
精华帖 (1) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-12-11  
上节建立了一个简单的Java Application以及所需要的数据库和数据表,本节将介绍JOTM在Spring中的配置。


JOTM(Java Open Transaction Manager)是ObjectWeb的一个开源JTA实现,本身也是开源应用程序服务器JOnAS(Java Open Application Server)的一部分,为其提供JTA分布式事务的功能。Spring对JOTM提供了较好的支持,提供了一个org.springframework.transaction.jta.JotmFactoryBean的支持类,在Spring2.0中也包含了JOTM相关的一些library。

jotm的下载地址为http://jotm.objectweb.org,最新版本为2.0.10.

下载完成后解压缩,然后打开jotm下面conf文件夹,拷贝carol.properties文件到classpath中,并修改这个文件如下
carol.properties
# do not use CAROL JNDI wrapper   
carol.start.jndi=false   
   
# do not start a name server   
carol.start.ns=false   
   
# Naming Factory
carol.jndi.java.naming.factory.url.pkgs=org.apache.naming

上面配置文件的目的是不使用JNDI的方式来加载JOTM的配置,当然也可以根据需要选择其它的一些配置,例如JTOM所提供的默认配置。

然后开始在Spring上下文中配置JOTM,在classpath中建立一个ApplicationContext-jotm.xml,配置如下

ApplicationContext-jotm.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
	
	<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="userTransaction" ref="jotm" />
	</bean>

	<bean id="ds1" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:MySQL://localhost:3306/test" />
			</bean>
		</property>
		<property name="user" value="root" />
		<property name="password" value="admin" />
	</bean>
	
	<bean id="ds2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:MySQL://localhost:3306/test2" />
			</bean>
		</property>
		<property name="user" value="root" />
		<property name="password" value="admin" />
	</bean>

	<bean id="template1" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="ds1" />
	</bean>
	
	<bean id="template2" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="ds2" />
	</bean>
	
	<bean id="dao1" class="com.xa.dao.UserDao1">
		<property name="jdbcTemplate">
			<ref bean="template1"></ref>
		</property>
	</bean>
	
	<bean id="dao2" class="com.xa.dao.UserDao2">
		<property name="jdbcTemplate">
			<ref bean="template2"></ref>
		</property>
	</bean>
	
	<bean id="userServiceTarget" class="com.xa.service.UserServiceImpl">
		<property name="dao1" ref="dao1"/>
		<property name="dao2" ref="dao2"/>
	</bean>


	<bean id="userTest" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">    
		<property name="transactionManager">
			<ref bean="txManager"/>
		</property>    
		<property name="target">
			<ref bean="userServiceTarget"/>
		</property>
		<property name="transactionAttributes">        
			<props>
				<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>            
			</props>
		</property>
	</bean>
</beans>

上面是一个完整的Spring上下文配置,可以看第一个bean “jotm”,实际上引用了Spring内部所提供的对JOTM支持的工厂类,参考下面的配置代码段
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>


随后,配置了JTA事务管理器,并且在管理器中使用上面所配置的jotm,如下面的代码
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
	<property name="userTransaction" ref="jotm" />
</bean>


再接下来就是配置多个数据源了,使用jotm提供的org.enhydra.jdbc.pool.StandardXAPoolDataSource类,根据类名可以明确地看出它是用以配置多个数据源的啦,配置的代码如下
<bean id="ds1" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:MySQL://localhost:3306/test" />
			</bean>
		</property>
		<property name="user" value="root" />
		<property name="password" value="admin" />
	</bean>
	
	<bean id="ds2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:MySQL://localhost:3306/test2" />
			</bean>
		</property>
		<property name="user" value="root" />
		<property name="password" value="admin" />
	</bean>

这里配置的两个数据源都连接到本地的mysql,实际上可以连接到不同的db server和不同类型的数据库,已经经过测试,这里为了方便,在本地建立了两个不同的数据库(test,test2)做测试。

随后的配置基本上和普通的Spring上下文配置相同了,根据不同的数据源配置两个jdbcTemplate,两个dao分别引用不同的jdbcTemplate, 将两个dao注入到UserService中, 最后将service纳入事务管理,并在事务代理配置中配置回滚规则,意思为如遇异常,则强制回滚内容。配置如下所示
<property name="transactionAttributes">        
	<props>
		<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>            
	</props>
</property>


这样,一个使用JOTM JTA事务的简单应用算大致成型了,最后,写一个JUnit,来测试一下结果
TestXa.java
package com.xa;

import org.springframework.context.ApplicationContext;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

import com.xa.service.UserService;

public class TestXa extends AbstractDependencyInjectionSpringContextTests
{
	protected String[] getConfigLocations() {
		return new String[] { "classpath:ApplicationContext-jotm.xml" };
	}

	public void testInsertBothDatabase() {
		ApplicationContext ctx = this.getApplicationContext();
		UserService ut = (UserService)ctx.getBean("userTest");
		try {
	        ut.insertBothDatabase("1", null);
        }
        catch (Exception e) {
        	e.printStackTrace();
        }
	}
}

在test中,调用了UserService的insertBothDatabase方法,有两个参数,userId和UserName,另外在方法的实现中调用了两个使用不同数据源dao,分别向两个不同的数据库插入输入,而test2数据库的xa_test表中,name字段是不允许为空的,因此,在插入test2数据库时会失败.

运行这个test,然后察看数据库结果:),test和test2数据库中都没有插入成功,看serviceImpl中的代码可以知道,逻辑上dao1会先于dao2执行,但是由于JTA事务,在dao2插入数据出现异常时整个事务被回滚,由于事务被配置在service层,dao1和dao2都被纳入一个事务进行管理,呵呵。修改一下方法的参数,修改为
ut.insertBothDatabase("1", "name1");


然后再试试test看数据库结果,如何?

第三节将简单介绍Atomikos在Spring中的配置和使用。
   发表时间:2008-05-30  
DEBUG 14:08:58 (Logger.java:37) - StandardXADataSource:getConnection return connection associated with a given XID
DEBUG 14:08:58 (Logger.java:37) - StandardXAConnection:commit case(state)
DEBUG 14:08:58 (Logger.java:37) - StandardXAConnection:commit try to commit a connection (STATUS_ACTIVE)
DEBUG 14:08:58 (Logger.java:37) - StandardXAConnection:commit commit is ok
DEBUG 14:08:58 (Logger.java:37) - StandardXAConnection:commit setAutoCommit to 'true'
DEBUG 14:08:58 (Logger.java:37) - StandardXADataSource:freeConnection
DEBUG 14:08:58 (Logger.java:37) - StandardXADataSource:freeConnection remove id from xidConnections
DEBUG 14:08:58 (Logger.java:37) - StandardXAStatefulConnection:setState Stateful connection: 1 (state before=0)
DEBUG 14:08:58 (Logger.java:37) - StandardXAStatefulConnection:setState Stateful connection: 1 (state after=6)
DEBUG 14:08:58 (SubCoordinator.java:1551) - doAfterCompletion()
DEBUG 14:08:58 (SubCoordinator.java:1558) - sychronization list size= 1
DEBUG 14:08:58 (SubCoordinator.java:1565) - Synchronization sync= org.hibernate.transaction.CacheSynchronization
DEBUG 14:08:58 (SubCoordinator.java:1566) - sync.afterCompletion status= STATUS_COMMITTED
DEBUG 14:08:58 (CacheSynchronization.java:68) - transaction after completion callback, status: 3
DEBUG 14:08:58 (JDBCContext.java:283) - after transaction completion
DEBUG 14:08:58 (SessionImpl.java:403) - after transaction completion
DEBUG 14:08:58 (Current.java:1275) - threadTx.set = null
DEBUG 14:08:58 (Current.java:1988) - remove tx from xid (xid=bb14:38:0:01938febbcf9084673...9da401:)
DEBUG 14:08:58 (SubCoordinator.java:1580) - SubCoordinator unexported [subcoord=org.objectweb.jotm.SubCoordinator@b3319f]
DEBUG 14:08:58 (Current.java:499) - threadTx.set= null
DEBUG 14:08:58 (Current.java:500) - reset timeout= 60
DEBUG 14:08:58 (AbstractPlatformTransactionManager.java:697) - Triggering afterCompletion synchronization
DEBUG 14:08:58 (TransactionSynchronizationManager.java:265) - Clearing transaction synchronization
DEBUG 14:09:55 (ConnectionManager.java:369) - running Session.finalize()
DEBUG 14:09:55 (ConnectionManager.java:369) - running Session.finalize()
DEBUG 14:23:49 (Logger.java:37) - GenericPool:cleanUp clean up the pool
DEBUG 14:23:49 (Logger.java:37) - GenericPool:cleanUp clean up the pool
DEBUG 14:23:49 (Logger.java:37) - GenericPool:cleanUp killing an object
DEBUG 14:23:49 (Logger.java:37) - StandardPoolDataSource:expire expire a connection, remove from the pool
DEBUG 14:23:49 (Logger.java:37) - StandardXAConnection:close the XAConnection
DEBUG 14:23:49 (Logger.java:37) - StandardXADataSource:connectionClosed
DEBUG 14:23:49 (Logger.java:37) - StandardPoolDataSource:expire close the connection
DEBUG 14:23:49 (Logger.java:37) - GenericPool:cleanUp killing an object
DEBUG 14:23:49 (Logger.java:37) - StandardPoolDataSource:expire expire a connection, remove from the pool
DEBUG 14:23:49 (Logger.java:37) - StandardXAConnection:close the XAConnection
DEBUG 14:23:49 (Logger.java:37) - StandardXADataSource:connectionClosed
DEBUG 14:23:49 (Logger.java:37) - StandardXADataSource:connectionClosed close any free connections
DEBUG 14:23:49 (Logger.java:37) - StandardPoolDataSource:expire close the connection
INFO 14:23:49 (Logger.java:43) - GenericPool:cleanUp less than minSize objects in the pool min=2 max=50 count=0
DEBUG 14:23:49 (Logger.java:37) - StandardXADataSource:getXAConnection(user, password)
DEBUG 14:23:49 (Logger.java:37) - StandardDataSource:getConnection Connection from DriverManager is returned
DEBUG 14:23:49 (Logger.java:37) - StandardXAStatefulConnection created
DEBUG 14:23:49 (Logger.java:37) - StandardXAConnection created
DEBUG 14:23:49 (Logger.java:37) - StandardXAPoolDataSource:create create a object for the pool
DEBUG 14:23:49 (Logger.java:37) - StandardXADataSource:getXAConnection(user, password)
DEBUG 14:23:49 (Logger.java:37) - StandardDataSource:getConnection Connection from DriverManager is returned
DEBUG 14:23:49 (Logger.java:37) - StandardXAStatefulConnection created
DEBUG 14:23:49 (Logger.java:37) - StandardXAConnection created
DEBUG 14:23:49 (Logger.java:37) - StandardXAPoolDataSource:create create a object for the pool
INFO 14:23:49 (Logger.java:43) - GenericPool:cleanUp done min=2 max=50 count=2
DEBUG 14:33:49 (Logger.java:37) - GenericPool:cleanUp clean up the pool
DEBUG 14:33:49 (Logger.java:37) - GenericPool:cleanUp clean up the pool
DEBUG 14:33:49 (Logger.java:37) - GenericPool:cleanUp killing an object
DEBUG 14:33:49 (Logger.java:37) - StandardPoolDataSource:expire expire a connection, remove from the pool
DEBUG 14:33:49 (Logger.java:37) - StandardXAConnection:close the XAConnection
DEBUG 14:33:49 (Logger.java:37) - StandardXADataSource:connectionClosed
DEBUG 14:33:49 (Logger.java:37) - StandardPoolDataSource:expire close the connection
DEBUG 14:33:49 (Logger.java:37) - GenericPool:cleanUp killing an object
DEBUG 14:33:49 (Logger.java:37) - StandardPoolDataSource:expire expire a connection, remove from the pool
DEBUG 14:33:49 (Logger.java:37) - StandardXAConnection:close the XAConnection
DEBUG 14:33:49 (Logger.java:37) - StandardXADataSource:connectionClosed
DEBUG 14:33:49 (Logger.java:37) - StandardPoolDataSource:expire close the connection
INFO 14:33:49 (Logger.java:43) - GenericPool:cleanUp less than minSize objects in the pool min=2 max=50 count=0
DEBUG 14:33:49 (Logger.java:37) - StandardXADataSource:getXAConnection(user, password)
DEBUG 14:33:50 (Logger.java:37) - StandardDataSource:getConnection Connection from DriverManager is returned
DEBUG 14:33:50 (Logger.java:37) - StandardXAStatefulConnection created
DEBUG 14:33:50 (Logger.java:37) - StandardXAConnection created
DEBUG 14:33:50 (Logger.java:37) - StandardXAPoolDataSource:create create a object for the pool
DEBUG 14:33:50 (Logger.java:37) - StandardXADataSource:getXAConnection(user, password)
DEBUG 14:33:50 (Logger.java:37) - StandardDataSource:getConnection Connection from DriverManager is returned
DEBUG 14:33:50 (Logger.java:37) - StandardXAStatefulConnection created
DEBUG 14:33:50 (Logger.java:37) - StandardXAConnection created
DEBUG 14:33:50 (Logger.java:37) - StandardXAPoolDataSource:create create a object for the pool
INFO 14:33:50 (Logger.java:43) - GenericPool:cleanUp done min=2 max=50 count=2
0 请登录后投票
   发表时间:2008-05-30  
我也是按楼主操作的!为啥出现上面的日志后控制台就停止不动了!一直等待!楼主遇到过这样的问题吗!
0 请登录后投票
   发表时间:2008-07-01  
bad SQL grammar [insert into xa_test values('1',name1)]
0 请登录后投票
   发表时间:2008-07-02  
Atomikos文档全点。。。。建议使用。。
JOTM很久没更新了。。。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics