【问题标题】:Not able to connect to embedded derby database in glassfish with hibernate无法使用休眠连接到 glassfish 中的嵌入式 derby 数据库
【发布时间】:2012-01-06 10:22:25
【问题描述】:

错误:

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都可以找到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>

完整的源码,包括pom.xml,位于here

我使用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)

【问题讨论】:

  • 从“connectionAttributes”中删除前导分号有帮助吗? “创建=真”。
  • 注意到了。认为这是源代码中的拼写错误,但没有......这没有帮助。以后我会更正sn-p。

标签: hibernate jakarta-ee jpa glassfish-3 derby


【解决方案1】:

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

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

这个属性当然必须改变:

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

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

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

【讨论】:

  • 我确实在端口 1527 上从外部启动了服务器。当该服务器未运行时,我得到完全不同的错误,说它无法连接到服务器。
  • 将 domain.xml 中的 datasource-classname="org.apache.derby.jdbc.EmbeddedDriver" 和 URL 属性更改为 jdbc:derby://localhost:1527/chapter2;create=true 仍然会获取相同的错误
  • 您是否能够使用该连接 URL 并使用 Derby "ij" 工具连接到数据库?
【解决方案2】:

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

【讨论】:

  • +1,感谢您的跟进。您让我感到困惑,我的回答对您的情况无效;毕竟你没有运行嵌入式数据库。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
  • 2017-09-28
  • 1970-01-01
  • 2012-08-27
  • 2020-06-24
  • 2012-08-04
  • 2015-06-20
相关资源
最近更新 更多