JDBC Event Storage Engine - Postgresql Transaction Manager

Non-spring, anyone got an example of Postgresql Transaction Manager, I find it odd the disconnect between connection and transaction, no state passed between.

I guess for startTransaction I’ll have to register the transaction on some thread local storage, then for getConnection() check the TLS for an existing connection with a transaction started etc… ?

I came up with something using posgresql and cp3o, there’s probably issues but if it’s helps get going then below, as I wasn’t able to find other non-spring jdbc examples…

class WrappedConnectionProvider(val comboPooledDataSource: ComboPooledDataSource) : ConnectionProvider, TransactionManager {

private var tlsConnection : ThreadLocal = ThreadLocal()
override fun getConnection(): Connection = comboPooledDataSource.connection

override fun startTransaction() : Transaction {
val conn = this.tlsConnection.get() ?: this.connection.also {
it.autoCommit = false
tlsConnection.set(it)
}

return object : Transaction {
override fun rollback() {
try {
if(!conn.isClosed) {
conn.rollback()
}
} finally {
if(!conn.isClosed) {
conn.close()
}
tlsConnection.remove()
}
}
override fun commit() {
try {
conn.commit()
} finally {
conn.close()
tlsConnection.remove()
}
}
}
}

}

Hi Matt,

you can use the UnitOfWorkAwareConnectionProviderWrapper to have Axon register connections in the Unit of Work, so that the same transaction is reused between different components in the same Unit of Work.
You can then configure a transaction manager that takes a connection from this UoW and starts a transaction (of none was already started). It should use the UnitOfWork.onCommit() phase to commit the transaction. This ensures locks and other resources are released after transactions have been committed.

Hope this helps.

Allard

PS. I guess we should be looking into a JdbcTransactionManager of some sort…