无法使用 hibernate 连接到 glassfish 中的嵌入式 derby 数据库
错误:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哦,对了,我忽略了标题。
您正在尝试使用独立服务器的驱动程序连接到嵌入式 Derby。服务器未运行,因此出现错误。
该属性当然必须更改:
并且驱动程序必须是:
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:
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.
权限可能很难处理。 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 toPermission
would have given me this hint much earlier.