title: How to use JdbcInterceptor to retry queries
body: <p>I want to retry MySql queries in cases of deadlock which will be removed if I can simply retry.</p>
  
  <p>I use apache tomcat jdbc pool library for mysql connection pooling and it has concept of JdbcInterceptor objects which can be attached to existing Connection object. I wonder how to use these interceptors to do a query retry. How can I know if query has thrown an exception and I should retry?</p>
  
  <p>I am thinking of doing something like this but I am confused in getting the right retry logic:</p>
  
  <pre><code>public class JdbQueryRetryInterceptor extends JdbcInterceptor {
      private final static Logger logger = LoggerFactory
              .getLogger(JdbQueryRetryInterceptor.class);
  
  
  
      @Override
      public void reset(ConnectionPool parent, PooledConnection con) {
  
  
      }
  }
  </code></pre>
  
  <p>And I have added this interceptor in the pool properties:</p>
  
  <pre><code>p.setJdbcInterceptors("com.test.mysql.JdbQueryRetryInterceptor");
  </code></pre>
  
  <p>I am not sure if it can be done at interceptor level however since tomcat docs say that it can be done so I want to go with this approach.</p>
  
  <p><a href="http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html" rel="nofollow">http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html</a></p>
  
  <blockquote>
    <p>Ability to configure custom interceptors. This allows you to write custom interceptors to enhance the functionality. You can use
    interceptors to gather query stats, cache session states, reconnect
    the connection upon failures, retry queries, cache query results, and
    so on. Your options are endless and the interceptors are dynamic, not
    tied to a JDK version of a java.sql/javax.sql interface.</p>
  </blockquote>