LCN分布式事务管理
官方地址: http://www.txlcn.org/
下载工具
首先,你可以参考官网,官网有更详细的讲解:https://github.com/codingapi/tx-lcn
如果想使用整理好的包,可以从这下载, 按照配置说明配置便可. https://download.csdn.net/download/coder_arley/10710659
配置解析
参考官网配置,这里做出解释:
在LcnConfg.java中,我们添加了如下注解:
@Configuration
@EnableTransactionManagement(proxyTargetClass=true)
@ComponentScan(basePackages={"com.codingapi.tx.*","com.iking.provider.*"})
@ImportResource(locations = {"classpath*:dubbo_provider.xml"})
public class LcnConfig implements TxManagerTxUrlService{
@Autowired
private Environment env;
/**
* 获取代理连接池
* @return
*/
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
return dataSource;
}
/**
* 注入LCN的代理连接池
* @return
*/
@Bean("transactionManager")
public PlatformTransactionManager txManager(){
return new DataSourceTransactionManager(dataSource());
}
@Override
public String getTxUrl() {
return env.getProperty("tx.manager.url");
}
}
1)首先呢,我们将此类标注为全局配置类,使用@Configuration
2)我们使用@EnableTransactionManagement(proxyTargetClass=true)开启事务远程代理,并进行连接池和LCN代理的注入:
@Autowired
private Environment env;
/**
* 获取代理连接池
* @return
*/
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
return dataSource;
}
/**
* 注入LCN的代理连接池
* @return
*/
@Bean("transactionManager")
public PlatformTransactionManager txManager(){
return new DataSourceTransactionManager(dataSource());
}
这个注解等同于官网上xml中的下列代码:
<!--lcn代理连接池配置-->
<bean name="lcnDataSourceProxy" class="com.lorne.tx.db.LCNDataSourceProxy">
<property name="dataSource" ref="dataSource"/>
<!-- 分布式事务参与的最大连接数,确保不要超过普通连接池的最大值即可 -->
<property name="maxCount" value="20"/>
</bean>
<!--jdbcTemplate -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="lcnDataSourceProxy"/>
</property>
</bean>
<!--jdbc事务配置 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="lcnDataSourceProxy" />
</bean>
还有
<tx:annotation-driven/>
<!-- Aspect -->
<aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true"/>
使用说明
分布式事务发起方:
@Override
@TxTransaction(isStart=true)
@Transactional
public boolean hello() {
//本地调用
testDao.save();
//远程调用方
boolean res = test2Service.test();
//模拟异常
int v = 100/0;
return true;
}
分布式事务被调用方(test2Service的业务实现类)
@Override
@Transactional
@TxTransaction
public boolean test() {
//本地调用
testDao.save();
return true;
}
说明:在使用LCN分布式事务时,只需要将事务的开始方法添加@TxTransaction(isStart=true)
注解即可,在参与方添加@TxTransaction
或者实现ITxTransaction
接口即可。详细见demo教程
关于@TxTransaction 使用说明
@TxTransaction注解是分布式事务的标示。
若存在业务方法:a->b b->c b->d,那么开启分布式事务注解的话,需要在各个模块方法上添加@TxTransaction即可。
@TxTransaction(isStart=true)
@Transactional
public void a(){
b();
}
@TxTransaction
public void b(){
c();
d();
}
@TxTransaction
public void c(){}
@TxTransaction
public void d(){}