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

  • JDBCTemplate and PrepareStatement

    1) Does JDBCTemplate do its own PrepareStatement internally?

    I read somewhere that it does but my DBA says that my select statement is running twice because it is not prepared!

    Running twice, once to get a the row and a second time to check if the are anymore to fetch!
    Only one row is ever returned but regardless it runs twice.

    2) Should I be Preparing all statements I use in Spring!?

    3) Is there a simple example to do this?

  • #2
    1. Yes
    2. No.
    3. It already does

    If it is running twice you are probably doing something weird or your DBA is expecting something else as you are .


    • #3
      That's good; so what I think is happening is that each time I call a query it prepares and then executes.
      What I'd like to do is have the query already prepared then each time I call it, it just executes!

      Is there a way of doing this?


      • #4
        I think you have a misunderstanding of what a PreparedStatement is (I suggest a read of the JDBC docs). What you want is to cache PreparedStatements and that is in general a configuration option for your datasource. Also my answer was a bit to straight and actually it depends on which method you use from the JdbcTemplate.


        • #5
          Hi Martin,

          I will refresh my knowledge on the PreparedStatement's but can you expand:
          1) "depends on which method you use from the JdbcTemplate"
          I'm generally using:
          this.getJdbcTemplate().queryForObject(this.inventi onPermId, new Object[] { docId }, String.class);

          What should I be using?

          2) "What you want is to cache PreparedStatements and that is in general a configuration option for your datasource".
          I'm using the apache driver to connect to Oracle [[org.apache.commons.dbcp.BasicDataSource]]

          i) What configuration are you referring to? Please state this option.
          ii) What driver has this option?