java Hibernate JDBCConnection 无法打开与 H2 数据库的连接错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13314950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:15:41  来源:igfitidea点击:

Hibernate JDBCConnection cannot open connection error with H2 database

javahibernatejdbcjunit

提问by Ericrius1

I am new to Java and Enterprise development, and currently I am trying to implement some JUnit tests incorporating Hibernateby creating an in-memory database using H2.

我是 Java 和企业开发的新手,目前我正在尝试通过使用 H2 创建内存数据库来实现一些结合Hibernate的JUnit 测试。

I found a tutorial onlinedealing with this and have integrated some of this code. I have a feeling the following code that may be responsible for the specific error I am seeing:

我在网上找到了一个教程来处理这个问题,并集成了一些代码。我有一种感觉,以下代码可能是造成我所看到的特定错误的原因:

org.hibernate.exception.JDBCConnectionException: Cannot open connection

org.hibernate.exception.JDBCConnectionException:无法打开连接

 private SessionFactory createSessionFactory() 
    {
        AnnotationConfiguration configuration = new AnnotationConfiguration();
        configuration.addAnnotatedClass(Facility.class);
        configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        configuration.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
        configuration.setProperty("hibernate.connection.url", "jdbc:h2:mem");
        configuration.setProperty("hibernate.hbm2ddl.auto", "create");

        SessionFactory sessionFactory = configuration.buildSessionFactory();
        return sessionFactory;  
    }

I have downloaded H2 and added the JAR to my build path, and I know Hibernate is configured correctly besides this because when I run Junit tests using our already set up development database, the tests execute successfully without any connection errors. The configuration file for that is in a separate file called hibernate.properties, consisting of following:

我已经下载了 H2 并将 JAR 添加到我的构建路径中,我知道除此之外 Hibernate 配置正确,因为当我使用我们已经设置的开发数据库运行 Junit 测试时,测试成功执行,没有任何连接错误。其配置文件位于一个名为 hibernate.properties 的单独文件中,包含以下内容:

 hibernate.connection.datasource=java:/jdbc/mysql
 hibernate.dialect=org.hibernate.dialect.MySQLDialect
 hibernate.cache.use_second_level_cache=false
 hibernate.cache.use_query_cache=false

Does anyone have an idea of what could be causing this error?

有没有人知道可能导致此错误的原因?

This is my first post, so if anyone needs more information or clarification, please let me know.

这是我的第一篇文章,所以如果有人需要更多信息或澄清,请告诉我。

Thanks!

谢谢!

EDIT:

编辑:

Here is the stack trace

这是堆栈跟踪

    java.sql.SQLException: No suitable driver found for jdbc:h2:mem
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
    at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
    at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:252)
    at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:211)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:343)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    at com.cmt.test.SessionFactoryRule.createSessionFactory(SessionFactoryRule.java:73)
    at com.cmt.test.SessionFactoryRule.access
Class.forName("org.h2.Driver");
(SessionFactoryRule.java:64) at com.cmt.test.SessionFactoryRule.evaluate(SessionFactoryRule.java:26) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner.run(ParentRunner.java:193) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access
DriverManager.getConnection("jdbc:h2:mem:", "sa", "");
0(ParentRunner.java:42) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:24) at org.junit.runners.ParentRunner.run(ParentRunner.java:193) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access
public static void main(String[] a) throws Exception {
    Class.forName("org.h2.Driver");
    Connection conn = DriverManager.getConnection("jdbc:h2:mem:", "sa", "");
    System.out.println("woohoo!  connected.");
    conn.close();
}
0(ParentRunner.java:42) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:24) at org.junit.runners.ParentRunner.run(ParentRunner.java:193) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access##代码##0(ParentRunner.java:42) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at org.junit.runner.JUnitCore.run(JUnitCore.java:136) at org.junit.runner.JUnitCore.run(JUnitCore.java:117) at com.cmt.test.TestRunner.runTest(TestRunner.java:57) at com.cmt.test.bean.TestBean.runTestCase(TestBean.java:73) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.el.parser.AstValue.invoke(AstValue.java:170) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153) at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769) at javax.faces.component.UICommand.broadcast(UICommand.java:300) at javax.faces.component.UIData.broadcast(UIData.java:1092) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:791) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1256) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190) at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92) at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126) at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:383) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Unknown Source) 14:12:12,044 WARN [JDBCExceptionReporter] SQL Error: 0, SQLState: 08001 14:12:12,044 ERROR [JDBCExceptionReporter] No suitable driver found for jdbc:h2:mem 14:12:12,046 INFO [STDOUT] DEBUG - Failure : Cannot open connection CreateFacility(com.cmt.test.memory.InMemoryTest) org.hibernate.exception.JDBCConnectionException: Cannot open connection at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52) at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449) at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167) at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142) at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85) at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1353) at com.cmt.test.SessionFactoryRule.beginTransaction(SessionFactoryRule.java:89) at com.cmt.test.SessionFactoryRule.evaluate(SessionFactoryRule.java:28) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner.run(ParentRunner.java:193) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access##代码##0(ParentRunner.java:42) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:24) at org.junit.runners.ParentRunner.run(ParentRunner.java:193) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access##代码##0(ParentRunner.java:42) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:24) at org.junit.runners.ParentRunner.run(ParentRunner.java:193) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access##代码##0(ParentRunner.java:42) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at org.junit.runner.JUnitCore.run(JUnitCore.java:136) at org.junit.runner.JUnitCore.run(JUnitCore.java:117) at com.cmt.test.TestRunner.runTest(TestRunner.java:57) at com.cmt.test.bean.TestBean.runTestCase(TestBean.java:73) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.el.parser.AstValue.invoke(AstValue.java:170) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153) at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769) at javax.faces.component.UICommand.broadcast(UICommand.java:300) at javax.faces.component.UIData.broadcast(UIData.java:1092) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:791) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1256) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190) at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92) at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126) at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:383) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Unknown Source) Caused by: java.sql.SQLException: No suitable driver found for jdbc:h2:mem at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133) at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446) ... 74 more CreateFacility(com.cmt.test.memory.InMemoryTest) Cannot open connection org.hibernate.exception.JDBCConnectionException: Cannot open connection

采纳答案by PepperBob

Looks like the driver is not registered properly. Try calling this right at the beginning of your create session factory method:

看起来驱动程序没有正确注册。尝试在创建会话工厂方法的开头调用它:

##代码##

or

或者

##代码##

We're using H2 for unit tests as well and doing some pre-init work calling the latter - this registers the Driver (implicitly).

我们也使用 H2 进行单元测试,并做一些调用后者的预初始化工作 - 这会注册驱动程序(隐式)。

回答by MarkOfHall

Since you're just starting out I suggest starting simple. Create a class with a main method like below. If you run this and you don't see the system out print, then the H2 driver isn't on your classpath. If this works, understand how to apply the connection parameters to the Hibernate config.

由于您刚刚开始,我建议从简单开始。使用如下所示的主要方法创建一个类。如果您运行它并且没有看到系统输出,那么 H2 驱动程序不在您的类路径中。如果可行,请了解如何将连接参数应用于 Hibernate 配置。

##代码##