简体   繁体   中英

tomcat 7 jdbc connection pool and jdbc4 validation

I would like to validate a jdbc connection using tomcat 7 jdbc pool using JDBC4 Connection.isValid instead of a validation Query.

I am wondering if implementing a Validator like bellow would be enough. Also I am wondering which would be a nice timeout.

Thanks in advance! Fede

PS: I am using oracle 11g.

public class MyValidator implements Validator{
   public static final int DEFAULT_TIMEOUT = 5;

   public boolean validate(final Connection c, final int validateAction){
      try{
         return c.isValid(DEFAULT_TIMEOUT);
      }catch(Exception e){
        // LOG e
        return false;
      }
}

A debugger would help you troubleshoot the situation: Tomcat / DBCP connection pools do not return a raw connection but a wrapper/adapter that recycle underlying "true" connections. The pool usually checks the connections by itself (reconnect if needed) and, I guess, does not allow to really validate.

You have more info about the pool validation parameters in the Tomcat doc . Another exchange deals about JBoss pools wrapping Oracle connections, what is a common practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM