Hello community! I’d like to discuss how to use autoCommit option in camunda engine.
Short intro.
I,m using camunda-engine 7.16 with spring-boot-2.6.3 running on openjdk-15 and postgres-13 as storage.
As all you know, by default spring-boot uses HikariPool. There is an autoCommit option in this pool.
autoCommit
This property controls the default auto-commit behavior of connections returned from the pool. It is a boolean value. Default: true.
Spring boot uses same behaviour by default spring.datasource.hikari.auto-commit
.
From the other hand, camunda-engine implementation is based on Command-pattern. There are various Commands, CommandContexts and CommandExecutors.
If we take a look into org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl
we can see special CommandInterceptors for transaction handling. In case using spring and spring boot, org.camunda.bpm.engine.spring.SpringTransactionInterceptor
implementation will be used.
Under the hood, SpringTransactionInterceptor interacts with spring TransactionManager and TransactionTemplate. In case using spring and spring boot, org.springframework.jdbc.datasource.DataSourceTransactionManager
implementation will be used.
The problem
DataSourceTransactionManager can detect how autoCommit is configured. In case autoCommit is true, txManager sets its value false explicitly at the begin of transaction and change it back at the end.
See org.springframework.jdbc.datasource.DataSourceTransactionManager#doBegin
and org.springframework.jdbc.datasource.DataSourceTransactionManager#doCleanupAfterCompletion
for details.
Also there is important comment in doBegin
method
// Switch to manual commit if necessary. This is very expensive in some JDBC drivers,
// so we don't want to do it unnecessarily (for example if we've explicitly
// configured the connection pool to set it already).
if (con.getAutoCommit()) {
txObject.setMustRestoreAutoCommit(true);
if (logger.isDebugEnabled()) {
logger.debug("Switching JDBC Connection [" + con + "] to manual commit");
}
con.setAutoCommit(false);
}
The question
I’d like to know should I use autoCommit=true
or autoCommit=false
with camunda-engine. Are there any best practices for this?
Respected community, can you please explain your point of view on the question how to use autoCommit.
Thanks!