cancel
Showing results for 
Search instead for 
Did you mean: 

How transactions work in Activiti

birju
Champ in-the-making
Champ in-the-making
looking at someone legacy code.

If transaction is configured into a config file

      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
      </bean>

And one of the APIs has following code

def insertUser(user:User) = {
  val user = identityService.newUser(user.id.toString)
  identityService.saveUser(user)

}


identityService.newUser and identityService.saveUser both will run into separate transactions right? This is a code bug if that's true. If so what's the best way to combine multiple acticiti calls into a single transaction?
3 REPLIES 3

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi,

it depends on transaction propagation level set in CommandConfig. e.g. for spring


  @Override
  protected void initDefaultCommandConfig() {
    if (defaultCommandConfig == null) {
      defaultCommandConfig = new CommandConfig().setContextReusePossible(true);
    }
  }

By default TransactionPropagation.REQUIRED

More info:
http://www.activiti.org/userguide/#N1094F

Regards
Martin

birju
Champ in-the-making
Champ in-the-making
Since it's using default which like you said is TransactionPropogation.REQUIRED still don't following two calls run into separate transactions?

<code>
def insertUser(user:User) = {
  val user = identityService.newUser(user.id.toString)
  identityService.saveUser(user)

}
</code>
The way I understand Propogation.REQUIRED is that if let's say identityService.newUser was calling another transactional method within itself, then it'll use the same transaction.

particular usage of calling two separate methods in above code has no knowledge about combining the two into one transaction given they are distinct calls? Isn't that right?

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi,


    try {
      User secondUser = identityService.newUser("testuser2");
      if (true)
        throw new RuntimeException();
      identityService.saveUser(secondUser);
      fail("Exception should have been thrown");
    } catch (RuntimeException re) {
    }

    assertEquals(0, identityService.createUserQuery().userId("testuser2").count());

identityService.newUser("testuser2"); creates user object with Id set.
identityService.saveUser(secondUser); persists object into the DB.

Regards
Martin