Hibernate JDBCConnection cannot open connection error with H2 database

13,360

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

Class.forName("org.h2.Driver");

or

DriverManager.getConnection("jdbc:h2:mem:", "sa", "");

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

Share:
13,360
Ericrius1
Author by

Ericrius1

Updated on June 04, 2022

Comments

  • Ericrius1
    Ericrius1 almost 2 years

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

    I found a tutorial online dealing 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

     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:

     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$0(SessionFactoryRule.java:64)
        at com.cmt.test.SessionFactoryRule$1.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$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.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$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.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$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.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$1.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$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.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$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.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$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.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
    
  • Ericrius1
    Ericrius1 over 11 years
    Hey PepperBob, I tried both approaches and in the stack trace got the same error: java.sql.SQLException: No suitable driver found for jdbc:h2:mem:
  • PepperBob
    PepperBob over 11 years
    Are you sure that the library is on the classpath?
  • Ericrius1
    Ericrius1 over 11 years
    That sounds like a good idea Steve. I made a separate class with only that main method, and when I try to run the class as a java application, I get "Could not find the main class: <package_name>.class. Program will exit. I have a feeling I am missing something obvious.. but I can't figure out what it is...
  • Ericrius1
    Ericrius1 over 11 years
    Yeah, I just removed it from build path(it had been referenced from a folder that was different from my current project's), copied into my current folder. then re-added jar to build path to make sure, but still got error
  • MarkOfHall
    MarkOfHall over 11 years
    Sounds like you haven't compiled your class. If you aren't using an IDE, you need to use javac to compile the .java file into a .class file, then you can run your class with java.
  • Ericrius1
    Ericrius1 over 11 years
    Thanks so much PepperBob! I added both lines you suggested, and after cleaning the server, together they work