简体   繁体   中英

Do I need to close the connection after doing #isClosed method?

I am wondering if testing the database connection will add up to the connection pool? to make it clear please see my code.

try {
    if (conn ==null || conn.isClosed()) {
        return false;
    }else {
        return true;
    }
} catch (Exception e){ 
    return false;
}

Do I need to add conn.close() after the return? even if it's just connection testing?

If this method is responsible in your design for making sure the connection is closed, then yes, you should close it just to make things clean; there's no sense in letting stale connections pile up. (And you shouldn't be catching Exception ; catch whatever specific exception is declared, which shouldn't be any here, so that real problem reports don't get eaten.)

Do I need to add conn.close() after the return?

You can't have any piece of code after a return statement. It'll be an unreachable code!

even if it's just connection testing?

If its just connection testing, better to close it, as you're not gonna use it further!

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