由于有些查询很简单,有些查询需要特殊操作,所以在我的项目中,我使用JDBI的Object和流体语法,以及它的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是否像在抽象方法中那样处理关闭操作?
发布于 2016-04-21 13:24:11
使用onDemand时不需要关闭句柄。
从文件中:
获取sql对象实例的最后一种方法将根据需要自动获得和释放连接。通常,这意味着它将获得一个连接来执行一个语句,然后立即释放它,但是各种事情,比如打开事务或基于迭代器的结果,都会导致连接保持打开状态,直到事务完成或迭代结果被完全遍历为止。
请参阅本页末尾:overview/
https://stackoverflow.com/questions/36757364
复制相似问题