cancel
Showing results for 
Search instead for 
Did you mean: 

Service Task transaction with database

fernando-sc
Champ in-the-making
Champ in-the-making
Hi,

I'm using a serviceTask to make some rules in my process.
The problem is that I'm trying to do a query on my database (hibernate), I realized that when my serviceTask starts, the activiti opens a transaction and only closes when it is finalized.
I wanted to enjoy the transaction already opened to make the query.

It is possible to do this?

Thanks Smiley Wink
11 REPLIES 11

jbarrez
Star Contributor
Star Contributor
How do you manage the connection and datasource? If you use Spring for example, it is possible to have hibernate and activiti go against the same database, using the same transaction manager, which would answer your question.

fernando-sc
Champ in-the-making
Champ in-the-making
I'm using Spring in this way:
<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>

<task:annotation-driven/>

  <bean id="dataSource"
    class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="driverClass" value="${database.driver}" />
      <property name="jdbcUrl" value="${database.url}" />
      <property name="user" value="${database.username}" />
      <property name="password" value="${database.password}" />
      <property name="initialPoolSize" value="3" />
      <property name="minPoolSize" value="1" />
      <property name="maxPoolSize" value="17" />
  </bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="dataSource" ref="dataSource" />
    <property name="sessionFactory" ref="sessionFactory" />
  </bean>

<bean
    class="org.springframework.transaction.aspectj.AnnotationTransactionAspect"
    factory-method="aspectOf">
    <property name="transactionManager">
      <ref local="transactionManager" />
    </property>
  </bean>

<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
    <property name="databaseType" value="postgres" />
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="history" value="full" />
   
    <property name="databaseSchemaUpdate" value="true" />
    <property name="jobExecutorActivate" value="true" />

Is something wrong in my configuration?

Thank you!

jbarrez
Star Contributor
Star Contributor
That looks as it should.

How do you do the query in the servicetask? How do you inject the hibernate session in the service task?

fernando-sc
Champ in-the-making
Champ in-the-making
This is my serviceTask:
public class TestServiceTask implements JavaDelegate{

private VersionDAO versionDAO;

public TestServiceTask(){
  versionDAO = new BeanAccessor().getBean(VersionDAO.class);
}

public void execute(DelegateExecution execution) throws Exception {
  Version version = versionDAO.loadDistributionVersion("Test");
    }
}
My DAO:
@Transactional
public Version loadDistributionVersion(String productName) {
  return (Version) getCurrentSession().createQuery("" +
    "from Version as v " +
    "where v.product.name = Smiley TongueroductName " +
    "and v.product.distributionVersion.id = v.id")
    .setParameter("productName", productName)
    .uniqueResult();
}
And my getCurrentSession() which is in GenericDAO:
public GenericDAO{
   @Autowired
private SessionFactory sessionFactory;


    protected Session getSession() {
  return sessionFactory.getCurrentSession();
}
}
I can't use the @Autowired within ServiceTask, so I use the class BeanAccessor:
@Configurable
public class BeanAccessor {

  @Autowired
  private WebApplicationContext context;

  public <T> T getBean(Class<T> beanClass) {
    return context.getBean(beanClass);
  }
 
  public <T> Map<String, T> getBeansOfType(Class<T> beanClass) {
    return context.getBeansOfType(beanClass);
  }
}

Thanks!

jbarrez
Star Contributor
Star Contributor
Damn, I was hoping to find something wrong in that setup … but I don't see anything unusual.

So let me get this straight: you are actually seeing two separate transactions going on? If so, could you please file a jira report with some simple test case, cause that stuff should just work.

fernando-sc
Champ in-the-making
Champ in-the-making
Hi jbarrez,

I'm using the changes you've made to the branch, where you fixed the problem(https://jira.codehaus.org/browse/ACT-1386 and http://forums.activiti.org/en/viewtopic.php?f=4&t=4646).

But, I'm still having the same problem using the configuration above.
I have to change the way they work with Spring after these changes?

Thanks.

jbarrez
Star Contributor
Star Contributor
Well, the issue in the test case you attached on the jira is that you recreate every time the application context, effectively creating a new transaction manager. You need to inject the services in the bean instead, such that only one applicationContext is used.

fernando-sc
Champ in-the-making
Champ in-the-making
Added a new example, now working correctly with the Spring.

https://jira.codehaus.org/browse/ACT-1386

Thank you!

jbarrez
Star Contributor
Star Contributor
Ok, great to hear it works now! Thanks for following up and attaching your project.