无法使用 hibernate 连接到 glassfish 中的嵌入式 derby 数据库

发布于 2024-12-18 01:48:41 字数 2714 浏览 7 评论 0原文

错误:

Connection could not be allocated because: 
The connection was refused because the database chapter2 was not found.

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
   xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
      http://java.sun.com/xml/ns/persistence
      http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
   <persistence-unit name="users" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>jdbc/chapter2</jta-data-source>
      <properties>
         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
         <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
      </properties>
   </persistence-unit>
</persistence>

Domain.xml:可以找到整个 domain.xml 此处

<resources>
    <jdbc-resource pool-name="Chapter2Pool" description="Chapter 2 Datasource" jndi-name="jdbc/chapter2" />
    <jdbc-connection-pool datasource-classname="org.apache.derby.jdbc.ClientDataSource" res-type="javax.sql.DataSource" name="Chapter2Pool">
        <property name="DatabaseName" value="chapter2" />
        <property name="Password" value="password" />
        <property name="PortNumber" value="1527" />
        <property name="ServerName" value="localhost" />
        <property name="User" value="admin" />
        <property name="URL" value="jdbc:derby://localhost:1527/chapter2" />
        <property name="connectionAttributes" value="create=true" />
    </jdbc-connection-pool>
</resources>
<servers>
        <server name="server" config-ref="server-config">
            <application-ref ref="__admingui" virtual-servers="__asadmin" />
            <resource-ref ref="jdbc/__TimerPool" />
            <resource-ref ref="jdbc/__default" />
            <resource-ref ref="jdbc/chapter2" />
        </server>
</servers>

完整源代码(包括 pom.xml)位于 此处

我使用 setNetworkServerCP 命令在外部启动了 derby 服务器

 % sudo lsof -i :1527
COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    3369 bhaarat   27u  IPv6  29472      0t0  TCP localhost:1527 (LISTEN)

Error:

Connection could not be allocated because: 
The connection was refused because the database chapter2 was not found.

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
   xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
      http://java.sun.com/xml/ns/persistence
      http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
   <persistence-unit name="users" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>jdbc/chapter2</jta-data-source>
      <properties>
         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
         <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
      </properties>
   </persistence-unit>
</persistence>

Domain.xml: entire domain.xml can be found here

<resources>
    <jdbc-resource pool-name="Chapter2Pool" description="Chapter 2 Datasource" jndi-name="jdbc/chapter2" />
    <jdbc-connection-pool datasource-classname="org.apache.derby.jdbc.ClientDataSource" res-type="javax.sql.DataSource" name="Chapter2Pool">
        <property name="DatabaseName" value="chapter2" />
        <property name="Password" value="password" />
        <property name="PortNumber" value="1527" />
        <property name="ServerName" value="localhost" />
        <property name="User" value="admin" />
        <property name="URL" value="jdbc:derby://localhost:1527/chapter2" />
        <property name="connectionAttributes" value="create=true" />
    </jdbc-connection-pool>
</resources>
<servers>
        <server name="server" config-ref="server-config">
            <application-ref ref="__admingui" virtual-servers="__asadmin" />
            <resource-ref ref="jdbc/__TimerPool" />
            <resource-ref ref="jdbc/__default" />
            <resource-ref ref="jdbc/chapter2" />
        </server>
</servers>

complete source, including pom.xml, is located here

I started the derby server outside using setNetworkServerCP command

 % sudo lsof -i :1527
COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    3369 bhaarat   27u  IPv6  29472      0t0  TCP localhost:1527 (LISTEN)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悸初 2024-12-25 01:48:41

哦,对了,我忽略了标题。

您正在尝试使用独立服务器的驱动程序连接到嵌入式 Derby。服务器未运行,因此出现错误。

该属性当然必须更改:

<property name="URL" value="jdbc:derby:path_to_database;create=true" />

并且驱动程序必须是:org.apache.derby.jdbc.EmbeddedDriver

另外,在嵌入式上实现连接池没有意义,因为在嵌入式设备上只能有一个连接这种模式。

Oh, right, I overlooked the title.

You're trying to connect to an embedded Derby using drivers for a standalone server. The server is not running, hence the error.

This property certainly must be changed:

<property name="URL" value="jdbc:derby:path_to_database;create=true" />

And the driver must be: org.apache.derby.jdbc.EmbeddedDriver

Also, implementing the connection pool on embedded does not make sense, since there can be only one connection in this mode.

北斗星光 2024-12-25 01:48:41

权限可能很难处理。 derby 在 /usr/share/javadb/bin 下运行,并且运行 intelliJ 的用户不是 root,因此没有对该文件夹的写访问权限。因此,为了解决这个问题,我必须向运行 intelliJ 的用户授予对该文件夹的写访问权限。这样数据库就创建成功了。我仍然希望该错误能够提供更多信息。任何与Permission相关的东西都会更早地给我这个提示。

permissions can be hard to deal with. the derby was running under /usr/share/javadb/bin and the user running intelliJ was not root and hence did not have write access to that folder. So to resolve the issue I had to give write access to that folder to the user that ran intelliJ. This way the database was successfully created. I still wish that the error was more informative. anything related to Permission would have given me this hint much earlier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文