File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to change the database in Java EE

 
Ravi Danum
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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.

-Ravi


 
Prabhu Mehta
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have exact situation as yours explained in my post,
http://www.coderanch.com/t/431150/EJB-Other-Java-EE-Technologies/java/Dynamic-Datasource-invocation-at-runtime

Please do let me know if you found the solution.
Thanks in advance.
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi,

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.

Regards,
Reza
 
Ravi Danum
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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="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="TestPU" transaction-type="JTA">
<jta-data-source>jdbc/eeee3</jta-data-source>
<properties/>
</persistence-unit>
</persistence>

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:

<resources>
<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="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
</jdbc-connection-pool>
</resources>

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.

-Ravi

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic