分布式事务是指事务的参与者、支持事务的服务器、资源管理器以及事务管理器分别位于分布系统的不同节点之上,在两个或多个网络计算机资源上访问并且更新数据,将两个或多个网络计算机的数据进行的多次操作作为一个整体进行处理。如不同银行账户之间的转账。
对于在项目中接触到JTA,大部分的原因是因为在项目中需要操作多个数据库,同时,可以保证操作的原子性,保证对多个数据库的操作一致性。
1、项目结构图:
2、pom.xml
4.0.0
org.nercita.ltxx
jtaTest
0.0.1-SNAPSHOT
jar
jtaTest
http://maven.apache.org
UTF-8
4.1.6.RELEASE
junit
junit
4.12
test
org.springframework
spring-test
${spring.version}
provided
mysql
mysql-connector-java
5.1.26
com.oracle
ojdbc6
12.1.0.1
com.atomikos
transactions-jdbc
3.9.3
javax.transaction
jta
1.1
javax
javaee-api
7.0
org.springframework
spring-context
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.aspectj
aspectjweaver
1.8.2
org.aspectj
aspectjrt
1.8.2
2、applicationContext.xml
master xa datasource
masterDataSource
root
0729
jdbc:mysql://127.0.0.1:3306/master
slave xa datasource
slaveDataSource
root
0729
jdbc:mysql://127.0.0.1:3306/slave
UserTransactionManager
true
ps:数据库和表需自行创建
3、java类
dao:
package org.nercita.ltxx.jtatest.dao;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class JtaTestMasterDao{
@Resource(name="masterJdbcTemplate")
JdbcTemplate masterJdbcTemplate;
public String master() {
masterJdbcTemplate.execute("update teacher set name='master' where id=1");
return "success";
}
public void update() {
masterJdbcTemplate.execute("update teacher set name='8' where id=1");
System.out.println("update");
masterJdbcTemplate.execute("fff teacher set name=''6' where id=1");
}
}
package org.nercita.ltxx.jtatest.dao;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class JtaTestSlaveDao{
@Resource(name="slaveJdbcTemplate")
JdbcTemplate slaveJdbcTemplate;
public String slave() {
slaveJdbcTemplate.execute("update student set name='slave' where id=1");
return "success";
}
}
service:
package org.nercita.ltxx.jtatest.service;
import javax.annotation.Resource;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import org.nercita.ltxx.jtatest.dao.JtaTestMasterDao;
import org.nercita.ltxx.jtatest.dao.JtaTestSlaveDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.jta.JtaTransactionManager;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
@Service
public class JtaTestService{
@Resource(name = "springTransactionManager")
private JtaTransactionManager txManager;
@Autowired
private JtaTestMasterDao jtaTestMasterDao;
@Autowired
private JtaTestSlaveDao jtaTestSlaveDao;
@Resource(name = "transactionTemplate")
private TransactionTemplate transactionTemplate;
//编程式
public String test() {
UserTransaction userTx = txManager.getUserTransaction();
try {
userTx.begin();
jtaTestMasterDao.master();
jtaTestSlaveDao.slave();
int a=1/0;
System.out.println(a);
userTx.commit();
} catch (Exception e) {
System.out.println("捕获到异常,进行回滚" + e.getMessage());
e.printStackTrace();
try {
userTx.rollback();
} catch (IllegalStateException e1) {
System.out.println("IllegalStateException:" + e1.getMessage());
} catch (SecurityException e1) {
System.out.println("SecurityException:" + e1.getMessage());
} catch (SystemException e1) {
System.out.println("SystemException:" + e1.getMessage());
}
}
return null;
}
//声明式
@Transactional
public void update(){
jtaTestMasterDao.master();
int a=1/0;
System.out.println(a);
jtaTestSlaveDao.slave();
}
//事务模板方式
public void test3() {
transactionTemplate.execute(new TransactionCallbackWithoutResult(){
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
jtaTestMasterDao.master();
jtaTestSlaveDao.slave();
int a=1/0;
System.out.println(a);
} catch (Exception ex) {
// 通过调用 TransactionStatus 对象的 setRollbackOnly() 方法来回滚事务。
status.setRollbackOnly();
ex.printStackTrace();
}
}
});
/*
//有返回值的回调
Object obj=transactionTemplate.execute(new TransactionCallback(){
@Override
public Object doInTransaction(TransactionStatus status) {
return 1;
}
});
*/
}
}
controller(本文直接使用controller进行测试)
package org.nercita.ltxx.jtatest.web;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.nercita.ltxx.jtatest.service.JtaTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
@Controller
public class JtaTestController {
@Autowired
private JtaTestService jtaTestService;
//MySQL的数据库引擎必须是InnoDB,否则无法回滚
@Test
public void test(){
jtaTestService.test();
}
@Test
public void test2(){
jtaTestService.update();
}
@Test
public void test3(){
jtaTestService.test3();
}
}
4、数据源详细参数配置
select 1
${jdbc.url}
${jdbc.username}
${jdbc.password}
${jdbca.url}
${jdbca.username}
${jdbca.password}