使用Glassfish连接到h2数据库

问题描述 投票:0回答:3

我正在使用 Glassfish 应用程序服务器开发一个网络应用程序。 我已经安装了 H2 DB,现在尝试在 glassfish 中定义连接池,但在尝试 ping 数据库时失败。

这就是我所做的:

  1. 安装了H2 DB并创建了一个新的DB(服务器模式)。
  2. 我在 pom.xml 中添加了 H2 maven 依赖项:

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.166</version>
    </dependency>
    
  3. 将 H2 jar 文件 (h2-1.3.166.jar) 复制到我的 glassfish 库:C:\glassfish3\glassfish\lib
  4. 登录 glassfish 管理 gui 并创建以下 jdbc 连接:
    • 资源类型:javax.sql.DataSource
    • 数据库驱动程序供应商:H2
    • 数据源类名:org.h2.jdbcx.JdbcDataSource

我的“附加属性”是:

  • 用户:my_db_user
  • 密码:my_db_password
  • url:jdbc:h2:tcp://localhost/~/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)

我错过了什么吗? 如有任何帮助,我们将不胜感激!

jdbc glassfish-3 h2
3个回答
6
投票

ClassNotFoundException: org.h2.jdbcx.JdbcDataSource
表示 H2 JDBC 驱动程序不在类路径中。我不太了解 Glassfish,但它似乎不在类路径中。其他数据库驱动似乎也出现了类似的问题,例如MySQL(需要添加.jar文件
/glassfish/domains/[domainname]/lib/ext

您需要使用不同的数据库 URL:

jdbc:h2:~/my_db_name;AUTO_SERVER=TRUE

如文档中所述,自动混合模式不需要也不使用服务器 URL(以

jdbc:h2:tcp://
开头的 URL)


0
投票

在服务器安装文件夹的以下路径中添加 h2 的驱动程序 jar 文件:

glassfish/domains/domain1/lib/ext

0
投票

使用 Payara-Enterprise 5.62.0,我必须执行以下操作来更新用于构建基础映像的 Payara-Enterprise Dockerfile 中的 H2 驱动程序:

...

ENV PAYARA_DIR=/opt/payara
ENV H2_DRIVER_JAR_VERSION=2.2.224

RUN rm ${PAYARA_DIR}/h2db/bin/h2.jar

curl -Lk -s -o ${PAYARA_DIR}/h2db/bin/h2.jar  \
          https://repo1.maven.org/maven2/com/h2database/h2/2.2.224/h2-${H2_DRIVER_JAR_VERSION}.jar
...

在构建基础镜像时使用

asadmin add-library --type ext
命令由于任何原因都没有在运行时提供更新的H2版本。

payara 和 asadmin add-library 命令中类加载顺序的来源:https://docs.payara.fish/community/docs/Technical%20Documentation/Payara%20Server%20Documentation/Server%20Configuration%20And%20Management/Classloading/Standard %20Classloading.html

https://docs.payara.fish/community/docs/documentation/payara-server/asadmin-commands/misc-commands.html

© www.soinside.com 2019 - 2024. All rights reserved.