java 用Java重新打开数据库连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10043993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:22:48  来源:igfitidea点击:

Reopen database connection in Java

javajdbc

提问by Jiew Meng

After closing a database connection in java, can I reopen it? Or do I need to do DriverManager.getConnection()again?

在java中关闭数据库连接后,我可以重新打开它吗?还是我需要再做DriverManager.getConnection()一次?

采纳答案by Scott M.

I'm not 100% sure that you needto call DriverManager.getConnection()but what's the harm? You already closed the connection, just grab a new one when you need it. The garbage collector worries about that closed connection after you discard it.

我不是 100% 肯定你需要打电话,DriverManager.getConnection()但有什么危害?您已经关闭了连接,只需在需要时再获取一个新连接。垃圾收集器会在您丢弃它后担心关闭的连接。

回答by ring bearer

If you had called connection.close();, the connection(assuming java.sql.Connectiontype) becomes useless. This operation releases this Connection object's database and JDBC resources.

如果您已调用connection.close();,则connection(假设java.sql.Connection类型)将变得无用。此操作释放此 Connection 对象的数据库和 JDBC 资源。

So Yesyou need to get a fresh connection when before you can proceed by

所以是的,你需要在你可以继续之前获得一个新的连接

connection = DriverManager.getConnection()

回答by Nirmal- thInk beYond

yes, you cant do anything after closing connection. you have to call getConnection

是的,关闭连接后你不能做任何事情。你必须打电话getConnection

回答by Korhan Ozturk

As Javadocmentions:

正如Javadoc 所提到的:

getConnection: Attempts to establish a connection to the given database URL.

Therefore yes, calling getConnection() again looks like the only way to establish a new connection with database once you closed previous connection.

因此,是的,再次调用 getConnection() 看起来是关闭先前连接后与数据库建立新连接的唯一方法。

回答by Android Developer

Well Ideally I made slightly different approach and gave a different solution.

理想情况下,我采用了稍微不同的方法并给出了不同的解决方案。

Instead of opening and closing the database, which literally happening on every request of the client, I made a singleton instance of a class and made single open connection which will be reused as long as the process of the server is alive.

我没有打开和关闭数据库,这实际上发生在客户端的每个请求上,而是创建了一个类的单例实例,并创建了一个打开的连接,只要服务器的进程处于活动状态,就可以重用该连接。

In short:

简而言之:

I have getConnection()inside Databaseclass

我有getConnection()内部Database

  public Connection getConnection() throws Exception {
    try {
        String connectionURL = "jdbc:mysql://localhost:3306/someDatabase";
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        if (connection == null) {
            connection = DriverManager.getConnection(connectionURL, "someUser", LOCAL_MYSQL_PASSWORD);
        }

        return connection;
    } catch (Exception e) {
    }

}

The Databaseclass is singleton, hence reusing the same class and reusing the same connection.

所述Database类独居,因此重复使用相同的类和再使用相同的连接。

I've tested this by show processList, and this one was giving around 100 connections If I won't close the connection, and even If I close the connection, actual process will not likely to be killed, but put into sleep instead so it will be used whenever the same client will ask for same request, but if you have like 15 requests, then each of them will have separate processes, so closing and opening connection for me wasn't best solution.

我已经对此进行了测试show processList,这个提供了大约 100 个连接如果我不关闭连接,即使我关闭连接,实际进程也不太可能被杀死,而是进入睡眠状态每当同一个客户端要求相同的请求时使用,但如果你有 15 个请求,那么每个请求都会有单独的进程,所以关闭和打开连接对我来说不是最好的解决方案。

By this I have 1 single process which is responsible layer for queries.

通过这个,我有 1 个单独的进程负责查询层。