Tomcat 没有找到 H2 内存数据库

Tomcat does not find H2 in-memory database

出于测试目的,我尝试将休眠与 h2 内存数据库结合使用。我正在使用 Maven 进行依赖管理。 Tomcat 似乎没有找到 h2 数据库驱动程序 - 然而,通过 maven 添加 postgresql,启动本地 postgresql-daemon 并连接到它工作正常。

我还能够 运行 针对 h2 内存数据库进行一些简单的 JUnit 测试(没有 tomcat)。

我的初始化代码(在代码中而不是 xml 以排除那里的任何错误):

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();

如果我 运行 使用 H2 初始化针对此 class 进行单元测试,一切正常。在 tomcat 上部署后,显示以下错误:

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.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)
[...]

完整错误显示在 this page

两个库(h2 和 psql)都是通过 Maven 安装的,都没有范围(默认为 "compile")。我在正确的 tomcat 服务器上,我正在正确部署(因为 postgresql 确实有效),tomcat 和 java 使用相同的 JRE8 运行time 环境。其他注册的 web 服务(主要项目使用 JAX)没有按预期工作的数据库依赖性。

我没有想法 - 非常感谢任何帮助。

您正在为 postgresql 设置驱动程序 class 而不是为 h2

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