1
因为我有一些疑问是简单的,有些需要特殊的操作,在我的项目我使用JDBI的对象API与流体语法混合,与它一起的点播功能,像这样:使用JDBI的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时,我是否需要确保像我在示例中那样关闭我的句柄,还是像抽象方法那样处理闭合处理?
这就是我的想法,我的担心是正义的,因为我是开手柄的通常流程以外,我将不得不关闭它自己以及。 –