1.分布式事物
分布式事务是指操作多个数据库之间的事务,为了保证事物的一致性,一般都采用2阶段提交的办法实现。
这里强调下一致性要求,如果追求强一致性就只能采用JTA事物实现。如果是最终一致性就不需要JTA实现了,可以
采用异步消息队列实现。我这里用的是spring提供的JTA事物,因为项目需要强一致性。
2.服务端实现与配置
服务端采用CXF 写webservice实现,关于webservice的实现与配置可以自行百度实现。我在服务端写了多个配置文件
,其中一个是webservice的发布文件,一个是实现DAO层的配置,这里是不需要声明事物和对应切面的,但是一定要配置支
持XA的数据源。JTA事物的配置在客户端实现。我项目里数据源采用阿里的druid。服务端的结果通过json传递给客户端。
服务端部分配置如下:
3.客户端测试
客户端部分配置文件如下:
init-method="init" destroy-method="close">
class="org.springframework.transaction.jta.JtaTransactionManager">
客户端实现类部分如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:applicationContext-client.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = false)
public class TestClient {
@Autowired
private HelloServiceI service;
@Autowired
private DemoServiceI demoService;
@Test
public void saveInfo() {
service.insertHello("2222", "kkkkk");
demoService.insertHello("2222", "jjj");
}
public void queryAllXA() {
String str = demoService.queryAll();
List
> list=JSON.parseObject(str, new TypeReference>>(){});
for(Map m:list){
System.out.println(m.get("id").toString()+" "+m.get("username").toString());
}
}
}
测试结果如下:
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING core version: 3.9.3
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.automatic_resource_registration = true
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.client_demarcation = false
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.threaded_2pc = false
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.serial_jta_transactions = true
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.serializable_logging = true
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.log_base_dir = .\
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.max_actives = 50
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.checkpoint_interval = 500
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.enable_logging = true
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.output_dir = .\
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.log_base_name = tmlog
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.max_timeout = 300000
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.tm_unique_name = 192.168.135.100.tm
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING java.naming.factory.initial = com.sun.jndi.rmi.registry.RegistryContextFactory
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING java.naming.provider.url = rmi://localhost:1099
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.service = com.atomikos.icatch.standalone.UserTransactionServiceFactory
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.force_shutdown_on_vm_exit = false
[com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.default_jta_timeout = 10000
[org.springframework.transaction.jta.JtaTransactionManager]Using JTA UserTransaction:
[org.springframework.transaction.jta.JtaTransactionManager]Using JTA TransactionManager:
一月 20, 2015 4:44:56 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {}HelloServiceIService from class com.liuyu.service.HelloServiceI
一月 20, 2015 4:44:57 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {}DemoServiceIService from class com.liuyu.demo.service.DemoServiceI
[com.atomikos.icatch.imp.thread.TaskManager]THREADS: using JDK thread pooling...
[com.atomikos.icatch.imp.BaseTransactionManager]createCompositeTransaction ( 300000 ): created new ROOT transaction with id 192.168.135.100.tm0000100002
[org.springframework.test.context.transaction.TransactionContext]Began transaction (1) for test context [DefaultTestContext@f2ed42 testClass = TestClient, testInstance = , testMethod = , testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1c63a8 testClass = TestClient, locations = '{classpath*:applicationContext-client.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]; transaction manager [org.springframework.transaction.jta.JtaTransactionManager@1d2d7c9]; rollback [false]
[com.atomikos.icatch.imp.CompositeTransactionImp]commit() done (by application) of transaction 192.168.135.100.tm0000100002
阅读(2956) | 评论(0) | 转发(0) |