简体   繁体   English

我应该在哪里存储DBI连接,什么时候应该关闭它?

[英]Where should I store a DBI connection and when should I close it?

I'm storing it in a public static field 我将其存储在公共静态字段中

public class DB {

    private static final String url = "jdbc:sqlite:file:target/todo";
    public static final DBI dbi = new DBI(url);


    public static void migrate() {
        Flyway flyway = new Flyway();
        flyway.setDataSource(url, "", "");
        flyway.migrate();
    }
}

And never close it, is there a better option? 而且永远不要关闭它,还有更好的选择吗?

This amounts to how you handle getting ahold of any dependency in your application. 这相当于您如何处理应用程序中的任何依赖关系。 The best general model, IMHO, is passing it in to the constructor of things that need it. 最好的通用模型,恕我直言,将其传递给需要它的事物的构造函数。 If you want to put some kind of DAO facade around your database access, pass the DBI to the ctor of your DAO. 如果要在数据库访问周围放置某种DAO门面,请将DBI传递给DAO的ctor。 If you are using a DI framework, bind the DBI instance to the framework, and @Inject it. 如果使用的是DI框架,请将DBI实例绑定到该框架,然后@Inject。

For your specific question about Connections, the DBI equivalent of a JDBC Connection is the Handle. 对于有关连接的特定问题,句柄是JDBC连接的DBI等效项。 You should obtain a Handle, use it, and close it as soon as you are done. 您应该获取一个Handle,使用它,并在完成后立即将其关闭。 Typical use of DBI instance is to give it a DataSource which manages actual database connections, by releasing the Handle as soon as you finish with it, you make better use of the connection pool. DBI实例的典型用法是为它提供一个管理实际数据库连接的数据源,通过在完成处理后立即释放Handle,可以更好地利用连接池。

In most cases, you would only close the DBI instance if you want to close the Datasource, that is all that closing the DBI instance does. 在大多数情况下,仅当您要关闭数据源时才关闭DBI实例,这就是关闭DBI实例所要做的。 98% of the time, in a java-for-server world, closing the datasource doesn't make sense, so worrying about closing the DBI (as compared to the Handle) is not a big deal. 98%的时间里,在Java for Server的世界中,关闭数据源没有任何意义,因此担心关闭DBI(与Handle相比)并不是什么大问题。

When using JDBI, keep in mind: 使用JDBI时,请记住:

DBI -> Datasource
Handle -> Connection
Query/SQLStatement -> Statement

This doc elaborates on these. 本文档对此进行了详细说明。

The best option is to make a handler class. 最好的选择是制作一个处理程序类。 This class "hands" out handles as someone needs them. 此类在有人需要时“分发”出句柄。 You need to worry most about closing the handles. 您最需要担心关闭手柄。 If you really want a fast system, something like c3p0 is great. 如果您真的想要一个快速的系统,那么像c3p0这样的东西很好。 Normally, it is best to make mutable objects private and final, using getters/setters. 通常,最好使用getter / setter将可变对象设为私有和最终对象。 You can keep DBI static if you want. 您可以根据需要使DBI保持静态。 When you make a call to checkout a Handle, you should use try-with-resources. 调用检出Handle时,应使用try-with-resources。

public Handle getHandle(){
    dbi.open(dataSource);
}

public void doSomething(){
    try(Handle handle = getHandle()){
        // Do something
    }
    catch(DBIException e){
        // TODO Handle it...
    }
}

I'd probably make my handler autocloseable and close everything left over (like any connection pools) when it closes. 我可能会让处理程序自动关闭,并在关闭时关闭所有剩余的内容(如所有连接池)。 This, by the way, lets you pull your credentials in the handler and keep that data safe there. 顺便说一句,这使您可以在处理程序中提取凭据,并将数据保存在那里。

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

相关问题 使用SessionFactoryUtils时是否应该关闭连接 - Should I close connection when using SessionFactoryUtils 我应该关闭tcp连接吗? - Should I close the tcp connection? Android SQLite helper - 我应该在哪里关闭连接? - Android SQLite helper - where should I close the connection? 我应该在哪里关闭我的 activemq 连接(java,球衣) - Where should I close my activemq connection (java, jersey) 在构造函数中打开数据库连接,何时应该关闭它? - Opening a database connection in a constructor, when should I close it? 我何时应该关闭在无状态会话bean中创建的JMS连接? - When should I close a JMS connection that was created in a stateless session bean? 我应该在何时何地使用close()方法来避免ObjectInputStream中的IOException? - Where and when should I use the close() method to avoid IOException in ObjectInputStream? 我什么时候应该关闭()一个BufferedWriter? - When should I close() a BufferedWriter? 我应该关闭来自数据源的连接吗? - Should I close a connection that came from a datasource? 我应该首先关闭哪个,PreparedStatement还是Connection? - Which should I close first, the PreparedStatement or the Connection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM