Regardless of the shells and the specific implementation of Hibernate / Spring, etc., you can check not the DataSource, but the database type (and this can be convenient).
The idea is to use DatabaseMetaData and type check against it (since Hibernate detects a dialect):
private boolean isTestDb(Session session) { return session.doReturningWork(new ReturningWork<Boolean>() { @Override public Boolean execute(Connection connection) throws SQLException { DatabaseMetaData metaData = connection.getMetaData(); return metaData.getDatabaseProductName().startsWith("HSQL"); } }); }
Note that the body of the method can be changed the way you want (check the JDBC URL, check the driver name, check almost everything).
Edit : The above approach works for 3.5+ sleep mode.
For an earlier version of Hibernate (e.g. 3.2) this might be even simpler:
private boolean isTestDb(Session session) { Conection connection = session.connection();//deprecated method, which was dumped in hibernate 3.5+ DatabaseMetaData metaData = connection.getMetaData(); return metaData.getDatabaseProductName().startsWith("HSQL"); }
source share