java - 传统的数据库单例连接效果不佳

标签 java jdbc query-performance

我在我的 java 应用程序中使用单例数据库连接,这里是我的连接管理器类的代码:

public abstract class DatabaseManager {
    //Static instance of connection, only one will ever exist
        private static Connection connection = null;    
        private static String dbName="SNfinal";
        //Returns single instance of connection
        public static Connection getConnection(){       
            //If instance has not been created yet, create it
            if(DatabaseManager.connection == null){
                initConnection();
            }
            return DatabaseManager.connection;
        }   
        //Gets JDBC connection instance
        private static void initConnection(){           
            try{        
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                   String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
                      "databaseName="+dbName+";integratedSecurity=true";

                DatabaseManager.connection =
                             DriverManager.getConnection(connectionUrl);        
            }
            catch (ClassNotFoundException e){       
                System.out.println(e.getMessage());
                System.exit(0);
            }
            catch (SQLException e){         
                System.out.println(e.getMessage());
                System.exit(0);
            }
            catch (Exception e){        
            }       
        }
    public static ResultSet executeQuery(String SQL, String dbName)
    {
        ResultSet rset = null ;
        try {
               Statement st = DatabaseManager.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
               rset = st.executeQuery(SQL);
               //st.close();
        }
        catch (SQLException e) {
            System.out.println(e.getMessage());
            System.exit(0);
        }
        return rset;
     }

    public static void executeUpdate(String SQL, String dbName)
    {
        try {
               Statement st = DatabaseManager.getConnection().createStatement();
               st.executeUpdate(SQL);
               st.close();
        }

        catch (SQLException e) {
            System.out.println(e.getMessage());
            System.exit(0);
        }
     }
}

问题是我的代码在开始时工作完美,但随着时间的推移它变得非常慢。是什么导致了这个问题,我该如何解决? 在开始时,我的应用程序每秒处理大约 20 个查询,运行 1 小时后达到每秒 10 个查询,运行 3 天后达到每 10 秒 1 个查询! P.S:我的应用程序是一个单用户应用程序,它通过数据库进行许多查询。 P.S:这是我在 eclipse.ini 中的 JVM 参数:

--launcher.XXMaxPermSize
512M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
512m
--launcher.defaultAction
openFile
--launcher.appendVmargs
-vmargs
-Dosgi.requiredJavaVersion=1.6
-Xms500m
-Xmx4G
-XX:MaxHeapSize=4500m

不幸的是,数据库是远程的,我无法对其进行任何监控以了解那里发生了什么。

这是我的用法示例:

String count="select count(*) as counter from TSN";
ResultSet rscount=DatabaseManager.executeQuery(count, "SNfinal");
if(rscount.next()) {
    numberofNodes=rscount.getInt("counter");
}

最佳答案

What caused that problem and how can i fix that?

这里的主要问题是 executeQuery() 方法。 您没有关闭 Statement,我想您已经注释了 st.close() 行,因为您需要打开 ResultSet 进行进一步处理。 我知道您的想法是避免在您的应用程序中看到重复的 JDBC 代码,但这不是正确的方法。

规则是:关闭ResultSet,然后关闭Statement, 否则你没有正确释放资源,你就会遇到你所描述的那种问题。

Here你可以找到关于如何正确关闭资源的很好的解释(请记住,在你的情况下你不需要 关闭连接)

编辑: 一个例子可以是

try{
Statement st = DatabaseManager.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rsCount = st.executeQuery(count);         //count="select count(*) as counter from TSN";
if(rsCount.next()) {
    numberofNodes=rscount.getInt("counter");
}
} catch (SQLException e) {
    //log exception
} finally {
    rsCount.close();
    st.close();
}

关于java - 传统的数据库单例连接效果不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20716169/

相关文章:

java - 如何更改java中第3方库的日志级别

java - checkin mvn 版本时出现 "Exception in thread "main"java.lang.UnsupportedClassVersionError : Bad version number in . class file"错误

java - 嵌入式 Derby/Java DB 中的错误自动增量

java - SQL 数据库未使用 execute/addBatch 通过 Java 填充

mysql - 更新操作期间的MySQL CPU使用率

algorithm - Delta E (CIE Lab) 在 SQL 中计算和排序的性能

java - JButton 在 Windows 上显示浅蓝色背景

java - 在 Java 中找不到方法 "setString(int,String)"的符号

sql - 我如何优化这个 JOIN 查询?

JavaScript 或 Python 从 block 数据中提取 IP 地址