Yes, it can be any valid query - from the Tomcat JDBC documentation:
(String) The SQL query that will be used to validate connections from this pool before returning them to the caller. If specified, this query does not have to return any data, it just can't throw a SQLException. The default value is null. If not specified, connections will be validation by the isValid() method. Example values are SELECT 1(mysql), select 1 from dual(oracle), SELECT 1(MS Sql Server)
You asked about using nested queries, different tables and multiple columns and so I give you something to think about:
The validationQuery
is executed every time an application requests a connection from the pool. This is before the application issues it's own query - so at a minimum you issue two queries against the database. With that in mind, why would you want to make the query more complex? All you would do is add unnecessary load on the database server for no reason at all - AFAIK you cannot see the output of the validationQuery
within a Java application because it happens at the container level before the connection is passed to the client.
Keep it simple and use one of the recommended queries.