简体   繁体   English

DropWizard 中的 JDBI 3,按需重用连接

[英]JDBI 3 in DropWizard, onDemand reusing connection

Trying to figure out this case from the documentation:试图从文档中找出这种情况:

http://jdbi.org/#_attached_to_handle 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.我的想法是,在下面,两者的行为都相同,并且 dao1 在调用 insert 时将检查是否有正在进行的事务/连接,对于这种类型并使用它。

However documentation says this true for onDemand instances, however, this dao2 was attached, not an onDemand one as dao1.然而,文档说这对于 onDemand 实例是正确的,但是,这个 dao2 是附加的,而不是像 dao1 那样的 onDemand 。

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 .dao2调用将从显式打开的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 ).dao1调用将为每个方法调用使用单独的事务(来自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:如果你想在事务范围内执行来自不同SqlObject的方法,你有几个选择:

  • Jdbi#inTransaction / Jdbi#useTransaction : works for both onDemand / attach Jdbi#inTransaction / Jdbi#useTransaction :适用于onDemand / attach
  • Combination of @Transaction and @CreateSqlObject , as in the docs @Transaction@CreateSqlObject组合,如文档中所示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM