2012-11-09 82 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

相关问题