Announcement Announcement Module
Collapse
No announcement yet.
A JdbcTemplate defect Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • A JdbcTemplate defect

    Code:
    
    public Object execute(PreparedStatementCreator psc, PreparedStatementCallback action)
                throws DataAccessException {
    
            Assert.notNull(psc, "PreparedStatementCreator must not be null");
            Assert.notNull(action, "Callback object must not be null");
            if (logger.isDebugEnabled()) {
                String sql = getSql(psc);
                logger.debug("Executing prepared SQL statement" + (sql != null ? " [" + sql + "]" : ""));
            }
    
            //here if got a excetion will not be translated.
            Connection con = DataSourceUtils.getConnection(getDataSource());
            PreparedStatement ps = null;
            try {
                Connection conToUse = con;
                if (this.nativeJdbcExtractor != null &&
                        this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativePreparedStatements()) {
                    conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
                }
                ps = psc.createPreparedStatement(conToUse);
                applyStatementSettings(ps);
                PreparedStatement psToUse = ps;
                if (this.nativeJdbcExtractor != null) {
                    psToUse = this.nativeJdbcExtractor.getNativePreparedStatement(ps);
                }
                Object result = action.doInPreparedStatement(psToUse);
                handleWarnings(ps.getWarnings());
                return result;
            }
            catch (SQLException ex) {
                // Release Connection early, to avoid potential connection pool deadlock
                // in the case when the exception translator hasn't been initialized yet.
                if (psc instanceof ParameterDisposer) {
                    ((ParameterDisposer) psc).cleanupParameters();
                }
                String sql = getSql(psc);
                psc = null;
                JdbcUtils.closeStatement(ps);
                ps = null;
                DataSourceUtils.releaseConnection(con, getDataSource());
                con = null;
                throw getExceptionTranslator().translate("PreparedStatementCallback", sql, ex);
            }
            finally {
                if (psc instanceof ParameterDisposer) {
                    ((ParameterDisposer) psc).cleanupParameters();
                }
                JdbcUtils.closeStatement(ps);
                DataSourceUtils.releaseConnection(con, getDataSource());
            }
        }
    Here is the code from JdbcTemplate.java start with line 523. if we are failed to get a connection when a exception occured

    Code:
    public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException {
    		try {
    			return doGetConnection(dataSource);
    		}
    		catch (SQLException ex) {
    			throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);
    		}
    	}

    notice if we use a custom SQLExceptionTranslator and what aforedmetioned happened, my custom sql exception translator will not be executed as the step of getting a connection is not inclued in the try catch block....

    do you have any ideas?

    regards,
    Jerry

  • #2
    The exception is already translated to a DataAccessException, also not sure what you want to translate it in otherwise? And as you can see it is by design as it is also documented in the throws clause of the getConnection method...

    If you really wanted you can always create your own subclass of JdbcTemplate, override the execute method, put an additional try/catch around it for the CannotGetJdbcConnectionException, when this happens, pull out the cause and pass that to the ExceptionTranslator.

    But if you really want this changed open a JIRA but I doubt it will be implemented.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      The exception is already translated to a DataAccessException, also not sure what you want to translate it in otherwise? And as you can see it is by design as it is also documented in the throws clause of the getConnection method...

      If you really wanted you can always create your own subclass of JdbcTemplate, override the execute method, put an additional try/catch around it for the CannotGetJdbcConnectionException, when this happens, pull out the cause and pass that to the ExceptionTranslator.

      But if you really want this changed open a JIRA but I doubt it will be implemented.

      thank you for your reply.
      I have a custom exception translator injected to the jdbcTemplate. I did some business logic in my translator. In case when I get connection fail my business logic will not be executed.
      that is what I don't want.
      but, I totally agree with your opinions that I can catch the CannotGetJdbcConnectionException and did my business logic again.
      this is a little misunderstanding you know. I expected the translator can handle all exceptions.

      Comment

      Working...
      X