Benedikt Waldvogel
Benedikt Waldvogel

Reputation: 12886

How to assert that database connection is in a transaction?

I'm using an object/relational mapper to talk to the database. The library in my case is iBatis which is also used for transaction management. However, I recently had a case where iBatis didn't actually start a transaction even though startTransaction() etc. was called as documented. After some debugging I found out that there was a configuration mistake on my side.

One might blame iBatis but I would like to avoid such misconceptions in the future. So here's the question:

How can I programmatically assert that the current database connection is running in a transaction?

The databases I'm using are Oracle, MySQL and H2 (for testing).

Upvotes: 2

Views: 748

Answers (1)

skaffman
skaffman

Reputation: 403581

I'm not 100% sure if this is absolutely indicative of being in a tx, but Connection.getAutoCommit() tells you if the connection is in auto-commit mode, where auto-commit "on" means "no transaction".

There may well be cases where this assertion does not hold, but most JDBC-based frameworks will use that setting to control transactions.

Upvotes: 2

Related Questions