• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Connection to data source on the basis of userlogin using JPA

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. [=
 
chandr prakash
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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......."
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
chandr prakash
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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?

 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic