使用 JDBI 的 onDemand 语法,我需要关闭句柄吗?

Using JDBI's onDemand syntax, do I need to close handles?

因为我有一些简单的查询,还有一些需要特殊操作,所以在我的项目中,我使用 JDBI 的对象 api 混合了流畅的语法,以及它的 onDemand 功能,如下所示:

public abstract class MyDAO implements GetHandle {

    @SqlQuery("SELECT anInt FROM aTable;")
    public abstract int getAnInt();

    public List<Integer> insertThings(List<Thing> things) {

        Handle h = getHandle();

        PreparedBatch batch = h.prepareBatch("INSERT INTO thingsTable (a, b) VALUES (?, ?)");

        for (Thing thing : things) {
            batch.add(things.getA(), thing.getB());
        }

        List<Integer> ids = batch.executeAndGenerateKeys(IntegerColumnMapper.WRAPPER).list();

        h.close();

        return ids;
    }
}

//Elsewhere in my code

MyDAO dao = jdbi.onDemand(myDAO.class);

dao.insertThings(things);

所以我的问题是,当以这种方式使用 JDBI 时,我是否需要确保像示例中那样关闭句柄,或者 onDemand 是否像在抽象方法中那样处理关闭?

使用onDemand时不需要关闭句柄

来自文档:

The final method for obtaining a sql object instance will obtain and release connection automatically, as it needs to. Generally this means that it will obtain a connection to execute a statement and then immediately release it, but various things such as open transactions or iterator based results will lead to the connection remaining open until either the transaction completes or the iterated result is fully traversed.

见本页结尾:http://jdbi.org/sql_object_overview/