I am using a single point database connection in my java application, here is the code of my connection manager:
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); } } }
The problem is that my work with the code works from the very beginning, but when the time has passed, it becomes very slow. What caused this problem and how can I fix it? During launch, my application processes about 20 requests per second, after 1 hour of operation it reaches 10 requests per second and after 3 days of operation it reaches 1 request in 10 seconds! PS: My application is a single-user application that processes many requests through a database. PS: Here are my JVM options in eclipse.ini:
--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
Unfortunately, the database is deleted, and I do not have access to it to monitor what is happening there.
Here is an example of my use:
String count="select count(*) as counter from TSN"; ResultSet rscount=DatabaseManager.executeQuery(count, "SNfinal"); if(rscount.next()) { numberofNodes=rscount.getInt("counter"); }
source share