File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Multiple DB access using multiple persistent units in same persistence.xml file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Multiple DB access using multiple persistent units in same persistence.xml file" Watch "Multiple DB access using multiple persistent units in same persistence.xml file" New topic
Author

Multiple DB access using multiple persistent units in same persistence.xml file

Siddhesh Kenkre
Greenhorn

Joined: Jan 02, 2013
Posts: 2
Hello All,
I m new to JSF and primefaces... I m currently working on a JSF project with primefaces wherein i have to access 2 databases A and B . I have create 2 persistent units in persistence.xml as follows

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="myJSFAppPU" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>java:/LnpDatasource</non-jta-data-source>
<class>com.proj.entity.table1</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://192.168.5.228:3306/A"/>
<property name="hibernate.connection.username" value="lsms_yogesh"/>
<property name="hibernate.connection.password" value="hello09"/>
</properties>
</persistence-unit>

<persistence-unit name="myJSFAppPU1" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>java:/LnpDatasource_NE</non-jta-data-source>
<class>com.proj.entity.table2</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://192.168.5.228:3306/B"/>
<property name="hibernate.connection.username" value="lsms_shango_test"/>
<property name="hibernate.connection.password" value="hello09"/>
</properties>
</persistence-unit>
</persistence>

My datasource files i have kept in deploy folder of jboss server

Contents of my -ds files
Filename : LnpDatasource-ds.xml
<datasources>
<local-tx-datasource>
<jndi-name>LnpDatasource</jndi-name>
<use-java-context>true</use-java-context>
<connection-url>jdbc:mysql://192.168.5.228:3306/A</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>lsms_yogesh</user-name>
<password>hello09</password>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>

Filename : LnpDatasource_NE-ds.xml


<datasources>
<local-tx-datasource>
<jndi-name>LnpDatasource_NE</jndi-name>
<use-java-context>true</use-java-context>
<connection-url>jdbc:mysql://192.168.5.228:3306/B</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>lsms_shango_test</user-name>
<password>hello09</password>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>

My queries:

1) is it OK for me to use <non-jta-data-source> ? Shud i change to <jta-data-source> ?
2) Is the content of -ds.xml files correct ?
3) I have created 2 entityManagers( em1 and em2 ) one for each persistent unit. and am using the annotation @PersistenceContext(unitName = "") before my entityManager declaration. In my case when i first use em1 and then try to fire a query using em2 , the query does not get fired and i get "javax.faces.el.EvaluationException: java.lang.IllegalStateException: Transaction not active" error. How to solve this ?
4) Do i have to add any separate configuration file to my project ... During googling i came across one post when it says we have to add "applicationContext.xml" file to our project.



Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16098
    
  21

There's no difference between doing this in JSF and doing it without JSF. So that makes this an ORM question, not a JSF question. I'll move this thread over to where the ORM experts can advise you.


Customer surveys are for companies who didn't pay proper attention to begin with.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2383
    
  28

Wait!! You are calling EntityManager directly from your JSF Managed Bean?. In that case this is a JSF question (Sorry Tim).


The core problem is that you are trying to do some modifications in the database without a transaction. I know almost next to nothing about JSF, so I have no idea how transactions are handled in JSF. Does the container automatically weave transaction code for you? Or do you have to explicitly start/stop transaction yourself?

AFAIK. the standard practice is that the JSF Managed Bean should call a service. The service can be an EJB or a Spring loaded bean, and the container that manages the service will weave transaction management code for you. Again, I know nothing about JSF, so I don;t know if the container treats JSF managed beans like EJBs
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16098
    
  21

Sorry, I missed that little detail.

It is bad practice to put persistence code directly into JSF backing beans. Yes, you can inject an entityManager directly into a backing bean if you are using Spring to wire it (which is what the applicationContext.xml file is for) in AND you have installed the Spring-JSF EL Resolver by configuring it in the faces-config.xml file. But I recommend putting all of the persistence code in a separate service class similar to a Session EJB.

I run multiple tiers. The lowest level is the object model itself. Next up are the DAOs, which handle the individual table finder and update methods. Next up from that are the persistence Services, which use the DAOs to manipulate complex table relationships within a database transaction. This tier is also providing the transaction-bounded business logic, since that usually is closely tied to the object relationships. The Service modules I inject into the JSF backing beans. Only the DAO level uses EntityManager. All database transactions are delimited at the Service methods. A transaction starts when the method is invoked and is terminated when the method returns. I use detached objects within the JSF code itself, as I think that working with attached objects at the UI levels is not only sloppy, but unsafe.
Siddhesh Kenkre
Greenhorn

Joined: Jan 02, 2013
Posts: 2
Hello all ,
I am not creating the entityManager in the JSF managed bean. I create it in the service class and and all the DB operations are done here in the service class.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2383
    
  28

Is your service an EJB or is it spring managed? Do you manage your own transactions, or do you use container managed transactions?
 
GeeCON Prague 2014
 
subject: Multiple DB access using multiple persistent units in same persistence.xml file