Spring实现WebService分布式事务一致性

1.分布式事物

分布式事务是指操作多个数据库之间的事务,为了保证事物的一致性,一般都采用2阶段提交的办法实现。

这里强调下一致性要求,如果追求强一致性就只能采用JTA事物实现。如果是最终一致性就不需要JTA实现了,可以采用异步消息队列实现。我这里用的是spring提供的JTA事物,因为这是个人习惯。

2.服务端实现与配置

服务端采用CXF 写webservice实现,关于webservice的实现与配置可以自行百度实现。我在服务端写了多个配置文件,其中一个是webservice的发布文件,一个是实现DAO层的配置,这里是不需要声明事物和对应切面的,但是一定要配置支持XA的数据源。JTA事物的配置在客户端实现。我项目里数据源采用阿里的druid。服务端的结果通过json传递给客户端。

服务端部分配置如下:

  <bean id="helloServiceBean" class="com.liuyu.service.impl.HelloServiceImpl"/>
  <jaxws:server id="helloService" serviceclass="com.liuyu.service.HelloServiceI" address="/Hello">
  <jaxws:serviceBean>                 
    <ref bean="helloServiceBean"/>   
  </jaxws:serviceBean>
    </jaxws:server>
 3.客户端测试
  客户端部分配置文件如下:
    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
  init-method="init" destroy-method="close">
  <property name="forceShutdown" value="true" />
  </bean>
  <bean id="atomikoSUSErTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
  <property name="transactionTimeout" value="300" />
  </bean>
  <!-- JTA事务管理器 -->
  <bean id="springTransactionManager"
  class="org.springframework.transaction.jta.JtaTransactionManager">
  <property name="transactionManager" ref="atomikosTransactionManager" />
  <property name="userTransaction" ref="atomikosUserTransaction" />
  </bean>
  <!-- 事务管理 -->
  <tx:advice id="txAdvice" transaction-manager="springTransactionManager">
  <tx:attributes>
  <tx:method name="save*" propagation="REQUIRED"/>
  <tx:method name="add*" propagation="REQUIRED"/>
  <tx:method name="create*" propagation="REQUIRED"/>
  <tx:method name="insert*" propagation="REQUIRED"/>
  <tx:method name="update*" propagation="REQUIRED"/>
  <tx:method name="delete*" propagation="REQUIRED"/>
  <tx:method name="*" read-only="true" />
  </tx:attributes>
  </tx:advice>
  <aop:config proxy-target-class="true">
  <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.liuyu.test..*.*(..))" />
  </aop:config>
 
  客户端实现类部分如下:
 @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");
  }
  //@Test
  public void queryAllXA() {
    String str = demoService.queryAll();
    List<HashMap<String, Object>> list=JSON.parseObject(str, new TypeReference<List<HashMap<String,Object>>>(){});
    for(Map<String, Object> 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: com.atomikos.icatch.jta.UserTransactionImp@101b7cf
  [org.springframework.transaction.jta.JtaTransactionManager]Using JTA TransactionManager: com.atomikos.icatch.jta.UserTransactionManager@1cab519
  一月 20, 2015 4:44:56 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
  信息: Creating Service {http://service.liuyu.com/}HelloServiceIService from class com.liuyu.service.HelloServiceI
  一月 20, 2015 4:44:57 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
  信息: Creating Service {http://service.demo.liuyu.com/}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 = com.liuyu.test.TestClient@b691c6, testMethod = saveInfo@TestClient, 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

Spring 的详细介绍:请点这里
Spring 的下载地址:请点这里 

相关推荐