jfinal mysql 事务_jfinal集成多数据库事务框架测试

packagecom.csnt.source.plugin.atomikos;

importcom.atomikos.icatch.jta.UserTransactionImp;

importcom.atomikos.jdbc.AtomikosDataSourceBean;

importcom.jfinal.kit.LogKit;

importcom.jfinal.kit.StrKit;

importcom.jfinal.plugin.IPlugin;

importcom.jfinal.plugin.activerecord.ActiveRecordException;

importcom.jfinal.plugin.activerecord.IAtom;

importcom.jfinal.plugin.activerecord.IDataSourceProvider;

importjavax.sql.DataSource;

importjava.util.Properties;/**

* @authorsource

*/public classAtomikosPluginimplementsIPlugin,IDataSourceProvider {privateStringname=null;

privateStringurl;

privateStringusername;

privateStringpassword;

privateStringxaDataSourceStr;

private intminPoolSize=10;

private intmaxPoolSize=32;

privateAtomikosDataSourceBeands;

private booleanlocalTransactionMode=true;

private volatile booleanisStarted=false;

publicAtomikosPlugin(String url,String username,String password,String xaDataSourceStr) {this.url= url;

this.username= username;

this.password= password;

this.xaDataSourceStr= xaDataSourceStr;}publicAtomikosPluginset(intminPoolSize, intmaxPoolSize) {this.minPoolSize= minPoolSize;

this.maxPoolSize= maxPoolSize;

return this;}public finalAtomikosPluginsetLocalTransactionMode(booleanlocalTransactionMode) {this.localTransactionMode= localTransactionMode;

return this;}public finalStringgetName() {return this.name;}public final voidsetName(String name) {this.name= name;}public static booleantx(IAtom atom) {

UserTransactionImp utx =newUserTransactionImp();

try{

utx.begin();

booleanresult = atom.run();

if(result) {//提交事务utx.commit();}else{//回滚事务utx.rollback();}returnresult;}catch(Throwable t) {try{

utx.rollback();}catch(Exception e) {

LogKit.error(e.getMessage(),e);}throwtinstanceofRuntimeException ? (RuntimeException) t :newActiveRecordException(t);}

}@Overridepublic booleanstart() {if(isStarted) {return true;}else{ds=newAtomikosDataSourceBean();

if(StrKit.notBlank(name)) {ds.setUniqueResourceName(name);}ds.setMinPoolSize(minPoolSize);ds.setMaxPoolSize(maxPoolSize);//是否开启本地事务与jta事务混合ds.setLocalTransactionMode(localTransactionMode);ds.setXaDataSourceClassName(xaDataSourceStr);Properties properties =newProperties();properties.put("URL",url);properties.put("user",username);properties.put("password",password);ds.setXaProperties(properties);

this.isStarted=true;

return true;}

}@Overridepublic booleanstop() {if(ds!=null) {ds.close();}ds=null;isStarted=false;

return true;}@OverridepublicDataSourcegetDataSource() {returnds;}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值