NutzCN Logo
精华 activiti流程部署失败后,数据回滚失败
发布于 2497天前 作者 zhangkaidsy 4900 次浏览 复制 上一个帖子 下一个帖子
标签: fireflow

最近在项目中使用activiti工作流,但是发现在自己人为制造一个错误后,nutz捕捉到了异常,启动了事务回滚,但是activiti表内数据却没有回滚成功,怀疑是activiti没有成功注入nutz事务,请问一下大佬有遇到过类似问题吗?
Trans.exec(new Atom() {
@Override
public void run() {
try {
//获取bpmn文件的输入流
InputStream inputStream = tf.getInputStream();
//开始部署工作流
System.out.println(tf.getSubmittedFileName());
Deployment deployment = processEngine.getRepositoryService().createDeployment().name(expendClassifyName+"费用申请流程")
.addInputStream(tf.getSubmittedFileName(), inputStream).deploy();
System.out.println(1/0);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});

23 回复

以前有人做过, 某个帖子里面,得找找了, 好像是从mybatis 的事务入手的

@wendal 请问一下具体是怎么做的?研究了一天,没什么进展呀,兽总

找找那个帖子才行

@wendal 搞定了。谢谢我兽总

@zhangkaidsy 怎样搞定的?分享一下?

@wendal 还是有问题的,nutz的数据库操作和activiti的数据库操作不是一个连接,回滚还是失败

@wendal 兽总,你那还有没有其他的资料说到过这个问题的呀?

核心应该是TransactionFactory接口,然后Transaction接口需要返回nutz正在使用的连接,如果存在的话

@wendal 兽总,那如何获取nutz正在使用的连接?

Trans类有方法

@wendal 问题已经解决了,具体的解决步骤需要私发给你吗?兽总

额,涉及机密?还是需要截图。。。 直接发这里就好了吧?

@wendal 我新写了两个类,类的代码直接贴到帖子里也行

噢,贴吧, 如果能搞个pull request就更完美了^_^

CustomJdbcTransaction.java类代码:

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

import javax.sql.DataSource;

import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.apache.ibatis.transaction.jdbc.JdbcTransaction;
import org.nutz.trans.Trans;

import com.sys.util.nutz.NutzUtil;

public class CustomJdbcTransaction extends JdbcTransaction{

private static final Log log = LogFactory.getLog(CustomJdbcTransaction.class);

public CustomJdbcTransaction(Connection connection) {
    super(connection);
}

public CustomJdbcTransaction(DataSource ds,
        TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) {
    super(ds, desiredLevel, desiredAutoCommit);
}

@Override
protected void openConnection() throws SQLException {

// super.openConnection();
//connection = NutzUtil.getConnection();
connection = Trans.getConnectionAuto(NutzUtil.getDataSource());
if (log.isDebugEnabled()) {
log.debug("{CustomJdbcTransaction } Openning JDBC Connection"+connection.hashCode()+"[]"+autoCommmit);
}
// connection = dataSource.getConnection();
if (level != null) {
connection.setTransactionIsolation(level.getLevel());
}
setDesiredAutoCommit(autoCommmit);
}

@Override
public void close() throws SQLException {
    if (log.isDebugEnabled()) {
          log.debug("{CustomJdbcTransaction } closing JDBC Connection"+connection.hashCode());
        }

// super.close();
}

@Override
public void commit() throws SQLException {
    // TODO Auto-generated method stub

// super.commit();
}

@Override
public void rollback() throws SQLException {
    // TODO Auto-generated method stub

// super.rollback();
}
}

CustomJdbcTransactionFactory.java类代码:

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.jdbc.JdbcTransactionFactory;

public class CustomJdbcTransactionFactory extends JdbcTransactionFactory {
@Override
public void setProperties(Properties props) {
super.setProperties(props);
}

@Override
public Transaction newTransaction(Connection conn) {

// return super.newTransaction(conn);
return new CustomJdbcTransaction(conn);
}

@Override
public Transaction newTransaction(DataSource ds,
        TransactionIsolationLevel level, boolean autoCommit) {

// return super.newTransaction(ds, level, autoCommit);
return new CustomJdbcTransaction(ds, level, autoCommit);
}

}

然后在activiti-engine.js配置文件内添加如下配置:
jdbcTransactionFactory : {
type : "com.sys.activiti.transaction.CustomJdbcTransactionFactory"
}

最后在processEngineSpec 中引用jdbcTransactionFactory 就大功告成了
processEngineSpec : {
type : "com.sys.org.nutz.integration.activiti.ActivitiProcessEngineConfiguration",
factory: "$conf#make",
args : ["com.sys.org.nutz.integration.activiti.ActivitiProcessEngineConfiguration", "activiti."],
fields : {
dataSource : {refer:"dataSource"},
expressionManager : {refer:"nutzExpressionManager"},
transactionFactory : {refer:"jdbcTransactionFactory"}
}
}

@wendal 就是以上这些内容

额,你没按 插入代码 。。

CustomJdbcTransaction.java类代码:

package com.sys.activiti.transaction;

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

import javax.sql.DataSource;

import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.apache.ibatis.transaction.jdbc.JdbcTransaction;
import org.nutz.trans.Trans;

import com.sys.util.nutz.NutzUtil;

public class CustomJdbcTransaction extends JdbcTransaction{
	
	private static final Log log = LogFactory.getLog(CustomJdbcTransaction.class);
	
	public CustomJdbcTransaction(Connection connection) {
		super(connection);
	}

	public CustomJdbcTransaction(DataSource ds,
            TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) {
        super(ds, desiredLevel, desiredAutoCommit);
    }

    @Override
    protected void openConnection() throws SQLException {
//      super.openConnection();
        //connection = NutzUtil.getConnection();
    	connection = Trans.getConnectionAuto(NutzUtil.getDataSource());
        if (log.isDebugEnabled()) {
          log.debug("{CustomJdbcTransaction } Openning JDBC Connection"+connection.hashCode()+"[]"+autoCommmit);
        }
//      connection = dataSource.getConnection();
        if (level != null) {
          connection.setTransactionIsolation(level.getLevel());
        }
        setDesiredAutoCommit(autoCommmit);
    }

    @Override
    public void close() throws SQLException {
        if (log.isDebugEnabled()) {
              log.debug("{CustomJdbcTransaction } closing JDBC Connection"+connection.hashCode());
            }
//      super.close();
    }

    @Override
    public void commit() throws SQLException {
        // TODO Auto-generated method stub
//      super.commit();
    }

    @Override
    public void rollback() throws SQLException {
        // TODO Auto-generated method stub
//      super.rollback();
    }
}

CustomJdbcTransactionFactory.java类代码:

package com.sys.activiti.transaction;

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.jdbc.JdbcTransactionFactory;

public class CustomJdbcTransactionFactory extends JdbcTransactionFactory {
	@Override
    public void setProperties(Properties props) {
        super.setProperties(props);
    }

    @Override
    public Transaction newTransaction(Connection conn) {
//      return super.newTransaction(conn);
        return new CustomJdbcTransaction(conn);
    }

    @Override
    public Transaction newTransaction(DataSource ds,
            TransactionIsolationLevel level, boolean autoCommit) {
//      return super.newTransaction(ds, level, autoCommit);
        return new CustomJdbcTransaction(ds, level, autoCommit);
    }

}

然后在activiti-engine.js配置文件内添加如下配置:

jdbcTransactionFactory : {
			type : "com.sys.activiti.transaction.CustomJdbcTransactionFactory"
		}

最后在processEngineSpec 中引用jdbcTransactionFactory 就大功告成了

processEngineSpec : {
			type : "com.sys.org.nutz.integration.activiti.ActivitiProcessEngineConfiguration",
			factory: "$conf#make",
			args : ["com.sys.org.nutz.integration.activiti.ActivitiProcessEngineConfiguration", "activiti."],
			fields : {
				dataSource : {refer:"dataSource"},
				expressionManager : {refer:"nutzExpressionManager"},
				transactionFactory : {refer:"jdbcTransactionFactory"}
			}
		}

@wendal 失误,改正过来了

额,不需要关闭?

添加回复
请先登陆
回到顶部