Announcement Announcement Module
Collapse
No announcement yet.
Close ResultSet objects in JdbcTemplate callbacks? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Close ResultSet objects in JdbcTemplate callbacks?

    Should ResultSet objects be explicitly closed when implementing a callback such as a PreparedStatementCallback for a JdbcTemplate execute method?

    For example in the following, should the rs object be closed as good practice for the method returns? The examples I have seen do not have an explicit rs.close() statement.

    Code:
                new PreparedStatementCallback()
                {
                   public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException
                   {
                      List tmplist = new ArrayList();
                      ResultSet rs = ps.executeQuery();
                      while (rs.next())
                      {
                         DetailBean bean = new DetailBean();
                         bean.setConnectstring(rs.getString(1));
                         bean.setUnit(rs.getString(2));
                         bean.setApp(rs.getString(3));
                         bean.setVersion(rs.getString(4));
                         bean.setInstalldate(rs.getDate(5));
                         tmplist.add(bean);
    
                         //load the bean into the hashmaps
                         Object tmp = unitmap.get(bean.getConnectstring());
    
                         if (tmp == null)
                         {
                            tmp = new HashMap(20);
                            unitmap.put(bean.getConnectstring(), tmp);
                         }
    
                         ((HashMap) tmp).put(bean.getApp(), bean);
                      }
                      return tmplist;
                   }
                }
    thanks,

    Brad

  • #2
    You should not need to close the result set, because you should not open it in the first place, but let Spring do it. Thus you don't want the following line:
    Code:
    ResultSet rs = ps.executeQuery();
    You should modify the example to use a callback that will cause Spring to obtain and close the result set. Use the following method. Look at the overloaded JdbcTemplate methods that take callbacks that handle result sets such as RowCallbackHandler. Like the following method:
    Code:
    public List query(PreparedStatementCreator psc, RowCallbackHandler rch) throws DataAccessException
    The usage in your example is not idiomatic, and the ResultSet handling is not best practice for Spring JDBC.

    Comment


    • #3
      ResultSet, Statement need to be closed in JDBC template?

      Hello,

      However there might be some cases wherein you need to perform two database operations in a single transaction wherein you would need to open your own PreparedStatement and ResultSet. E.g.

      if you have a Primary Key generation logic where you want to generate a sequence based primary key using a database table and the logic to generate this can be in a cluster of JVMs, it would be required to create your own result set and statement and close them.

      The logic could appear like

      Tx begins
      {
      Update Sequence = Sequence + ChunkSize // Statement 1
      Select Sequence; //// Statement 2
      CurrentSequenceValue = ResultSet.Sequence - ChunkSize
      }
      Tx Commit/Rollback

      Is this pattern wrong to be used with JDBC template?

      Thanks,
      Shashi

      Comment

      Working...
      X