Jfinal与Activiti整合事务方案
博客专区 > 超龍 的博客 > 博客详情
Jfinal与Activiti整合事务方案
超龍 发表于3个月前
Jfinal与Activiti整合事务方案
  • 发表于 3个月前
  • 阅读 121
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

activiti基于spring的事务集成,网上的资料很多。这里省略1000字。

但是,有的项目并没有使用spring,那怎么控制事务呢?

比如jfinal框架与Activiti整合。

先写一个插件:ActivitiPlugin

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration;

import com.jfinal.plugin.IPlugin;
import com.jfinal.plugin.activerecord.DbKit;

public class ActivitiPlugin implements IPlugin{

private static ProcessEngine processEngine = null;
private static ProcessEngineConfiguration processEngineConfiguration = null;
private boolean isStarted = false;
@Override
public boolean start(){
try {
createProcessEngine();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}

@Override
public boolean stop() {
ProcessEngines.destroy();
isStarted = false;
return true;
}

private Boolean createProcessEngine() throws Exception{
if (isStarted) {
return true;
}
StandaloneProcessEngineConfiguration conf = (StandaloneProcessEngineConfiguration) ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
conf.setDataSource(DbKit.getConfig().getDataSource());
conf.setEnableDatabaseEventLogging(false);
conf.setDatabaseSchemaUpdate(ProcessEngineConfigurationImpl.DB_SCHEMA_UPDATE_TRUE);//更新
// conf.setDatabaseSchemaUpdate(ProcessEngineConfigurationImpl.DB_SCHEMA_UPDATE_DROP_CREATE);//重置数据库!!!调试用!!!请勿打开!!!
conf.setDbHistoryUsed(true);
// conf.setTransactionsExternallyManaged(true); // 使用托管事务工厂
conf.setTransactionFactory(new ActivitiTransactionFactory());
UuidGenerator uuidG = new UuidGenerator();
conf.setIdGenerator(uuidG);
ActivitiPlugin.processEngine = conf.buildProcessEngine();
isStarted = true;
//开启流程引擎
System.out.println(“启动流程引擎…….”);
return isStarted;
}

// 开启流程服务引擎
public static ProcessEngine buildProcessEngine() {
if (processEngine == null)
if (processEngineConfiguration != null) {
processEngine = processEngineConfiguration.buildProcessEngine();
}
return processEngine;
}

ActivitiTransactionFactory:

import java.sql.Connection;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.session.TransactionIsolationLevel;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;

public class ActivitiTransactionFactory implements TransactionFactory {

@Override
public void setProperties(Properties props) {
}

@Override
public Transaction newTransaction(Connection conn) {
return new ActivitiTransaction(conn);
}

@Override
public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) {
return new ActivitiTransaction(ds, level, autoCommit);
}
}

UuidGenerator:

import java.util.UUID;

import org.activiti.engine.impl.cfg.IdGenerator;

public class UuidGenerator implements IdGenerator {

@Override
public String getNextId() {
return UUID.randomUUID().toString().replace(“-“, “”);
}

}

ActivitiTransactionFactory:

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.ibatis.session.TransactionIsolationLevel;
import org.apache.ibatis.transaction.Transaction;

import com.jfinal.plugin.activerecord.DbKit;
public class ActivitiTransaction implements Transaction {

protected Connection connection;
protected DataSource dataSource;
protected TransactionIsolationLevel level;
protected boolean autoCommmit;

public ActivitiTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) {
dataSource = ds;
level = desiredLevel;
autoCommmit = desiredAutoCommit;
}

public ActivitiTransaction(Connection connection) {
this.connection = connection;
}

/**
* 重写了openConnection()方法,获取数据库连接是我业务打开的那个连接
*/

@Override
public Connection getConnection() throws SQLException {
if (connection == null) {
openConnection();
}
return connection;
}

/**
* 连接关闭close(),提交commit(),回滚rollback() ,
* 全部注释了。对数据库连接的操作都有我业务来控制,不让activiti控制了
*/

@Override
public void commit() throws SQLException {
}

@Override
public void rollback() throws SQLException {
}

@Override
public void close() throws SQLException {
if(connection!=null){
DbKit.getConfig().close(connection);
}
}

protected void openConnection() throws SQLException {
connection = DbKit.getConfig().getConnection();
if (level != null) {
connection.setTransactionIsolation(level.getLevel());
}
}
}

Jfinal配置,在configPlugin方法里面写:

/**
* 配置插件
*/
public void configPlugin(Plugins me) {
//me.add(new EhCachePlugin()); //缓存
// 配置druid数据库连接池插件
DruidPlugin druidPlugin = createDruidPlugin();
druidPlugin.set(2, 2, 200);//设置最大连接数
// 配置Oracle驱动
druidPlugin.setDriverClass(“oracle.jdbc.driver.OracleDriver”);
druidPlugin.addFilter(new StatFilter());
// 指定连接验证语句(用于保存数据库连接池), 这里不加会报错误:invalid oracle validationQuery. select
// 1, may should be : select 1 FROM DUAL
druidPlugin.setValidationQuery(“select 1 FROM DUAL”);
WallFilter wall = new WallFilter();
// 设计数据库类型
wall.setDbType(JdbcConstants.ORACLE);
druidPlugin.addFilter(wall);
me.add(druidPlugin);

// 配置ActiveRecord插件
ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
// 配置属性名(字段名)大小写不敏感容器工厂
// arp.setContainerFactory(new CaseInsensitiveContainerFactory());
arp.setContainerFactory(new PropertyNameContainerFactory());
// 配置Oracle方言
arp.setDialect(new OracleDialect());
// arp.setTransactionLevel(8);
arp.setShowSql(true);
arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
arp.addSqlTemplate(“global.sql”);
// 所有映射在 MappingKit 中自动化搞定
//_MappingKit.mapping(arp);
me.add(arp);

// 配置工作流引擎
ActivitiPlugin ap = new ActivitiPlugin();
me.add(ap);
SqlReporter.setLog(true);
// 用于缓存session模块的redis服务
RedisPlugin sessionRedis = new RedisPlugin(“session”, PropKit.get(“sessionRedis”).trim());
me.add(sessionRedis);

}

上面我用的是druid连接池。

需要注意的是:me.add(arp);必须在me.add(ap);之前,否则会有空指针报错!

共有 人打赏支持
粉丝 0
博文 30
码字总数 8618
×
超龍
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: