Access to Derby database from another project using EclipseLink - jpa

I have a dynamic web project that creates and writes Derby database. Everything works fine. Here is the persistence.xml I used:
<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"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="alerts" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>test.Alert</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:databases/bDb;create=true" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="user-pwd" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.logging.level.sql" value="FINEST" />
</properties>
</persistence-unit>
Now I would like to create a java project that reads the same database. So I use the same persistence.xml (except that I change "create=true" to "create=false" since I don't want to create a new table). but I got this error:
[EL Severe]: ejb: 2014-12-28 22:27:20.379--ServerSession(1488953836)--Thread(Thread[DefaultQuartzScheduler_Worker-7,5,main])--Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Database 'databases/bDb' not found.
Error Code: 40000
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:326)
at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:138)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:204)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:741)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:239)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:685)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:304)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:336)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:302)
If I use "create=true", it will create a new database instance (it is different from the one I created from the web project). How do I fix this problem? Thanks a lot!

Since you're using a relative filename in your jdbc.url (databases/bDb), Derby is looking for the database relative to your application start directory.
And your two separate applications have two separate directories in which they are being run.
You could address this in a number of ways:
Arrange for each application to be run from the same directory
Use an absolute database path name, not a relative one, in your jdb.url value
Use the Derby Network Server configuration, not the Derby Embedded configuration
And there are undoubtedly other approaches.

Related

OpenJPA Exception related to JDBC Driver and connection URL in TomEE+

We are facing issue in our application with respect to the persistence configuration. The application runs on TomEE Plus (7.0) and uses Open JPA implementation. Below is the error which we are getting
<openjpa-2.4.2-r422266:1777108 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: There were errors initializing your configuration: <openjpa-2.4.2-r422266:1777108 fatal user error> org.apache.openjpa.util.UserException: A connection could not be obtained for driver class "oracle.jdbc.xa.client.OracleXADataSource" and URL "jdbc:oracle:thin:#//mydburl.abc.com:1881/MYSID.ABC.COM". You may have specified an invalid URL.
We dont' get the above error when using oracle.jdbc.driver.OracleDriver. The XA driver settings were working fine on IBM WebSphere 8.5. Is there anything else which we need to configure in TomEE+ to get it working?
Update
Adding the persistence.xml file as well
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="MySchema" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>myjndi</jta-data-source>
<class>com.abc.jpa.entity.Tuser</class>
<class>com.abc.jpa.entity.Taddress</class>
<properties>
<property name="tomee.jpa.factory.lazy" value="true"></property>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.xa.client.OracleXADataSource" />
<property name="javax.persistence.jdbc.url"
value="jdbc:oracle:thin:#//mydburl.abc.com:1881/MYSID.ABC.COM" />
<property name="javax.persistence.jdbc.user" value="userName" />
<property name="javax.persistence.jdbc.password" value="password" />
</properties>
</persistence-unit>
</persistence>
As mentioned, it works fine if I use oracle.jdbc.driver.OracleDriver as the jdbc driver class

Integrate BoneCP and EclipseLink with LOCAL_RESOURCE transaction-type

I am setting up a minimal working example where I use EclipseLink in a Java EE (JSF) project. My example works perfectly with the following configuration and code:
persistence.xml
<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="issat_PU_Local" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>pro.entity.Utilisateur</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/issat" />
<property name="javax.persistence.jdbc.user" value="java" />
<property name="javax.persistence.jdbc.password" value="JaVa" />
<property name="eclipselink.logging.level.sql" value="FINE" />
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>
Since I don't want to use JTA I am using RESOURCE_LOCAL as transaction type and perfectly leaving with "manually" handling transaction inside my DAO.
I now want to use BoneCP connection pool with my example.
How should I precisely modify my persistence.xml to do that? I found many configuration examples in the internet using also Hibernate, but I want to stay with EclipseLink.
I couldn't do it with bonecp, but I could get a pool connection with C3PO.
I followed this link : http://javabeginnerstutorial.com/hibernate/connection-pooling-with-hibernate-4/
I copied the dependency and the properties but, instead of <property name="hibernate.c3p0.min_size">1</property> put the value in the tag like this<property name="hibernate.c3p0.min_size" value="1"/>, otherwise I got an error.
Finally mi persitence.xml look this way:

Error No Script Specified when using JPA Persistence Schema Generation

