即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

分布式事务

编程语言 u010081710 5℃ 0评论

多数据源事务控制。

pom.xml



 4.0.0

 com.xy
 springmvc-mybatis
 1.0-SNAPSHOT
 war
 
  4.1.1.RELEASE
 
 
  
   
    org.apache.maven.plugins
    maven-surefire-plugin
    2.15
    
     true
    
   
   
    org.apache.maven.plugins
    maven-compiler-plugin
    3.1
    
     1.7
     1.7
     true
    
   
   
    org.apache.maven.plugins
    maven-war-plugin
    
    
    
   
   
    org.mortbay.jetty
    maven-jetty-plugin
    6.1.26
    
     10
     
      /maven-app
     
    
   
  
 
 
  
   org.springframework
   spring-jdbc
   ${spring.version}
  
  
   org.springframework
   spring-context-support
   ${spring.version}
  
  
   org.springframework
   spring-context
   ${spring.version}
  
  
   com.fasterxml.jackson.core
   jackson-core
   2.3.0
  
  
   com.fasterxml.jackson.core
   jackson-databind
   2.3.0
  
  
   ch.qos.logback
   logback-classic
   1.0.11
  
  
   ch.qos.logback
   logback-core
   1.0.11
  
  
   org.slf4j
   slf4j-api
   1.7.6
  
  
   org.springframework
   spring-webmvc
   ${spring.version}
  
  
   javax.servlet
   servlet-api
   2.5
   provided
  
  
   org.springframework
   spring-orm
   ${spring.version}
  
  
   org.springframework
   spring-tx
   ${spring.version}
  
  
   commons-dbcp
   commons-dbcp
   1.4
  
  
   commons-fileupload
   commons-fileupload
   1.3.1
  

  
   javax.servlet
   javax.servlet-api
   3.1.0
  
  
   org.mybatis
   mybatis
   3.2.7
  

  
   mysql
   mysql-connector-java
   5.0.8
  
  
   com.microsoft.sqlserver
   sqljdbc4
   4.0
  
  
   oracle
   ojdbc6
   6.0
  
  
   org.mybatis
   mybatis-spring
   1.2.2
  
  
   com.atomikos
   atomikos-util
   3.7.0
  
  
   com.atomikos
   transactions-jta
   3.7.0
  
  
   com.atomikos
   transactions
   3.7.0
  
  
   com.atomikos
   transactions-api
   3.7.0
  
  
   com.atomikos
   transactions-jdbc
   3.7.0
  
  
   org.codehaus.btm
   btm
   2.1.4
  
  
  
   junit
   junit
   4.11
  
  
   org.springframework
   spring-test
   ${spring.version}
  
 

model

package com.xy.model;

public class NameDev {
    private long id;
    private String nameDev;
 //略...
}
package com.xy.model;

public class NameQa {
    private long id;
    private String nameQa;
}

dao

package com.xy.daodev;

import org.springframework.stereotype.Repository;
import com.xy.model.NameDev;

@Repository
public interface NameDevMapper {
    int insert(NameDev nameDev);
}

package com.xy.dao;

import org.springframework.stereotype.Repository;
import com.xy.model.NameQa;

@Repository
public interface NameQaMapper {
    int insert(NameQa nameQa);
}

service

package com.xy.service;

import com.xy.dao.NameQaMapper;
import com.xy.daodev.NameDevMapper;
import com.xy.model.NameDev;
import com.xy.model.NameQa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class NameService {
    @Autowired
    NameQaMapper nameQaMapper;
    @Autowired
    NameDevMapper nameDevMapper;

    @Transactional(rollbackFor = Exception.class)
    public void addQaAndDev(boolean hasException) throws Exception {
        NameQa nameQa = new NameQa();
        nameQa.setNameQa("qa");
        nameQaMapper.insert(nameQa);

        NameDev nameDev = new NameDev();
        nameDev.setNameDev("dev");
        nameDevMapper.insert(nameDev);
     
        if(hasException) {
            throw new Exception();
        }
    }

}

mybatis配置



    
        
    
    
    

    
        
        
        
        
        
        
        
        
        
        
        
            select 1
        
    

    
        
        
        
        
            
                ${mysql.qa.db.url}
                ${mysql.qa.db.user}
                ${mysql.qa.db.password}
                true
            
        
    

    
        
        
        
        
            
                ${mysql.dev.db.url}
                ${mysql.dev.db.user}
                ${mysql.dev.db.password}
                true
            
        
    

    
        
        
    

    
        
        
    

    
        
            true
        
    
    
        
    

    
        
            
        
        
            
        
        
        

    

    
        
        
    

    
        
        
    

jta.properties

com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
com.atomikos.icatch.console_file_name = tm.out
com.atomikos.icatch.log_base_name = tmlog
com.atomikos.icatch.tm_unique_name = com.atomikos.spring.jdbc.tm
com.atomikos.icatch.console_log_level = INFO
com.atomikos.icatch.output_dir=/hello/atomikos
com.atomikos.icatch.log_base_dir=/hello/atomikos
com.atomikos.icatch.serial_jta_transactions=false

junit单元测试

package com.xy.test;

import com.xy.model.NameQa;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.xy.dao.NameQaMapper;
import com.xy.daodev.NameDevMapper;
import com.xy.model.NameDev;
import com.xy.service.NameService;

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations={"/context/mysql-beans.xml"}) //加载配置文件
public class TestJTA{
 
 @Autowired
 private NameService nameservice; 
 @Autowired
 private NameDevMapper namedevmapper; 
 @Autowired
 private NameQaMapper nameqamapper;
 
 @Test
 public void testDao(){
  /***插入namedev**/
  NameDev namedev=new NameDev();
  namedev.setId(11L);
  namedev.setNameDev("namedev");
  namedevmapper.insert(namedev);
  /***插入nameqa**/
  NameQa nameqa=new NameQa();
  nameqa.setId(1L);
  nameqa.setNameQa("nameqa");
  nameqamapper.insert(nameqa);  
 }
 
 @Test
 public void testService(){
  try {
   nameservice.addQaAndDev(false);
  } catch (Exception e) {
   e.printStackTrace();
  }
 } 
}








转载请注明:CodingBlog » 分布式事务

喜欢 (0)or分享 (0)