Results 1 to 2 of 2

Thread: JDBCTemplate Codebase Query

  1. #1

    Default 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());
            }
        }
    ************************************************** ***********************
    Thanks and Regards,
    Tarun Pasrija
    System Software Engineer
    India Software Labs - IBM India Pvt Ltd.
    Manyatha Embassy Business Park, Bangalore
    Extention - (080-402) - 55265
    Mobile - +91-9972200221
    Email - tarun.pasrija@in.ibm.com

    ************************************************** ***********************

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •