Lars
Lars

Reputation: 5799

Tomcat does not find H2 in-memory database

For testing purposes, I'm trying to use hibernate with an h2 in-memory database. I'm using Maven for dependency management. Tomcat does not seem to find the h2 database driver - however, adding postgresql via maven, starting a local postgresql-daemon and connecting to it works just fine.

I am also able to run some simple JUnit-Tests (without tomcat) against the h2 in-memory database.

My initialization code (in code instead of an xml to rule out any errors there):

Properties props = new Properties();

// h2 in-memory
props.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
props.setProperty("hibernate.connection.url", "jdbc:h2:mem:test");

// postgresql
props.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
props.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
props.setProperty("hibernate.connection.username", "admin");
props.setProperty("hibernate.connection.password", "...password...");
props.setProperty("hibernate.connection.url", "jdbc:postgresql://localhost:5432/glmtest");

// Common Options
props.setProperty("hibernate.connection_pool_size", "1");
props.setProperty("hibernate.hbm2ddl.auto", "create");
props.setProperty("hibernate.show_sql", "true");

sessionFactory =  
        new Configuration()
        .addProperties(props)
        .addAnnotatedClass( AnEntity.class )
        .buildSessionFactory();

If I run a unit-test against this class with the H2 initialization, everything works. Upon deployment on the tomcat, the following error is shown:

org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
[...]

org.hibernate.exception.JDBCConnectionException: Error calling DriverManager#getConnection
org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:115)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:101)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:123)
[...]

java.sql.SQLException: No suitable driver found for jdbc:h2:mem:test
java.sql.DriverManager.getConnection(DriverManager.java:689)
java.sql.DriverManager.getConnection(DriverManager.java:208)
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionCreator.makeConnection(DriverManagerConnectionCreator.java:34)
[...]

Full error is shown on this page.

Both libraries (h2 and psql) are installed via Maven, both without a scope (defaulting to "compile"). I am on the right tomcat server, I am deploying correctly (since postgresql does work), tomcat and java are using the same JRE8 runtime environment. Other registered webservices (main project uses JAX) without database-dependency work as expected.

I'm out of ideas - any help much appreciated.

Upvotes: 0

Views: 1184

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36103

You are setting the Driver class for posgresql but not for h2

props.setProperty("hibernate.connection.driver_class", "org.h2.Driver");

Upvotes: 3

Related Questions