`
scnu_lts
  • 浏览: 19224 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Activiti with Hibernate

阅读更多

Activiti with Hibernate

 

 

http://forums.activiti.org/en/viewtopic.php?f=6&t=4360

I have solved the problem for our needs.

Now Activiti uses the HibernateTransactionManager and with 
that we can make a full rollback! 
Its not important that Activiti uses MyBatis, 
because after a small research about MyBatis I found out that 
MyBatis support the HibernateTransactionManager.

ApplicationCotext:
Code:
<!-- DataSource Definition -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/activiti"/>
<property name="username" value="root" />
<property name="password" value="root" />
<property name="poolPreparedStatements" value="true" />
<property name="maxActive" value="50" />
<property name="maxIdle" value="10" />
</bean>

<!-- Session Factory Definition -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
...
</bean>


<!-- Hibernate Transaction Manager Definition -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>

<!-- Process Engine Definition -->
<bean id="processEngineConfiguration" 
class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="true" />
<property name="jobExecutorActivate" value="false" />
</bean>


Methods for start and commit a transation
 (all Transactions done after startTransaction are commit after call commit Transaction):
Code:
protected TransactionStatus startTransaction() {
PlatformTransactionManager manager = getTransactionManager();
DefaultTransactionDefinition d = new DefaultTransactionDefinition();
d.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW);

transaction = manager.getTransaction(d);

return transaction;
}

protected void commit(TransactionStatus transaction) {
PlatformTransactionManager manager = getTransactionManager();
manager.commit(transaction);
}

protected void rollback(TransactionStatus transaction) {
PlatformTransactionManager manager = getTransactionManager();
manager.rollback(transaction);
}

private PlatformTransactionManager getTransactionManager() {

return (PlatformTransactionManager) this.appContext
.getBean("transactionManager");
}


I hope this helps anyone who have a problem 
to integrate activiti in a hibernate environment.

P.S.: I think it would be great if this method (or something like that) 
is also found in the activiti doucumentation, 
because lots of projects currently using hibernate for persitences and in the activit
 documentation doesn't exist a guide to integrate activiti in an hibernate environment.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics