Omnipresent
Omnipresent

Reputation: 30424

Not able to connect to embedded derby database in glassfish with hibernate

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)

Upvotes: 2

Views: 8228

Answers (2)

Omnipresent
Omnipresent

Reputation: 30424

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.

Upvotes: 2

MaDa
MaDa

Reputation: 10762

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.

Upvotes: 2

Related Questions