This project has moved. For the latest updates, please go here.

Detecting if a session has an active transaction / getting transaction level

Aug 26, 2014 at 9:51 PM
Basically, the subject line. I had a longstanding bug just found today where a transaction wasn't active when it'd only be correct if a transaction was active. Generally most of our code goes through helper functions for all reads / writes so assertions would have possibly caught the problem. My horrific thought would be to iteratively call JetBeginTransaction until it fails with JET_errTransTooDeep and if the number of iterations is equal to the "known" max depth, assume that a transaction isn't active. This would only be debug mode, but it still feels dirty and unintentional side-effecty. I'd also not like to make debug builds any more slower than necessary, too (I haven't actually measured that cost, so it may be that this would be indistinguishable from free).

Alternatively, is there a way to make ESE enforce that a session has a transaction active whenever it's "used"? That'd be my preferred way of handling this, particularly since it would still be in effect in release mode. In this particular case, it was when reading JET_coltypBit, JET_coltypText, and JET_colTypLong values.
Sep 4, 2014 at 12:27 AM
You can use Api.JetGetSystemParameter() and pass in 38 as the JET_param. It's not documented, so it is not guaranteed to work in future versions. Hopefully we can expose a documented way in the future...