Posted By

octaviannitagmailcom on 02/24/12


Tagged

connection close jdbc statement resultset boilerplate


Versions (?)

Boilerplate: closing JDBC resources


 / Published in: Java
 

Boilerplate JDBC code to close various resources (I just hate repeating myself in my code...)

  1. public static void close(ResultSet resultSet, Statement statement, Connection connection) {
  2. if (resultSet != null) {
  3. try {
  4. resultSet.close();
  5. } catch (SQLException e) {
  6. logger.error("cannot close result set", e);
  7. }
  8. }
  9. if (statement != null) {
  10. try {
  11. statement.close();
  12. } catch (SQLException e) {
  13. logger.error("cannot close statement", e);
  14. }
  15. }
  16. if (connection != null) {
  17. try {
  18. connection.close();
  19. } catch (SQLException e) {
  20. logger.error("cannot close database connection", e);
  21. }
  22. }
  23. }
  24.  
  25. public static void close(ResultSet resultSet, Statement statement) {
  26. close(resultSet, statement, null);
  27. }
  28.  
  29. public static void close(ResultSet resultSet) {
  30. close(resultSet, null, null);
  31. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: __W_O_L_F__ on July 4, 2013

I am running my code on oracle wls. And apparantly it is possible to get a problem with the connection pool when having this approach. What I now beleve to be the correct way is to close the connection by firts casting it to WLConnection . something like: weblogic.jdbc.extensions.WLConnection con = (WLConnection) stmt.getConnection(); con.clearStatementCache(); con.close();

Any one care to commet?

You need to login to post a comment.