views:

72

answers:

1

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).

+2  A: 

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.

skaffman
That should do it.
Matthew Flynn