Forum Stats

  • 3,827,527 Users
  • 2,260,790 Discussions
  • 7,897,291 Comments

Discussions

Query Timeout for Prepared Statement

nitul.kukadia
nitul.kukadia Member Posts: 15

Hi All,

          JDBC API has Query Timeout parameter in PreparedStatement.

         PreparedStatement ps= connection.prepareStatement(QUERY);

         ps.setQueryTimeout(seconds);

         I need to set this parameter every time, when I create prepared statement from connection.

         Is there any way to set this parameter globally, so that eery prepared statement will be created from connection, has already query timeout parameter.

Thanks

Tagged:

Best Answer

  • I need to set this parameter every time, when I create prepared statement from connection.

    Based on Joe's answer you might consider creating you own library class/method to make it easier to enforce consistency.

    The method could take a connection parameter and a query string parameter and return a prepared statement. It would then set the timeout.

    Use a custom class that contains that method so that the class can could provide a configurable timeout (perhaps with a default value).

    The class could then include a method to modify the timeout.

Answers

  • Not from within the JDBC standard API, but depending on the driver you are using and/or whether

    you are using a fancy connection pooling implementation like WebLogic's for instance, you may

    have property/configuration settings that will essentially do the setQueryTimeout() call for you,

    in the background, whenever you create/prepare a statement...

    The notion of a Connection.setDefaultStatementQueryTimeout() method is not without merit.

    Joe Weinstein-Oracle
  • I need to set this parameter every time, when I create prepared statement from connection.

    Based on Joe's answer you might consider creating you own library class/method to make it easier to enforce consistency.

    The method could take a connection parameter and a query string parameter and return a prepared statement. It would then set the timeout.

    Use a custom class that contains that method so that the class can could provide a configurable timeout (perhaps with a default value).

    The class could then include a method to modify the timeout.

This discussion has been closed.