java - Tomcat 找不到 H2 内存数据库

标签 java hibernate maven tomcat h2

出于测试目的,我尝试将 hibernate 与 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 初始化针对此类运行单元测试,一切正常。在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$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)
[...]

完整错误显示在 this page 上.

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

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

最佳答案

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

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

关于java - Tomcat 找不到 H2 内存数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35635249/

相关文章:

hibernate - withTransaction 并保存大量对象?

java - 在新的 Eclipse 安装中重新索引本地 Maven Repo 失败并出现 java.nio.channels.OverlappingFileLockException

java - 从 Maven 文件夹结构的主/资源文件夹中选取文件时出现问题

Eclipse: "declared package does not match the expected package",但 buildpath 确实定义了声明的包

java - samza 容器失败

java - 如何正确实现 Google Play 结算库

java - 使用局部变量还是全局变量更好

Java - 从 JSONObject 中检索变量类型的值

hibernate 问题 - "Use of @OneToMany or @ManyToMany targeting an unmapped class"

java - 大对象不能在自动提交模式下使用