Closing Database Connections in Java

前端 未结 6 1269
小鲜肉
小鲜肉 2020-11-22 09:34

I am getting a little confused, I was reading the below from http://en.wikipedia.org/wiki/Java_Database_Connectivity

Connection conn = DriverManager.getConn         


        
6条回答
  •  情歌与酒
    2020-11-22 10:14

    Yes. You need to close the resultset, the statement and the connection. If the connection has come from a pool, closing it actually sends it back to the pool for reuse.

    You typically have to do this in a finally{} block, such that if an exception is thrown, you still get the chance to close this.

    Many frameworks will look after this resource allocation/deallocation issue for you. e.g. Spring's JdbcTemplate. Apache DbUtils has methods to look after closing the resultset/statement/connection whether null or not (and catching exceptions upon closing), which may also help.

提交回复
热议问题