简体   繁体   English

执行#isClosed方法后是否需要关闭连接?

[英]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? 返回后是否需要添加conn.close()? 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.) (并且您不应该捕获Exception ;捕获任何声明的特定异常,这里不应该包含任何异常,这样就不会吃掉真正的问题报告。)

Do I need to add conn.close() after the return? 返回后是否需要添加conn.close()?

You can't have any piece of code after a return statement. return语句后不能包含任何代码。 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! 如果它只是连接测试,最好关闭它,因为您不会再使用它了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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