Running on Wildly 8.2.0.Final. I get the following error when trying to generate a database schema from a script.
Caused by: javax.persistence.PersistenceException: Schema generation configuration indicated to include CREATE scripts, but no script was specified
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
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_1.xsd">
<persistence-unit name="com.mycompany_mavenFlowChartDB5_war_1.0-SNAPSHOTPU" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/postgresql</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name=""/>
<property name="javax.persistence.schema-generation.create-source" value="script"/>
<property name="javax.persistence.schema-generation.drop-source" value="script"/>
<property name="javax.persistence.schema-generation.scripts.action" value="none"/>
<property name="javax.persistence.schema-generation.scripts.drop-target" value="META-INF/drop-script.sql"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="META-INF/create-script.sql"/>
</properties>
</persistence-unit>
</persistence>
Where the create script is located:
/src/main/resources/META-INF/create-script.sql
Thanks in advance!
The Wildfly persistence schema generator gui creates incorrect property names for the drop and create targets.
The correct property names are
<property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/drop-script.sql"/>
<property name="javax.persistence.schema-generation.create-script-source" value="META-INF/create-script.sql"/>
change create-script to create-script-source and drop-script to drop-script-source
source: https://github.com/hantsy/ee7-sandbox

Database creation not working in eclipselink with glassfish 4 and Postgres

I want to create the database based on Entities. Configuration:
Glassfish: GlassFish Server Open Source Edition 4.1 (build 13)
Eclipselink: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd (delivered with glassfish)
Database: PostgreSQL Version: 9.4.2
Driver: PostgreSQL Native Driver Version: PostgreSQL 9.4 JDBC4.1 (build 1201)
From the moment eclipselink starts creating the database I see the following statements in the logs after putting a lot of log parameters on finest:
SELECT ID FROM table_name WHERE ID <> ID
SELECT 1
This is repeated 4-5 times. The first query gives the following obvious postgres error:
org.postgresql.util.PSQLException: ERROR: relation "table_name" does not exist
Position: 16
As a result a following queries give the following error:
org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block
After this eclipselink continues creating the tables:
CREATE TABLE table_name (ID BIGINT NOT NULL, PRIMARY KEY (ID))
But produces the same error.
For some reason creating the database happens in a transaction. I found the source code responsible, but I can't find out how the transaction is started. I can give more detailed information, but maybe somebody can already help now.
Edit: The 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="h ttp://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="AntennasOperatingSystemServerPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<non-jta-data-source>MyDatabase</non-jta-data-source>
<class>... bunch of classes ... </class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>
<property name="eclipselink.deploy-on-startup" value="true"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
<property name="eclipselink.logging.level.sql" value="FINEST"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.target-database" value="PostgreSQL" />
</properties>
</persistence-unit>
</persistence>
Have you tried setting the eclipselink.target-database property to PostgreSQL or org.eclipse.persistence.platform.database.PostgreSQLPlatform?
Please share your persistence.xml file if the above does not resolve the issue.
I did something similar some time ago. The following configuration work well.
<persistence-unit name="EjemploJpaPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url"
value="jdbc:derby://localhost:1527/example"/>
<property name="javax.persistence.jdbc.driver"
value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="123"/>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
Obviously, once the tables are created, it is necessary to change the value of the property eclipselink.ddl-generation to none.
NOTE: The database was Derby, but I've also tried it with MySQL. I think that it will work with PostgreSQL.

JPA, EclipseLink, PostgreSQL and schemas

I want to develop a Java EE application with JPA (EclipseLink implementation) and PostgreSQL as database.
I chose to have one database and multiples schemas instead of having multiples databases and one schema per database.
So, in my persistence.xml I have something like that :
<persistence-unit name="00" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>todo</jta-data-source>
<class>...</class>
<class>...</class>
<properties>
<property name="javax.persistence.target-database" value="PostgreSQL" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/testdb;create=true" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="userpwd" />
</properties>
</persistence-unit>
<persistence-unit name="01" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>todo</jta-data-source>
<class>...</class>
<class>...</class>
<properties>
<property name="javax.persistence.target-database" value="PostgreSQL" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/testdb;create=true" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="userpwd" />
</properties>
</persistence-unit>
I can easily reference persistence unit :
EntityManagerFactory emf = Persistence.createEntityManagerFactory("01");
EntityManager em = emf.createEntityManager();
but how can I store information about schemas in persistence.xml ? I want to access schema 00 or 01 in testdb database.
I probably could change schema via native SQL directly but is there a way to bind schema to persistence unit ?
Thank you
This same question was asked and answered here:
JPA - EclipseLink - How to change default schema
You can override the schema for the entire persistence unit using an orm.xml file, or define it within each table annotation or xml element as needed.