Announcement Announcement Module
Collapse
No announcement yet.
When does JdbcTemplate close connections? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • When does JdbcTemplate close connections?

    I am using JdbcTemplate.queryForObject to return me a Timestamp from my SQL query. The DataSource that I use in the JdbcTemplate constructor is a DataSource that is returned from JNDI. Once queryForObject method completes, when does JdbcTemplate close the connection. If I look at the source code, I can see when the execute methods close the connections, but I cannot seem to find when JdbcTemplate closes the connections. Is it after a method is performed on JdbcTemplate?

    Thanks

    -jay

  • #2
    queryForObject I believe delegates to other lower level methods, which do free the connections. You might have to dig in the source code a little deeper.

    Comment


    • #3
      Bingo...thanks.

      I look at the source instead of decompiling and found that queryForObject calls query which calls another version of query which calls another version of query which calls this:

      Code:
      	protected Object query(
      			PreparedStatementCreator psc, final PreparedStatementSetter pss, final ResultSetExtractor rse)
      			throws DataAccessException {
      
      		if (logger.isDebugEnabled()) {
      			String sql = getSql(psc);
      			logger.debug("Executing SQL query" + (sql != null ? " [" + sql  + "]" : ""));
      		}
      		return execute(psc, new PreparedStatementCallback() {
      			public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
      				ResultSet rs = null;
      				try {
      					if (pss != null) {
      						pss.setValues(ps);
      					}
      					rs = ps.executeQuery();
      					ResultSet rsToUse = rs;
      					if (nativeJdbcExtractor != null) {
      						rsToUse = nativeJdbcExtractor.getNativeResultSet(rs);
      					}
      					return rse.extractData(rsToUse);
      				}
      				finally {
      					JdbcUtils.closeResultSet(rs);
      					if (pss instanceof ParameterDisposer) {
      						((ParameterDisposer) pss).cleanupParameters();
      					}
      				}
      			}
      		});
      	}
      Thanks for your help!

      -jay

      Comment

      Working...
      X