Remote database connection open for long time

2019-08-15 00:34发布

问题:

I am executing some crud's on mssql server 2008 database by connecting remotely. I have business logic in Java with tight code for connection, statement and result set close() in finally block.
My doubt is, even when I finish the job the connection port are still open and it takes roughly 20 min to close all connections. I can view the open connection using netstat -a command.
I need to quickly close the connections since each operation opens/close connection several times. Hence I see large number of connections open. This is very insecure and costly to keep so many unwanted connections for long time which the business logic has already closed. I am using the latest jtds.jar file. Any hint or suggestion will be appreciated.
Thanks,
Akshay

Connection conn = null;
PreparedStatement ps = null;

ResultSet rs = null;

try {
    // Do stuff
    ...

} catch (SQLException ex) {
    // Exception handling stuff
    ...
} finally {
    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException e) { /* ignored */}
    }
    if (ps != null) {
        try {
            ps.close();
        } catch (SQLException e) { /* ignored */}
    }
    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException e) { /* ignored */}
    }
}

回答1:

Sorry guys and Thanks for taking out time to answer my question. The connection close was a framework call which was not closing properly. I didn't had access to it so I thought that was not the issue. Thank you once again for your time and guidance.