简体   繁体   中英

Jdbc - connection refused Exception

I am using jdbc to connect my java program with Oracle database . The program is working fine when the hits to the database is low , however when the hits to the database increases I am facing the following exception:

java.sql.SQLException: Io exception: The Network Adapter could not establish the connectionjava.sql.SQLException: Closed Connection

Ps i have closed connection within finally block .

try{ 
     con.close();
    }catch(Exception ex){
       System.out.println(ex);
 }

Any idea how to resolve this issue ?

Thanks in advance

The program is working fine when the hits to the database is low

Seems your application tries to establish a connection with database but number of existing connections are equal to number of SESSIONS allowed with database so database refuse to establish further connections.

To verify the same you can see number of allowed and current session details , for Oracle you can use below queries:

The number of sessions the database was configured to allow

SELECT name, value FROM v$parameter WHERE name = 'sessions'

The number of sessions currently active

SELECT COUNT(*) FROM v$session

I am not sure about your workflow but this issue look like connection leak issue where you are trying to use a closed connection. Whenever you are trying to close you connection put it in finally block. So, that you should not miss it in case of exception. The order of closing resultset, statement and connection should be like this.

try
{
 //open connection
//execute statement
//get resultset
//process resultset
}
catch(Exception e)
{
//Catch exception(using Exception just for example). It is a good programming practice to catch/throw specific exceptions instead of specifying super class Exception.
}
finally
{
rs.close();
stmt.close();
conn.close();
}

It would be helpful to explain if you have pasted your complete code. You can also use connection pool but if you are not closing or reusing them properly you will face the same issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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