| Change database schema at runtime [message #664304] |
Fri, 08 April 2011 06:28  |
Pavel Zelenka 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 11:37   |
James Sutherland Messages: 1834 Registered: July 2009 |
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
|
|
|
|
|
|
| Re: Change database schema at runtime [message #665456 is a reply to message #665415] |
Thu, 14 April 2011 12:39  |
Pavel Zelenka 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);
}
|
|
|
Powered by
FUDForum. Page generated in 0.01754 seconds