Looking through the code a bit, I would need to understand better how you are using co-routines and handles. An example (ideally of a failing piece of code!) would help me a lot.
If you are *not* using the Extension framework (and inside Jdbi today, there exists only the SqlObject extension), then the thread local should not be an issue for you:
- any of the useHandle, withHandle, inTransaction or withTransaction operations ends up in withHandle(HandleCallback)
- this method will try to acquire an existing handle through the threadlocal. This can happen when you use a construct like this:
@Test
public void testUseHandleUsesSameHandle() {
Jdbi jdbi = h2Extension.getJdbi();
jdbi.useHandle(handle1 ->
jdbi.useHandle(handle2 ->
assertThat(handle2).isSameAs(handle1)));
}
So if you call nested operations on the same thread, you will receive the same handle. That is intentional and, unless you plan on executing multiple operations concurrently on different connections (hence also different handles), passing the handle around is the right thing to do. Most databases only support per-connections transactions (I would not be shocked to learn that some of the commercial offerings do cross-connection transactions) otherwise you have a distributed transaction which works if you replace the transaction handler in the Jdbi with one that supports distributed transactions.
You can move the handle from one thread to another (so coroutines should work in theory), but you need to coordinate thread access to them (only one thread can use them at a time, otherwise funny things might happen) and your connection (which is a single TCP connection to the database) will most likely serialize the threads.
SQLObject extensions work in a similar way. As long as you do not try to do anything against the nature of transactions (which are attached to connections, which in turn are single-strand-of-execution), I don't think there should be a problem. What you can not do is creating an object that uses a single handle (like an attached sql object) and then use it in coroutine scope for parallel execution. That will in any case re-serialize your operations on the database connection and most likely lead to major confusion of the handle state as it cycles in and out of parallel execution of the sql object extension methods.
Some code of what you are trying to do would be super-helpful.
-h