-1

Trying to figure out this case from the documentation:

http://jdbi.org/#_attached_to_handle

Say you have

SomeClass dao1 = dbi.onDemand(SomeClass.class)

then you have something in another method, other place:

    try ( Handle handle = dbi.open(); ) {
        SomeClass dao2 = handle.attach(SomeClass.class);

        // What happens here if I use dao1 ? 

        dao1.insert(...)
        dao2.insert(...)
        dao2.insert(...)
        dao1.insert(...)
    }

Note that they are the same type in these cases.

Will there be a new connection / transaction for each insert called or will they all be grouped into one transaction?

It is not clear from documentation.

My thinking is that underneath, both will behave the same and dao1 when invoking insert will check to see if there is an ongoing transaction / connection, and for this type and use that.

However documentation says this true for onDemand instances, however, this dao2 was attached, not an onDemand one as dao1.

mjs
  • 21,431
  • 31
  • 118
  • 200

1 Answers1

1

Will there be a new connection / transaction for each insert called or will they all be grouped into one transaction?

Calls to dao2 will be grouped in one transaction from explicitly opened handle.

Calls to dao1 will use separate transaction per each method call (from Jdbi#onDemand javadoc: an extension which opens and closes handles (as needed) for individual method calls).

If you want to execute methods from different SqlObject's in the scope of transaction, you have several options:

  • Jdbi#inTransaction/Jdbi#useTransaction: works for both onDemand/attach
  • Combination of @Transaction and @CreateSqlObject, as in the docs
Yurii Melnychuk
  • 858
  • 1
  • 5
  • 11
  • Hmm, this is wierd, that means that if you have a global onDemand instance registered in your code, you can not register one on the thread to be used instead later but they will be separate? I would expect the onDemand insert to check if there is anything attached already, regardless. If I onDemand for the number two as well, same? dbi.inTransaction is not available, and the dao only sees the interface methods. The guide is a little wrong, since onDemand will only get you the interface instance. I would have to cast it? – mjs Nov 03 '21 at 15:11
  • how can I use inTransaction after attach to ensure any other instances uses the same connection regarless of when they were created? inTransaction is not on dbi. – mjs Nov 03 '21 at 15:26
  • Do I use the handle.inTransaction after attach ? There is no dao.useTransaction as documentation suggest. – mjs Nov 03 '21 at 15:27
  • Ah, maybe if I extend SqlObject ? – mjs Nov 03 '21 at 15:28
  • No, i don't have that class available in this module. – mjs Nov 03 '21 at 15:29
  • Which version of `jdbi` are you using? Check this [method](https://jdbi.org/apidocs/org/jdbi/v3/core/Jdbi.html#inTransaction(org.jdbi.v3.core.HandleCallback)) – Yurii Melnychuk Nov 03 '21 at 15:46
  • If you are stuck on v2 then I have no solution for you as I am not aware of the peculiarities of the implementation of that version, never used it. – Yurii Melnychuk Nov 03 '21 at 15:47