atomikos 使用示例
程序员文章站
2022-05-23 11:22:48
...
atomikos 使用示例
数据库h1:user表
数据库h2:person表
使用atomikos对数据库的数据操作做事务管理
***************************************
示例
***********************
config 层
数据源 h1
@Configuration
@MapperScan(basePackages = {"com.example.demo.dao.user"},sqlSessionFactoryRef = "sqlSessionFactory_1")
public class DataSourceConfig {
@Bean("datasource_1")
@ConfigurationProperties("spring.datasource.druid.one")
public DruidXADataSource initDruidXADataSource(){
return new DruidXADataSource();
}
@Bean("atomikosDatasource_1")
public AtomikosDataSourceBean initAtomikosDataSourceBean(@Qualifier("datasource_1") DruidXADataSource dataSource){
AtomikosDataSourceBean atomikosDataSourceBean=new AtomikosDataSourceBean();
atomikosDataSourceBean.setXaDataSource(dataSource);
atomikosDataSourceBean.setBeanName("datasource_1");
return atomikosDataSourceBean;
}
@Bean("sqlSessionFactory_1")
public SqlSessionFactory initSqlSessionFactory(@Qualifier("atomikosDatasource_1") AtomikosDataSourceBean dataSource) throws Exception{
MybatisSqlSessionFactoryBean sqlSessionFactoryBean=new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:/mapper/user/*.xml"));
return sqlSessionFactoryBean.getObject();
}
}
数据源 h2
@Configuration
@MapperScan(basePackages = {"com.example.demo.dao.person"},sqlSessionFactoryRef = "sqlSessionFactory_2")
public class DataSourceConfig2 {
@Bean("datasource_2")
@ConfigurationProperties("spring.datasource.druid.two")
public DruidXADataSource initDruidXADataSource2(){
return new DruidXADataSource();
}
@Bean("atomikosDataSource2")
public AtomikosDataSourceBean initAtomikosDataSourceBean(@Qualifier("datasource_2") DruidXADataSource dataSource){
AtomikosDataSourceBean atomikosDataSourceBean=new AtomikosDataSourceBean();
atomikosDataSourceBean.setXaDataSource(dataSource);
atomikosDataSourceBean.setUniqueResourceName("datasource2");
return atomikosDataSourceBean;
}
@Bean("sqlSessionFactory_2")
public SqlSessionFactory initSqlSessionFactory2(@Qualifier("atomikosDataSource2") AtomikosDataSourceBean dataSource) throws Exception{
MybatisSqlSessionFactoryBean sqlSessionFactoryBean=new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:/mapper/person/*.xml"));
return sqlSessionFactoryBean.getObject();
}
}
说明:可不显示配置事务管理器,导入的jar包会自动生成事务管理器,只需配置数据源即可
***********************
service 层
CustomService:自定义的service层接口
public interface CustomService {
void update(User user, Person person,Integer i);
}
***********************
serviceImpl 层
CustomServiceImpl:自定义的口接口实现类
@Service
public class CustomServiceImpl implements CustomService {
@Resource
private UserMapper userMapper;
@Resource
private PersonMapper personMapper;
@Override
@Transactional
public void update(User user,Person person,Integer i) {
user.setAge(6);
userMapper.updateById(user);
person.setAge(6);
personMapper.updateById(person);
if (i.equals(0)){
throw new RuntimeException("出错了");
};
}
}
***********************
controller 层
CustomController
@RestController
public class CustomController {
@Resource
private CustomService customService;
@Resource
private UserMapper userMapper;
@Resource
private PersonMapper personMapper;
@RequestMapping("/update")
public String hello(@RequestParam("id") Integer id,int num){
System.out.println("更新前的数据为:");
User user=userMapper.selectById(id);
Person person=personMapper.selectById(id);
System.out.println(user);
System.out.println(person);
try{
customService.update(user,person,num);
}catch (Exception e){
System.out.println(e.getMessage());
}
System.out.println("更新后的数据为:");
System.out.println(userMapper.selectById(id));
System.out.println(personMapper.selectById(id));
return "success";
}
}
*************************************
控制台测试输出
事务异常回滚
User(id=6, name=瓜田李下 0, age=10)
Person(id=6, name=海贼王 5, age=10)
出错了
更新后的数据为:
User(id=6, name=瓜田李下 0, age=10)
Person(id=6, name=海贼王 5, age=10)
事务操作异常事务回滚,数据没有修改
事务正常执行
更新前的数据为:
User(id=6, name=瓜田李下 0, age=10)
Person(id=6, name=海贼王 5, age=10)
更新后的数据为:
User(id=6, name=瓜田李下 0, age=6)
Person(id=6, name=海贼王 5, age=6)
事务正常执行,数据修改成功