Ali*_*i n 6 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我的代码工作在开始时是完美的,但是当它过去的时间变得非常慢.是什么导致了这个问题,我该如何解决?在启动时,我的应用程序每秒处理大约20个查询,运行1小时后达到每秒10个查询,运行3天后每10秒达到1个查询!PS:我的应用程序是一个单用户应用程序,通过数据库进行许多查询.PS:这是我在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
Run Code Online (Sandbox Code Playgroud)
不幸的是,数据库是远程的,我没有任何监视访问它来查找那里发生了什么.
以下是我的用法示例:
String count="select count(*) as counter from TSN";
ResultSet rscount=DatabaseManager.executeQuery(count, "SNfinal");
if(rscount.next()) {
numberofNodes=rscount.getInt("counter");
}
Run Code Online (Sandbox Code Playgroud)
您应该考虑使用断开连接的结果集,例如 CachedRowSet http://docs.oracle.com/javase/1.5.0/docs/api/javax/sql/rowset/CachedRowSet.html
public static ResultSet executeQuery(String SQL, String dbName)
{
CachedRowSetImpl crs = new CachedRowSetImpl();
ResultSet rset = null ;
Statement st = null;
try {
st = DatabaseManager.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rset = st.executeQuery(SQL);
crs.populate(rset);
}
catch (SQLException e) {
System.out.println(e.getMessage());
System.exit(0);
}finally{
rset.close();
st.close();
}
return crs;
}
Run Code Online (Sandbox Code Playgroud)
CachedRowSet 实现了 ResultSet,因此它的行为应该类似于 ResultSet。
http://www.onjava.com/pub/a/onjava/2004/06/23/cachedrowset.html
除了这些更改之外,我建议您使用池数据源来获取连接并关闭它们,而不是保留一个打开的连接。
http://brettwooldridge.github.io/HikariCP/
或者如果您不是 java7、bonecp 或 c3po。
编辑:
要回答您的问题,这可以解决您的问题,因为CachedRowSetImpl在使用时不会保持与数据库的连接。这允许您在Resultset填充.StatementCachedRowSetImpl
希望这能回答你的问题。
| 归档时间: |
|
| 查看次数: |
785 次 |
| 最近记录: |