Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Change database schema at runtime
Change database schema at runtime [message #664304] Fri, 08 April 2011 10:28 Go to next message
Pavel Zelenka is currently offline Pavel ZelenkaFriend
Messages: 61
Registered: July 2009
Member
Hi,

in my application which uses EclipseLink 1.1.1 I'd like to change database at runtime.
The reason is this: on MySQL server there are several databases with identical structure. Decission which database use is made in application at runtime and it may change during the application lifecycle.
Is the only solution to have one persistent unit for each database?

Thanks for help.
Re: Change database schema at runtime [message #664690 is a reply to message #664304] Mon, 11 April 2011 15:37 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You could either have a different persistence unit for each database.

Or you could have a single persistence unit, but create a different EntityManagerFactory for each database. You would pass a properties into the createEntityManagerFactory, and give the new url for the database.


James : Wiki : Book : Blog : Twitter
Re: Change database schema at runtime [message #664876 is a reply to message #664690] Tue, 12 April 2011 11:17 Go to previous messageGo to next message
Pavel Zelenka is currently offline Pavel ZelenkaFriend
Messages: 61
Registered: July 2009
Member
Thanks, this is a good idea.
Re: Change database schema at runtime [message #665064 is a reply to message #664876] Wed, 13 April 2011 07:35 Go to previous messageGo to next message
Pavel Zelenka is currently offline Pavel ZelenkaFriend
Messages: 61
Registered: July 2009
Member
I have one more question. When I try to create EntityManager with properties Map Eclipselink creates it, but it is the same as the first one. It seems like there is some cache. Maybe there is some property that needs to be set. Any help appreciated.

[Updated on: Wed, 13 April 2011 07:36]

Report message to a moderator

Re: Change database schema at runtime [message #665415 is a reply to message #665064] Thu, 14 April 2011 14:54 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

If you give it a different URL or DataSource it should create a new one.

What properties are you setting?


James : Wiki : Book : Blog : Twitter
Re: Change database schema at runtime [message #665456 is a reply to message #665415] Thu, 14 April 2011 16:39 Go to previous message
Pavel Zelenka is currently offline Pavel ZelenkaFriend
Messages: 61
Registered: July 2009
Member
My persistence.xml is:

<?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="myPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider >
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.jdbc.user" value="..."/>
<property name="eclipselink.jdbc.password" value="..."/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
<class>...</class>
</persistence-unit>
</persistence>

My code is:

private Map<Distributor, EntityManagerFactory> mapPU = new HashMap<Distributor, EntityManagerFactory>();
....
protected void createEntityManagerFactory(Distributor distributor) {
Map properties = new HashMap();
properties.put(PersistenceUnitProperties.JDBC_URL, "jdbc:mysql://192.168.1.14:3306/" + distributor.getDatabase() + "?zeroDateTimeBehavior=convertToNull");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPU", properties);
mapPU.put(distributor, emf);
}
Previous Topic:JPA2.1
Next Topic:MySQL JDBC URL
Goto Forum:
  


Current Time: Mon Nov 24 04:11:17 GMT 2014

Powered by FUDForum. Page generated in 0.01779 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software