使用activiti中有个很重要的问题就是需要保证事物的控制
activiti使用的是mybatis作为orm技术 封装了一系列的操作数据库操作 这也就是大家调用的api 操作的数据库表都是activit自带的数据表 一般情况下使用spring整合
activiti的事物是很强大的 但是往往就不能如愿 因为公司使用的都是自己的框架不能利用spring来整合事物。
通过java程序来控制activiti的事物
activiti的事物依赖于TransactionFactory 在创建流程引擎ProcessEngine来指定事物工厂,其原因分析如下:
创建流程引擎: ProcessEngine processEngine = conf.buildProcessEngine() ;
conf为StandaloneProcessEngineConfiguration的对象其buildProcessEngine方法如下:
1
2
3
4
5
|
public
ProcessEngine buildProcessEngine() {
init();
return
new
ProcessEngineImpl( this );
}
<br> //调用init方法 <br> <br> protected void init() {<br> initHistoryLevel();<br> initExpressionManager();<br> initVariableTypes();<br> initBeans();<br> initFormEngines();<br> initFormTypes();<br> initScriptingEngines();<br> initBusinessCalendarManager();<br> initCommandContextFactory();<br> initTransactionContextFactory();<br> initCommandExecutors();<br> initServices();<br> initIdGenerator();<br> initDeployers();<br> initJobExecutor();<br> initMailScanner();<br> initDataSource();<br> <span style="color: rgb(255, 0, 0);"> initTransactionFactory();</span><br> initSqlSessionFactory();<br> initSessionFactories();<br> initJpa();<br> initDelegateInterceptor();<br> initEventHandlers();<br> }<br>其中初始化了很多信息,可以依次看看源码 其中initTransactionFactory()方法代码如下: <br><br> // myBatis SqlSessionFactory ////////////////////////////////////////////////<br> <br> protected void initTransactionFactory() {<br> if (transactionFactory==null) {<br> if (transactionsExternallyManaged) {<br> transactionFactory = new ManagedTransactionFactory();<br> } else {<br> transactionFactory = new JdbcTransactionFactory();<br> }<br> }<br> }<br>
|
其中transactionFactory为 protected TransactionFactory
transactionFactory; 类中的属性
由代码可知
如果transactionFactory不为空则事物工厂为JdbcTransactionFactory的实例化对象
那么设置StandaloneProcessEngineConfiguration的transactionFactory的值为一JdbcTransactionFactory对象即可
代码如下:创建一个servlet监听器 在容器启动的时候创建一个流程引擎 (需要配置web.xml文件)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
/********************************************************************** * <pre>
* FILE : MyServletListener.java
* CLASS : MyServletListener
*
* AUTHOR : Liaokailin
*
* FUNCTION :
*
*
* Contact: <a href="mailto:Sina284888279@qq.com">Sina L凯林</a><br> *
*======================================================================
* CHANGE HISTORY LOG
*----------------------------------------------------------------------
* MOD. NO.| DATE | NAME | REASON | CHANGE REQ.
*----------------------------------------------------------------------
* |2014年1月17日|Liaokailin| Created |
* DESCRIPTION:
* </pre>
***********************************************************************/
package
com.infodms.dms.actions;
import
javax.servlet.ServletContextEvent;
import
javax.servlet.ServletContextListener;
import
org.activiti.engine.ProcessEngine;
import
org.activiti.engine.ProcessEngineConfiguration;
import
org.activiti.engine.ProcessEngines;
import
org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import
com.infodms.dms.db.DBManager;
import
com.infodms.dms.utils.DmsActivitiTransactionFactory;
public
class
MyServletListener implements
ServletContextListener {
@Override
public
void
contextDestroyed(ServletContextEvent arg0) {
ProcessEngines.destroy();
}
@Override
public
void
contextInitialized(ServletContextEvent arg0) {
StandaloneProcessEngineConfiguration conf = (StandaloneProcessEngineConfiguration) ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
conf.setDataSource(DBManager.getDataSource()).setDatabaseSchemaUpdate( "true" ).setDbHistoryUsed( true ) ;
<span style= "color: rgb(255, 0, 0);" > conf.setTransactionFactory( new
DmsActivitiTransactionFactory()) ;
</span> ProcessEngine processEngine = conf.buildProcessEngine() ;
}
} |
conf.setTransactionFactory(new DmsActivitiTransactionFactory()) ;设置事物
DmsActivitiTransactionFactory代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
/********************************************************************** * <pre>
* FILE : DmsActivitiTransactionFactory.java
* CLASS : DmsActivitiTransactionFactory
*
* AUTHOR : Liaokailin
*
* FUNCTION :
*
*
* Contact: <a href="mailto:Sina284888279@qq.com">Sina L凯林</a> <br> *
*======================================================================
* CHANGE HISTORY LOG
*----------------------------------------------------------------------
* MOD. NO.| DATE | NAME | REASON | CHANGE REQ.
*----------------------------------------------------------------------
* |2014年1月21日|Liaokailin| Created |
* DESCRIPTION:
* </pre>
***********************************************************************/
package
com.infodms.dms.utils;
import
java.sql.Connection;
import
java.util.Properties;
import
org.apache.ibatis.transaction.Transaction;
import
org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import
com.infoservice.po3.core.context.DBService;
public
class
DmsActivitiTransactionFactory extends
JdbcTransactionFactory {
@Override
public
Transaction newTransaction(Connection conn, boolean
autoCommit) {
System.out.println( "开启新的事物" );
return
new
DmsActitiviTransaction(conn,autoCommit) ;
//return (Transaction) DBService.getInstance().getTransaction() ;
}
@Override
public
void
setProperties(Properties props) {
super .setProperties(props);
}
} |
DmsActitiviTransaction 代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
/********************************************************************** * <pre>
* FILE : DmsActitiviTransaction.java
* CLASS : DmsActitiviTransaction
*
* AUTHOR : Liaokailin
*
* FUNCTION :
*
*
* Contact: <a href="mailto:Sina284888279@qq.com">Sina L凯林</a>
*
*======================================================================
* CHANGE HISTORY LOG
*----------------------------------------------------------------------
* MOD. NO.| DATE | NAME | REASON | CHANGE REQ.
*----------------------------------------------------------------------
* |2014年1月21日|Liaokailin| Created |
* DESCRIPTION:
* </pre>
***********************************************************************/
package
com.infodms.dms.utils;
import
java.sql.Connection;
import
java.sql.SQLException;
import
org.apache.ibatis.transaction.jdbc.JdbcTransaction;
import
com.infodms.dms.db.DBManager;
import
com.infoservice.po3.core.context.DBService;
public
class
DmsActitiviTransaction extends
JdbcTransaction {
private
static
Connection conn = null
;
public
DmsActitiviTransaction(Connection connection,
boolean
desiredAutoCommit) {
//connection.setAutoCommit(desiredAutoCommit);
super (connection, desiredAutoCommit);
try
{
connection.setAutoCommit(desiredAutoCommit);
} catch
(SQLException e) {
e.printStackTrace();
}
}
@Override
public
Connection getConnection() {
/*DBService d = DBService.getInstance() ;
d.loadConf("/DataAccessContext.xml");
return d.getConnection() ;*/
// return DBService.getInstance().getConnection() ;
try
{
if (conn== null ){
synchronized
(DmsActitiviTransaction. class ) {
conn = DBManager.getConnection() ;
}
}
return
conn ;
} catch
(Exception e) {
System.out.println( "获取链接失败:" +e);
return
null
;
}
}
@Override
public
void
close() throws
SQLException {
/* if(conn!=null&&!conn.isClosed()){
conn.close();
}*/
System.out.println( "activit:close()" );
}
@Override
public
void
commit() throws
SQLException {
/*if(conn!=null&&!conn.isClosed()){
conn.commit();
}*/
System.out.println( "activit:commit()" );
}
@Override
protected
void
resetAutoCommit() {
System.out.println( "resetAutoCommit()" );
/*try {
if (conn != null && !conn.isClosed()) {
conn.setAutoCommit(false);
}
} catch (SQLException e) {
e.printStackTrace();
}*/
}
@Override
public
void
rollback() throws
SQLException {
/*if(conn!=null&&!conn.isClosed()){
conn.rollback();
}*/
System.out.println( "activit:rollback()" );
}
@Override
protected
void
setDesiredAutoCommit( boolean
arg0) {
System.out.println( "setDesiredAutoCommit:" +arg0);
}
} |
其中DBManager
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
/********************************************************************** * <pre>
* FILE : DBManager.java
* CLASS : DBManager
*
* AUTHOR : Liaokailin
*
* FUNCTION :
*
*
* Contact: <a href="mailto:Sina284888279@qq.com">Sina L凯林</a>
*
*======================================================================
* CHANGE HISTORY LOG
*----------------------------------------------------------------------
* MOD. NO.| DATE | NAME | REASON | CHANGE REQ.
*----------------------------------------------------------------------
* |2014年1月22日|Liaokailin| Created |
* DESCRIPTION:
* </pre>
***********************************************************************/
package
com.infodms.dms.db;
import
java.sql.Connection;
import
java.sql.SQLException;
import
javax.sql.DataSource;
import
org.springframework.beans.factory.BeanFactory;
import
org.springframework.beans.factory.xml.XmlBeanFactory;
import
org.springframework.core.io.ClassPathResource;
import
org.springframework.core.io.Resource;
import
com.infoservice.po3.core.context.DBInfo;
public
class
DBManager {
public
static
DataSource dataSource = null
;
public
static
Connection conn = null
;
public
static
DataSource getDataSource(){
if (dataSource== null ){
synchronized
(DBManager. class ) {
Resource resource = new
ClassPathResource( "/DataAccessContext.xml" );
BeanFactory springBeanFactory = new
XmlBeanFactory(resource);
DBInfo dbInfo = ((DBInfo)springBeanFactory.getBean( "DBInfo" ));
dataSource = (DataSource)springBeanFactory.getBean(dbInfo.getDefDataSource());
System.out.println( "获取数据源" );
}
} else {
System.out.println( "默认数据源调用" );
}
return
dataSource ;
}
public
static
Connection getConnection() throws
SQLException{
if (conn!= null &&!conn.isClosed()){
System.out.println( "获取默认连接" );
} else {
synchronized
(DBManager. class ) {
System.out.println( "获取连接" );
conn = getDataSource().getConnection() ;
}
}
return
conn ;
}
public
static
void
startTransaction(Connection connection) throws
SQLException {
System.out.println( "开启事物" );
connection.setAutoCommit( false );
}
public
static
void
endTransaction(Connection connection) throws
SQLException{
System.out.println( "提交事物 " );
connection.commit();
connection.setAutoCommit( true );
}
public
static
void
rollBack(Connection connection) throws
SQLException{
System.out.println( "回滚事物" );
connection.rollback();
}
} |
调用的使用DBManager就ok了