DriverManager getConnection and Memory Leak
Hi,
I use the following code to get jdbc connection to the oracle database. When the oracle database is running, there is no problem.
If for some reason, the database is not running, the it will remain in the loop and keeps on checking for the connetion at a specified interval. This causes memory leak. Over a week, the memory can go over 200 MB.
Within the loop, I am not using registerDriver().
Could you please let me know, what am I not doing right?
Thanks.
Regards.
-M Pareek
--- Code Sample ----
public static oracle.jdbc.driver.OracleConnection getConnection (String oracleConnect,
String oracleUser,
String oraclePWD,
String retryTimeSt,
String retryTimeLongSt) {
/**
* This method is used to obtain a connection to the Oracle Database
*
* @param oracleConnect connect string containing driver,port and database info
* @param oracleUser username for the Oracle user
* @param oraclePWD password for the Oracle user
* @param retryTimeSt time out before attempting a new Oracle connection
* @param retryTimeLongSt longer time out before attempting a new Oracle connection
* @return OracleConnection an Oracle Connection object
* @exception Exception
* catches any errors encountered from this method
*
*/
boolean cont = false; /* continue flag */
OracleConnection oracleConn = null; /* conection object */
String retryTime = retryTimeSt; /* retry timeout value */
int i = 0;
// we only get 10 tries at getting a connection
try
{
OracleDriver d = (OracleDriver)(Class.forName("oracle.jdbc.driver.OracleDriver" ).newInstance());
DriverManager.registerDriver(d);
}
catch (Exception ex)
{
MsgLogger.log(MessageBundle.UDP_00002,ex.getMessage());
}
while (true) {
// if we have tried to connect 10 times with the default amount
// of time waitted before attempts then we will wait a longer time
// between attempts.
System.gc();
if ( i > 10 )
retryTime = retryTimeLongSt;
try {
oracleConn = (OracleConnection)DriverManager.getConnection(oracleConnect, oracleUser, oraclePWD);
oracleConn.setAutoCommit(false);
oracleConn.
cont = true;
break; // break the loop when we have a connection
}
catch (Exception ex) {
MsgLogger.log(MessageBundle.UDP_00002,ex.getMessage());
i++;
try{
if (oracleConn != null) {
oracleConn.close();
oracleConn.
oracleConn = null;
}
}
catch(Exception et){
oracleConn = null;
}
pauseProcessing(retryTime); // wait for a bit before trying again
}
} // end of while
return oracleConn;
}