Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes EJB and other Java EE Technologies and the fly likes How to change the database in Java EE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How to change the database in Java EE" Watch "How to change the database in Java EE" New topic

How to change the database in Java EE

Ravi Danum
Ranch Hand

Joined: Jan 13, 2009
Posts: 104

Is it possible to change the database in Java EE based on values read in from a config file by a web service deployed in an application server? How can I create an EntityMangerFactory based on the new database parameters?

I know this can be done in Java SE, by creating an EntityManagerFactory with a map containing user, password, database url, and driver.

What about Java EE? I know the container is responsible for creating the EntityManagerFactory based on the persistence.xml file it finds packaged in the war file. Is it advisable to create a new EntityManagerFactory in Java EE?

Thanks in advance.


Prabhu Mehta

Joined: Feb 13, 2009
Posts: 9
I have exact situation as yours explained in my post,

Please do let me know if you found the solution.
Thanks in advance.
Reza Rahman
Ranch Hand

Joined: Feb 01, 2005
Posts: 580

Yes, you can still programatically create entity managers when it is really necessary. Also, you can have multiple injected entity managers corresponding to each database instance.


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Ravi Danum
Ranch Hand

Joined: Jan 13, 2009
Posts: 104

I have read the links and have learned more, but still not sure how to create an EntityManagerFactory from Java EE.

I am using Glassfish, Toplink, and SQL server. The persistence.xml file contains the following:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<persistence-unit name="TestPU" transaction-type="JTA">

How can I dynamically set a new value for <jta-data-source>? Its not in the properties section.

Also, the databaseUrl, driver, userId, password are found in the sun-resources.xml file. An xml snippet is shown below:

<jdbc-resource enabled="true" jndi-name="jdbc/eeee2" object-type="user" pool-name="microsoft_sqlPool"/>
<jdbc-resource enabled="true" jndi-name="jdbc/eeee3" object-type="user" pool-name="microsoft_sqlPool"/>
<property name="serverName" value="server-name"/>
<property name="portNumber" value="1433"/>
<property name="databaseName" value="dbname"/>
<property name="User" value="username"/>
<property name="Password" value="password"/>
<property name="URL" value="jdbc:sqlserver://hostname:portnumber;databaseName=dbname"/>
<property name="driverClass" value=""/>

How would this file's contents be affected by the creation of a new EntityManagerFactory at runtime? How do I associate the new jta-data-source with the new database properties? Should I use: new EntityManagerFactory("namePU", map), where map contains the properties from the above snippet?

Any help is appreciated.


I agree. Here's the link:
subject: How to change the database in Java EE
It's not a secret anymore!