2012-11-09 84 views
2

我是Java和企業開發新手,目前我正嘗試通過使用H2創建內存數據庫來實現一些結合了Hibernate的JUnit測試。Hibernate JDBCConnection無法打開與H2數據庫的連接錯誤

我發現了一個tutorial online處理這一點,並集成了一些這個代碼。我有一種感覺下面的代碼,負責特定的錯誤我看到:

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; 
    } 

我已經下載了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 

有誰有什麼可能會造成這個錯誤的想法?

這是我的第一篇文章,所以如果有人需要更多的信息或澄清,請讓我知道。

謝謝!

編輯:

這裏是堆棧跟蹤

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 
+0

您可以發佈完整的堆棧跟蹤? – jjathman

+0

你的JDBC URL必須以冒號結束:' 「的jdbc:H2:MEM:」'。 –

+0

我加結腸,但我仍然得到連接錯誤。 – Ericrius1

回答

0

貌似驅動程序沒有正確註冊。試着在你創建會話工廠方法的開始調用此權利:

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

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

我們使用H2單元測試,以及和做一些初始化前的準備工作調用後者 - 這會註冊驅動程序(隱式)。

+0

Hey PepperBob,我嘗試了兩種方法,並且在堆棧跟蹤中得到了同樣的錯誤:java.sql.SQLException:找不到適用於jdbc的驅動程序:h2:mem: – Ericrius1

+0

你確定圖書館在類路徑? – PepperBob

+0

是啊,我只是刪除它從構建路徑(它已經從那是從我的當前項目的不同文件夾中引用),複製到我的當前文件夾。然後重新添加jar來建立路徑來確保,但仍然有錯誤 – Ericrius1

0

既然你剛開始我建議首先簡單。用下面的主要方法創建一個類。如果你運行這個,並且你沒有看到系統打印出來,那麼H2驅動程序不在你的類路徑中。如果這可行,請了解如何將連接參數應用於Hibernate配置。

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

這聽起來像一個好主意史蒂夫。我只用這個main方法創建了一個單獨的類,當我嘗試將該類作爲java應用程序運行時,我得到「找不到主類:<程序包名> .class。程序將退出。我有一種感覺,我是缺少明顯的東西..但我不知道它是什麼... – Ericrius1

+0

聽起來像你還沒有編譯你的類如果你不使用IDE,你需要使用javac編譯.java文件到一個.class文件,然後你可以用java運行你的類 – MarkOfHall

相關問題