Saving JPA entity not working in JavaDelegate

We have a problem saving an JPA entity within the execute method of a Camunda JavaDelegate. The entity is not persisted to the database.
When calling the same code from a normal Spring @RestController, it works fine.

Short excerpt from our code:

@Component
@Transactional  // do we need this...?
public class CreateUser implements JavaDelegate {

   @Autowired private UserRepository userRepository;

   @Override public void execute(DelegateExecution camunda) {     
      User user = new User(...);
      userRepository.save(user); // The user is _not_ saved in the database
   }
}

We compared the detailled log files when calling from JavaDelegate and from the Spring @RestController. They were identical, except the following lines just directly after userRepository.save which are missing when calling from JavaDelegate:

2019-04-30 12:17:55.759 DEBUG 9092 --- [nio-8080-exec-1] o.h.e.t.internal.TransactionImpl         : On TransactionImpl creation, JpaCompliance#isJpaTransactionComplianceEnabled == false
2019-04-30 12:17:55.759 DEBUG 9092 --- [nio-8080-exec-1] o.h.e.t.internal.TransactionImpl         : begin
2019-04-30 12:17:56.019 TRACE 9092 --- [nio-8080-exec-1] j.i.AbstractLogicalConnectionImplementor : Preparing to begin transaction via JDBC Connection.setAutoCommit(false)
2019-04-30 12:17:56.040 TRACE 9092 --- [nio-8080-exec-1] j.i.AbstractLogicalConnectionImplementor : Transaction begun via JDBC Connection.setAutoCommit(false)
2019-04-30 12:17:56.040 TRACE 9092 --- [nio-8080-exec-1] cResourceLocalTransactionCoordinatorImpl : ResourceLocalTransactionCoordinatorImpl#afterBeginCallback

It seems as if no transaction is opened when calling from JavaDelegate.
Any idea?

We could solve the problem very easily.

We had two configurations for the datasource, an application.yaml and also a @Config Spring class (including a transaction manager), see the code below. As soon as we deleted the @Config, everything was working.

Here is the @Config we now deleted (shortened):

@Configuration
public class Config {

  @Bean public PlatformTransactionManager transactionManager() {
    return new DataSourceTransactionManager(dataSource());
  }

  @Bean public DataSource dataSource() {
    // ... return a new SimpleDriverDataSource
  }

}

And here is the application.yaml that we kept (shortened):

camunda.bpm:
  database:
    type: mysql

spring.jpa:
  properties.hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
  hibernate.ddl-auto: update 

spring.datasource:
  url: jdbc:mysql://xxx/xxx
  username: xxx
  password: xxx
  driver-class-name: com.mysql.jdbc.Driver
2 Likes