Database connection is closed before transaction is committed

Hi,

I am using JdbcEventStorageEngine with my own implementation of ConnectionProvider and TransactionManager (implemented using SqlSessionManager from MyBatis, I am constrained not to used axon-spring)
and I am facing following situation when database connection is closed before transaction commit.
For example this snippet of code

transactionManager.executeInTransaction( () ->
executeBatch(
getConnection(),
connection -> { return some prepated statement },
e -> handlePersistenceException(e, event)
)
);

Will will to this:

  1. start transaction through transaction manager
  2. request database connection using connection provider
  3. invoke executeBatch method from JdbcUtils
    3.1. execute sql statement
    3.2. close quietly database connection
  4. commit transaction

So my problem is that the moment when transaction needs to be committed the connection is already closed so the commit fails.
Any help and feedback is very useful

Cheers,
Valeri

Hi,

your ConnectionProvider should return a connection that doesn’t close when close() is called, because the connection is “managed” by the outer transaction. In fact, the TransactionManager will probably already open a connection and start a transaction on it. Your ConnectionProvider should check for existing transactions and return the underlying connection is there is one. Otherwise, it can return a “regular” connection, which is closed when “close()” is called.

Have a look at the UnitOfWorkAwareConnectionProviderWrapper. Probably, you would want to use this class as your ConnectionProvider. Then use that same instance (of UnitOfWorkAwareConnectionProviderWrapper) to get the connection to start the transaction. It will register the connection with the current Unit of Work and reuse that connection for all activity in the unit of work, only closing it when the commit was done.

Hope this helps.
Cheers,

Allard