【问题标题】:Tomcat does not find H2 in-memory databaseTomcat 找不到 H2 内存数据库
【发布时间】:2016-06-08 16:42:58
【问题描述】:

出于测试目的,我正在尝试将休眠与 h2 内存数据库一起使用。我正在使用 Maven 进行依赖管理。 Tomcat 似乎没有找到 h2 数据库驱动程序 - 但是,通过 maven 添加 postgresql,启动本地 postgresql-daemon 并连接到它就可以了。

我还能够针对 h2 内存数据库运行一些简单的 JUnit-Tests(没有 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)没有数据库依赖,按预期工作。

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

【问题讨论】:

    标签: java hibernate maven tomcat h2


    【解决方案1】:

    您正在为 posgresql 设置 Driver 类,但没有为 h2 设置驱动程序类

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

    【讨论】:

    • 成功了 - 谢谢!但是为什么单元测试在 JUnit 中以完全相同的配置运行(没有显式设置 driver_class 属性)?
    猜你喜欢
    • 2011-08-11
    • 2016-08-07
    • 2017-06-16
    • 2015-03-27
    • 2022-11-21
    • 2021-07-22
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多