2012-05-19 142 views
2

我正在開發一個使用Glassfish應用程序服務器的Web應用程序。 我已經安裝了H2數據庫,現在試圖在glassfish中定義一個連接池,但是當試圖ping數據庫時,它會失敗。使用Glassfish連接到h2數據庫

這裏是我做過什麼:

  1. 安裝H2 DB並創建一個新的DB(服務器模式)。
  2. 我增加了一個H2行家依賴於我的pom.xml:

    <dependency> 
        <groupId>com.h2database</groupId> 
        <artifactId>h2</artifactId> 
        <version>1.3.166</version> 
    </dependency> 
    
  3. 複製H2 jar文件(h2-1.3.166.jar)我GlassFish的lib目錄下:C:\ glassfish3 \ GlassFish的\ lib中
  4. Loggd到GlassFish管理GUI和創建以下JDBC連接:
    • 資源類型:javax.sql.DataSource中
    • 數據庫驅動程序供應商:H2
    • 數據源類名:org.h2.jdbcx.J dbcDataSource

我的 「附加屬性」 是:

  • 用戶:my_db_user
  • 密碼:my_db_password
  • 網址爲:jdbc:H2:TCP://本地主機/〜/ my_db_name ; AUTO_SERVER = TRUE

當我嘗試ping此連接時失敗,消息:

Ping連接池myCpool失敗。類名稱錯誤或類路徑未設置爲:org.h2.jdbcx.JdbcDataSource請檢查server.log以獲取更多詳細信息。

完整的日誌說:

Wrong class name or classpath for Datasource Object java.lang.ClassNotFoundException: org.h2.jdbcx.JdbcDataSource at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at com.sun.gjc.common.DataSourceObjectBuilder.getDataSourceObject(DataSourceObjectBuilder.java:285) at com.sun.gjc.common.DataSourceObjectBuilder.constructDataSourceObject(DataSourceObjectBuilder.java:114) at com.sun.gjc.spi.ManagedConnectionFactory.getDataSource(ManagedConnectionFactory.java:1307) at com.sun.gjc.spi.DSManagedConnectionFactory.getDataSource(DSManagedConnectionFactory.java:163) at com.sun.gjc.spi.DSManagedConnectionFactory.createManagedConnection(DSManagedConnectionFactory.java:102) at com.sun.enterprise.connectors.service.ConnectorConnectionPoolAdminServiceImpl.getUnpooledConnection(ConnectorConnectionPoolAdminServiceImpl.java:697) at com.sun.enterprise.connectors.service.ConnectorConnectionPoolAdminServiceImpl.testConnectionPool(ConnectorConnectionPoolAdminServiceImpl.java:426) at com.sun.enterprise.connectors.ConnectorRuntime.pingConnectionPool(ConnectorRuntime.java:1086) at org.glassfish.connectors.admin.cli.PingConnectionPool.execute(PingConnectionPool.java:130) at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:348) at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:363) at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1085) at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:95) at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1291) at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1259) at org.glassfish.admin.rest.ResourceUtil.runCommand(ResourceUtil.java:214) at org.glassfish.admin.rest.resources.TemplateExecCommand.executeCommand(TemplateExecCommand.java:127) at org.glassfish.admin.rest.resources.TemplateCommandGetResource.processGet(TemplateCommandGetResource.java:78) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60) at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205) at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288) at com.sun.jersey.server.impl.uri.rules.SubLocatorRule.accept(SubLocatorRule.java:134) at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) at com.sun.jersey.server.impl.uri.rules.SubLocatorRule.accept(SubLocatorRule.java:134) at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339) at com.sun.jersey.server.impl.container.grizzly.GrizzlyContainer._service(GrizzlyContainer.java:182) at com.sun.jersey.server.impl.container.grizzly.GrizzlyContainer.service(GrizzlyContainer.java:147) at org.glassfish.admin.rest.adapter.RestAdapter.service(RestAdapter.java:148) at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179) at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117) at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:722)

我錯過了什麼? 任何幫助表示讚賞!

回答

4

ClassNotFoundException: org.h2.jdbcx.JdbcDataSource表示H2 JDBC驅動程序不在類路徑中。我不太瞭解Glassfish,但似乎它不在classpath中。這似乎發生類似的問題與其他數據庫驅動程序,for example MySQL(.jar文件需要被添加到/glassfish/domains/[domainname]/lib/ext

您需要使用不同的數據庫URL:

jdbc:h2:~/my_db_name;AUTO_SERVER=TRUE 

As described in the documentation, the automatic mixed mode不需要或工作服務器的URL(網址開始jdbc:h2:tcp://

+0

謝謝。 AUTO_SERVER = TRUE是我添加的一部分,作爲一部分我嘗試使它工作的一百萬個鏡頭。沒有它也是一樣(但是你的評論被採納了,我認爲更好)。關於ClassNotFoundException - 我添加了H2 jar(使用maven),我可以確定看到我的Maven Dependencies文件夾下的jar(所以它應該在我認爲的classpath下可用)。這個jar不應該包含驅動程序嗎?當應用程序服務器(本例中爲glassfish)指向classpath時,它是否指向與我的web應用程序類路徑相同的類路徑? – forhas

+0

jar文件包含驅動程序。只是,我不知道如何將該jar文件添加到Glassfish類路徑中...我發現了一個類似的問題(使用解決方案),我更新了我的答案 - 請參閱http://stackoverflow.com/questions/8349970/connecting- a-mysql-database-to-glassfish-classpath-is-set-or-classname-is-wr - 但我不確定這是否能解決問題。但是,您可能會發現其他(StackOverflow)問題和答案。 –

+0

您向我提供的鏈接已解決問題!看起來我把jar放在\ glassfish3 \ glassfish \ lib下,但這是一個錯誤的位置。正確的位置,如您在我提供的鏈接中提到的:/ glassfish/domains/[domainname]/lib/ext。謝謝!!! :) – forhas

0

添加驅動程序jar文件H2在以下路徑在服務器安裝文件夾:

glassfish/domains/domain1/lib/ext