aspose file tools*
The moose likes Object Relational Mapping and the fly likes Connection to data source on the basis of userlogin using JPA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Connection to data source on the basis of userlogin using JPA" Watch "Connection to data source on the basis of userlogin using JPA" New topic
Author

Connection to data source on the basis of userlogin using JPA

chandr prakash
Ranch Hand

Joined: Jan 29, 2011
Posts: 32
Enviornment : EJB 3.0,JPA,weblogic 10.x

1.I am connecting to data source on the basis of user login.
2. Data sources will already be deployed to application server.
3. Code should be flexible enough, if in future new login(with new data source) is added in applicaiton,then i have to change minimal code(preferablly change persistent.xml, dont want to change session bean code.) ,because login is subject for change in future(May be new login are added and old ones are deleted so it is easy to change persistent.xml rather than session bean.)

So i have written following code but it is not fulfilling my 3rd requirement.



persitent.xml




But if in future if new login is added in application then i have to change my TestSessionBean2.java and persistent.xml file, which i dont want. I only want to change persistent.xml file. Is it possible? if not then what should my code looks like so that minimal change can be possible?
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

I believe that you only need to change the DS.

PS.: If you classes are mapped with @Table annotation, you may have problems if you change your DB. But you can override annotations with xml configs. [=


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
chandr prakash
Ranch Hand

Joined: Jan 29, 2011
Posts: 32
If new login is added,then in above code i have to change persistent.xml as well as session Bean
New login : user3,persistentUnit corresponding to user3 : EntityBeanDS3,DataSource corresponding to EntityBeanDS3:TESTDS2



As we can see that i have added New persistentUnit "EntityBeansDS3" { @PersistenceContext(unitName="EntityBeanDS3")
EntityManager emds3; } in order to associate new login user3 to TESTDS2

and also i have to change my persistent.xml


So i have to change my session bean as well as persitent.xml.Which i dont want?
In order to incorporate new login preferablly code should be like I only have to change persistent.xml file. Is it possible? if not then what should my code looks like so that minimal ccode hange can fullfill my future requirement?
chandr prakash
Ranch Hand

Joined: Jan 29, 2011
Posts: 32
@Hebert Coelho Thankx for reply!..TESTDS,TESTDS1,TESTDS2 are connected to database server installed on different machine,But the schema from which these data sources are connected, having exactly same table design structure.Actually they are replicated on different machine, but only difference is they are installed on different machine. In future if new login is added, then the schema from which it will connected,will also have exactly same table design structure.

So no need to worry "If you classes are mapped with @Table annotation, you may have problems......."
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

You return a list of data sources in the method? So not a one associated with a login? What's the exact purpose of this? Perhaps there might be a better approach.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
chandr prakash
Ranch Hand

Joined: Jan 29, 2011
Posts: 32
@Vijitha Kumara..thankx for reply
Actually requirement is very simple,There are multiple login in the application and each loginId will be associated to the different datasources.The schemas to which these datasources will be connected, exactly have same table design sttructre....(Means each login can access only its datasource not other login's datasource.)...Datasources will be already deployed on jboss server deploy directory..oracle-ds.xml.(If jboss will be used) and by connection pool and data sources(if weblogic will be used)
In future if new login will be added in application then i have to associate it with new data sources,which i want, should be done with minimal code change(preferablly some xml file).
Is it possible?If yes then how my code will look like?

Enviornment : JSP,struts2,EJB 3.0,JPA
Please tell me if any other information you want?

James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
You can do this by passing properties when creating your EntityManagerFactory. You will not be able to use injection with the @PersistenceContext, so will need to use an application manager persistence context.

In your properties you will set "javax.persistence.jtaDataSource" to the DataSource that you want to use.

i.e.
EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistence-unit", properties);
em = factory.createEntityManager();
em.joinTransaction(); // jonis the current JTA transaction.

You will only need one persistence unit in your persistence.xml. You will probably want to store the factories in some kind of static Map keyed by the data source id.

This may not be the best solution though. You might be better off changing the persistence.xml and redeploying the application instead. So each data source (tenant?) will have their own deployed application (no code changes, as the persistence unit is the same).


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connection to data source on the basis of userlogin using JPA