Announcement Announcement Module
Collapse
No announcement yet.
JDBC - How to print prepared statement SQL if SQLException ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JDBC - How to print prepared statement SQL if SQLException ?

    Hello,


    i would like to know if there is a way to print prepared statement SQL if SQLException occurs.



    in JDBCTemplate class catch (SQLException ex) , the parameters of the prepareStatem are removed from SQL just before calling getExceptionTranslator().translate("PreparedStatem entCallback", sql, ex);



    code from JDBCTemplate :


    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("PreparedStatem entCallback", sql, ex);
    }



    Question : does there is a way to redifine getSql() method to prevent it to remove parameters ?


    regards,
    jfdesmar

  • #2
    Please use [ code][/code ] tags when posting code.

    I also suggest a read on jdbc and prepared statements. There are no arguments in the statement just ? because replacing the arguments done is done by the driver or database (depending on your vendor). So spring doesn't know how it is done. You can wrap your datasource with something like P6Spy which is basically intended for this.

    Comment


    • #3
      Hello,

      thanks for your response.

      I tried to proxy the org.springframework.jdbc.core.JdbcTemplate class with ProxyFactoryBean defining an interceptor method that implements org.springframework.aop.ThrowsAdvice and i got the value in the update() method parameter ( PreparedStatementCreatorImpl)).


      Do you believe proxing a JDBC template via ProxyFactoryBean /CGLIB is a robust and proven solution ?


      regards,
      jfdesmar

      <bean id="proxy_jdbcTemplate" class="org.springframework.aop.framework.ProxyFact oryBean">
      <property name="target" ref="my_jdbcTemplate"/>
      <property name="proxyTargetClass" value="true" />
      <property name="interceptorNames">
      <list>
      <value>ex_interceptor</value>
      </list>
      </property>
      </bean>




      >>>afterThrowing:methodublic int org.springframework.jdbc.core.JdbcTemplate.up
      date(org.springframework.jdbc.core.PreparedStateme ntCreator) throws org.springfr
      amework.dao.DataAccessException
      >>>afterThrowing:args:0:PreparedStatementCreatorFa ctory.PreparedStatementCreator
      Impl: sql=[xxxxxxxxxxxxxxxxx) VALUES(?
      , ?, ?)]; parameters=[Wed Nov 16 18:13:05 CET 2011, 233322546000005, JunitTestUs
      erLogin]
      >>>afterThrowing:target:org.springframework.jdbc.c ore.JdbcTemplate@17431b9
      >>>afterThrowing:ex:org.springframework.dao.DataIn tegrityViolationException: Pre
      paredStatementCallback;

      Comment

      Working...
      X