title: Hibernate JDBCConnection cannot open connection error with H2 database
body: <p>I am new to Java and Enterprise development, and currently I am trying to implement some JUnit tests incorporating <em>Hibernate</em> by creating an in-memory database using H2.</p>
  
  <p>I found a <a href="http://blog.schauderhaft.de/2011/03/13/testing-databases-with-junit-and-hibernate-part-1-one-to-rule-them/" rel="nofollow">tutorial online</a> 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: </p>
  
  <blockquote>
    <p>org.hibernate.exception.JDBCConnectionException: Cannot open connection</p>
  </blockquote>
  
  <pre><code> 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;  
      }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code> 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
  </code></pre>
  
  <p>Does anyone have an idea of what could be causing this error?</p>
  
  <p>This is my first post, so if anyone needs more information or clarification, please let me know.</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here is the stack trace</p>
  
  <pre><code>    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.&lt;init&gt;(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
  </code></pre>