繁体   English   中英

如何使用Java与数据库服务器建立持久连接?

[英]how to set up a persistent connection with a DB server using Java?

因此,我正在尝试优化一个使用java sql库从远程SQL服务器读取和写入的Java应用程序。 (这是一个项目)。

我们仅限于以下库:

com.mysql.*
java.io.*
java.lang.*
java.sql.*
java.text.*
java.util.*
javax.sql.*

而且我们的JVM内存限制为8MB。

现在设置代码的方式,对于需要访问数据库的每个功能,都会创建并关闭一个新连接。 例如:

public static void Read()
{
    Connection connection = NULL;
    try {
        connection  = DriverManager.getConnection(EnvManager.getDbUrl());
        Statement statement = connection.createStatement();

    statement.setFetchSize(Integer.MIN_VALUE);

        ******* FUNCTION ********
    }finally {
    if(connection != null) {
        try {
        connection.close();
        } catch (Exception e) {
        }
    }
}

     public static void Write()
{
    Connection connection = NULL;
    try {
        connection  = DriverManager.getConnection(EnvManager.getDbUrl());
        Statement statement = connection.createStatement(); 
    statement.setFetchSize(Integer.MIN_VALUE);

        ******* FUNCTION ********
    }finally {
    if(connection != null) {
        try {
        connection.close();
        } catch (Exception e) {
        }
    }
}

等等...

我正在尝试建立持久连接,以便可以传入连接对象,即

...
...
Connection connection = NULL;
try {
    connection  = DriverManager.getConnection(EnvManager.getDbUrl());
Read(connection);
Write(connection);

}finally {
    if(connection != null) {
        try {
        connection.close();
        } catch (Exception e) {
        }
    }
...
...

但是,当我尝试执行此操作时,每当我尝试向数据库中写入一个大文件(然后将其读取)(超过5 MB)时,我的JVM就会不断抛出错误,指出它的内存不足。

我的问题基本上是,当您创建单个连接对象并将其继续传递给函数时会发生什么? 似乎每次使用它都会以某种方式增长或复制。

我看了这个网站:

http://benjchristensen.com/2008/05/27/mysql-jdbc-memory-usage-on-large-resultset/

这建议我使用statement.setFetchSize(Integer.MIN_VALUE); 函数,以减少默认情况下JVM将整个结果集缓冲在内存中的情况,但这没有帮助。

非常感谢您提出任何有关解决此问题的想法,谢谢。

如果要将同一对象引用传递给其他方法,则不应复制任何内容。 您只是在传递对该对象的引用。 可能发生的情况是,您可能不会在每次执行SQL时都关闭Statement或PreparedStatements。

执行完查询后,您需要关闭所有将消耗资源的资源。 这是一个例子

public void Read() {
    Connection conn = null;
    Statement statement = null;
    ResultSet rs = null;

    try {
        connection  = DriverManager.getConnection(EnvManager.getDbUrl());
        statment = connection.createStatement();
        statement.setFetchSize(Integer.MIN_VALUE);

        // Do more stuff, iterate to ResultSet etc...
    } catch (SQLException ex) {
        // Exception handling stuff
        ...
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) { /* ignored */}
        }
        if (statment != null) {
            try {
                statment.close();
            } catch (SQLException e) { /* ignored */}
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) { /* ignored */}
        }
    }
}

您可能需要关闭Read()和Write()中的语句。 与这些对象关联的资源(在这种情况下,是用于读取/写入文件数据的缓冲区)在关闭之前不会释放。 关闭连接会自动关闭使用它创建的语句,但是如果要保持连接打开,则需要在完成连接后关闭该语句。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM