Can not obtain connection to DB using JPA - derby embedded driver class missing

Viewed 254

I am trying to create local derby database using jpa. As JPA implementation i am using openjpa and as sql implementation derby.

This is persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
    <persistence-unit name="DataLayer"
        transaction-type="RESOURCE_LOCAL">
        <non-jta-data-source>DataSource</non-jta-data-source>
        <properties>
            <property
                name="javax.persistence.schema-generation.database.action"
                value="create" />
            <property name="javax.persistence.jdbc.driver"
                value="org.apache.derby.jdbc.EmbeddedDriver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:derby:testdb" />
        </properties>
    </persistence-unit>
</persistence>

This is my main:

public static void main(String[] args)
{
    Persistence.generateSchema("DataLayer", null);
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("DataLayer");
    EntityManager manager = factory.createEntityManager(); // <- Exception here
}

This is starting part of exception i get:

Exception in thread "main" <openjpa-3.1.2-r66d2a72 nonfatal general error> org.a
pache.openjpa.persistence.PersistenceException: There were errors initializing y
our configuration: <openjpa-3.1.2-r66d2a72 fatal user error> org.apache.openjpa.
util.UserException: A connection could not be obtained for driver class "org.apa
che.derby.jdbc.EmbeddedDriver" and URL "jdbc:derby:testdb".  You may have spe
cified an invalid URL.
...

saying that i may have specified an invalid URL.

Complete stack trace: https://justpaste.it/3kmgo

Root exception: java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver

I have specified EmbeddedDriver in persistence.xml, also included Maven dependency.

Also i think my connection url and driver is correct according to docs about derby:

Dependencies included (using Maven):

<dependency>
    <groupId>org.apache.openjpa</groupId>
    <artifactId>openjpa</artifactId>
    <version>3.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <version>10.15.2.0</version>
</dependency>

I tried to change property javax.persistence.jdbc.url to jdbc:derby:testdb;create=true, but same exception. Also tried to set javax.persistence.schema-generation.database.action property to none.

2 Answers

Found out that org.apache.derby.jdbc.EmbeddedDriver class is not in derby artifactid dependency (it has derby and derbyshared jar's). It is in derbytools, so including derbytools to dependencies solve issue abbout missing EmbeddedDriver class:

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derbytools</artifactId>
    <version>10.15.2.0</version>
</dependency>

Using Debry 10.15.2.0 the Embedded driver is located in Derbytools.jar. Thus must be in your libraries and classpath. Created a new driver(I'd deleted any drivers that say anything Embedded). In the Services tab right click Drivers and Create New. Add Derbytools.jar Highlight it and click the Find Button. Give it a meaningful name and it works as expected. Just to be sure I also added Derby.jar and DerbyShared.jar to avoid problems.

Related