错误:
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
命令
% 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)
答案 0 :(得分:2)
哦,是的,我忽略了标题。
您正尝试使用独立服务器的驱动程序连接到嵌入式 Derby。服务器未运行,因此出错。
当然必须更改此属性:
<property name="URL" value="jdbc:derby:path_to_database;create=true" />
驱动程序必须是:org.apache.derby.jdbc.EmbeddedDriver
此外,在嵌入式上实现连接池没有意义,因为在此模式下只能有一个连接。
答案 1 :(得分:2)
权限可能很难处理。 derby在/usr/share/javadb/bin
下运行,运行intelliJ的用户不是root用户,因此没有对该文件夹的写访问权限。因此,要解决此问题,我必须向运行intelliJ的用户授予对该文件夹的写访问权限。这样就成功创建了数据库。我仍然希望错误提供更多信息。任何与Permission
相关的东西都会更早地给我这个暗示。