Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Can I create database sessions from an external file?
Can I create database sessions from an external file? [message #800204] Thu, 16 February 2012 15:33 Go to next message
Dean Schulze is currently offline Dean Schulze
Messages: 2
Registered: February 2012
Junior Member
I have a Java SE (desktop) application that needs to access different databases that all have the same data model. I need to be able to configure the various datasources through an external file so the users can add or modify datasources on their own.

JPA apparently doesn't have any way to do this. All datasources have to be specified in the persistence.xml file, which makes them static.

Does EclipseLink have this capability? What I've found so far is that the sessions.xml file needs to be contained in the .jar file which makes it just as static as JPA. Can EclipseLink Session configurations be read from an external file?

Or if EclipseLink has any other way to allow dynamic configuration of database sessions that may solve my problem too.

Thanks.

Re: Can I create database sessions from an external file? [message #804374 is a reply to message #800204] Wed, 22 February 2012 10:01 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

JPA allows for a properties Map to be passed to createEntityManagerFactory(). You can pass the database url/etc information in this map to specify the database at runtime.


James : Wiki : Book : Blog : Twitter
Re: Can I create database sessions from an external file? [message #804498 is a reply to message #804374] Wed, 22 February 2012 12:44 Go to previous message
Dean Schulze is currently offline Dean Schulze
Messages: 2
Registered: February 2012
Junior Member
Thanks, James.

The JavaDocs for createEntityManagerFactory() are inadequate and confusing. The 3 createEntityManagerFactory() methods all take a persistenceunit name, but the Javadocs don't give you a clue what can go in the properties or that the properties override what is in persistence.xml.

After checking the JPA 2.0 spec and the EclipseLink docs I've posted what I've learned at stackoverflow (copied below since this forum software won't allow me to include the URL).


You have to know what to put into the properties map and the Javadocs don't help at all. The Javadocs for createEntityManagerFactory() just suck.

======= From my post at StackOverflow ==================================

stackoverflow.com/questions/9315593/how-can-i-make-a-jpa-application-access-different-databases

The confusing thing about using properties to dynamically create EntityManagers is that there are three createEntityManagerFactory() methods in JPA and all 3 take a persistenceunit name. I didn't realize that the properties override the persistenceunit name until I checked the JPA 2.0 spec. Section 9.4.3 says that the properties override the values given in the persistence.xml. It also shows what the standard property names are.

(The JavaDocs don't tell you what can go into the properties and they don't say that the properties override what is in persistence.xml. Another example where JavaDocs suck.)

Your example uses property names like "JDBC_URL" that aren't in the Java EE 6 API Javadocs. The JPA spec explains that vendor specific property names can be used in addition to the standard properties. The EclipseLink docs show what property names that implementation supports via the non-standard PersistenceUnitProperties.

eclipse.org/eclipselink/api/2.3/index.html

So in order to have dynamic EntityManagers it is necessary to use vendor specific properties, so dynamic entity managers aren't portable. I guess you can't have everything.

One case where dynamic EntityManagers would be portable is through the use of the standard javax.persistence.jtaDataSource property. You would have to add the data source to your Java EE container, but that is as dynamic as you can be when running in a container. In Java SE it doesn't look like there are any portable, dynamic options.

The JavaDocs need to do a much better job of explaining how properties work with the createEntityManagerFactory() methods.

[Updated on: Wed, 22 February 2012 12:46]

Report message to a moderator

Previous Topic:stackOverFlowError
Next Topic:Not creating the Unique constraint on the DB (postgreSQL)
Goto Forum:
  


Current Time: Fri Jul 25 14:20:35 EDT 2014

Powered by FUDForum. Page generated in 0.01574 seconds