Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to change the database in Java EE

 
Ranch Hand
Posts: 165
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have exact situation as yours explained in my post,
https://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.
 
author
Posts: 580
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 165
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic