Announcement Announcement Module
Collapse
No announcement yet.
JDBCTemplate Codebase Query Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JDBCTemplate Codebase Query

    Hi..

    @ Developers of Spring
    I have a query regarding the codebase of JDBC Template. We have two callback methods in JDBC Template, one with connectioncallback as param and other one with statementcallback as param. But there is a major difference regarding connection handling which I am not clear with. In the method with connection callback as param, connection is taken from the connection proxy if the instance of nativejdbcextractor is null, where as in the method with statementcallback action, if the instance of nativejdbcextractor is null, we use the connection as is and do not get connection from connection proxy. I am unable to decipher the reason for such a difference between both these callback methods. Also the first method with connectioncallback is not used anywhere in JdbcTemplate and only the second one is used. It would be helpful to get an explanation of such a design decision. Please refer to the code as under which defines both the methods:

    Ref Code -
    Code:
    public Object execute(ConnectionCallback action) throws DataAccessException {
            Assert.notNull(action, "Callback object must not be null");
    
            Connection con = DataSourceUtils.getConnection(getDataSource());
            try {
                Connection conToUse = con;
                if (this.nativeJdbcExtractor != null) {
                    // Extract native JDBC Connection, castable to OracleConnection or the like.
                    conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
                }
                else {
                    // Create close-suppressing Connection proxy, also preparing returned Statements.
                    conToUse = createConnectionProxy(con);
                }
                return action.doInConnection(conToUse);
            }
            catch (SQLException ex) {
                // Release Connection early, to avoid potential connection pool deadlock
                // in the case when the exception translator hasn't been initialized yet.
                DataSourceUtils.releaseConnection(con, getDataSource());
                con = null;
                throw getExceptionTranslator().translate("ConnectionCallback", getSql(action), ex);
            }
            finally {
                DataSourceUtils.releaseConnection(con, getDataSource());
            }
        }
    Code:
    public Object execute(StatementCallback action) throws DataAccessException {
            Assert.notNull(action, "Callback object must not be null");
    
             Connection con = DataSourceUtils.getConnection(getDataSource());
            Statement stmt = null;
            try {
                Connection conToUse = con;
                if (this.nativeJdbcExtractor != null &&
                        this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) {
                    conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
                }
                 stmt = conToUse.createStatement();
                applyStatementSettings(stmt);
                Statement stmtToUse = stmt;
                if (this.nativeJdbcExtractor != null) {
                    stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt);
                }
                Object result = action.doInStatement(stmtToUse);
                handleWarnings(stmt.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.
                JdbcUtils.closeStatement(stmt);
                stmt = null;
                DataSourceUtils.releaseConnection(con, getDataSource());
                con = null;
                throw getExceptionTranslator().translate("StatementCallback", getSql(action), ex);
            }
            finally {
                JdbcUtils.closeStatement(stmt);
                DataSourceUtils.releaseConnection(con, getDataSource());
            }
        }

  • #2
    The ConnectionCallback lets you work on a Connection directly. As this connection is managed by Spring the framework prevents the user from directly calling the close method (hence the proxy).

    This issue isn't there when you use a Statement hence no proxy is needed.

    Comment

    Working...
    X