Reputation: 939
I have a small test app with JPA, Hibernate persisting to Oracle XE. This all works fine.
However, when the classpath includes derby.jar, I get an exception, although I'm not specifically calling anything in derby... that I know of.
Apparently, Derby is trying to load. But I am not calling any Derby initializations nor is Derby configured for JPA. According to the Derby docs, "In an embedded environment, loading the driver also starts Derby."
But I am not requesting anything from Derby. I removed the derby persistence-unit from the persistence.xml. Yet, with derby.jar remaining on the path, it blows up. If I remove derby.jar from the path, the persistence-unit defined for Oracle works fine.
I have found nothing relevant in the Derby docs or online.
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.apache.derby.jdbc.AutoloadedDriver40
The reason I have derby.jar on the classpath is that I am trying to define a persistence.xml with two declarations, Oracle and Derby, so that I could test the two databases in one test harness to demonstrate compatibility for a forthcoming project that will use Oracle in-house and Derby on portable machines.
PERSISTENCE.XML
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="persistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.gdeb.swift.NIIN</class>
<class>com.gdeb.swift.RIC</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="javax.persistence.jdbc.user" value="swift" />
<property name="javax.persistence.jdbc.password" value="swift" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="oracle.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.max_fetch_depth" value="3" />
</properties>
</persistence-unit>
</persistence>
Upvotes: 1
Views: 1925
Reputation: 81
I ran into this problem also. Issue was that there was an older derby.jar in my classpath ahead of the new one. I just had to remove the old one.
I wrote a simple program to do 'Class.forName(org.apache.derby.jdbc.AutoloadedDriver40)' and it failed with a "java.lang.SecurityException: sealing violation" which implied that it was finding classes from the same package in two different JARs (presumably the older and newer derby.jar files) which the Security Manager does not allow.
Upvotes: 1
Reputation: 16359
The automatic loading of the Derby JDBC driver is due to a JDBC 4.0 feature called "JDBC Driver Auto-loading". See http://onjava.com/onjava/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html and http://docs.oracle.com/javadb/10.6.1.0/ref/rrefjdbc4_0summary.html for what little doc there is of this feature.
The idea is that you don't have to do a 'Class.forName()' to load your driver and register it with DriverManager; that now happens automatically.
But it generally doesn't cause any problems; I'm surprised you're getting an exception here.
What is the full stack trace of the exception? Can you cut and paste the entire thing into this question?
There's no trivial way to disable JDBC driver auto-loading; it would be better to figure out why it isn't working in your case, and adjust things so that the auto-loading doesn't throw an exception.
Upvotes: 0