I am researching a possibility of using Play 2/Java for an application that needs to be able to access multiple identical databases (using Ebean or other ORM). I found some discussions on this subject related to Play 1, but not much for Play 2. Any pointers would be greatly appreciated.
Asked
Active
Viewed 142 times
0
-
Write more precisely what exactly you want to achieve – biesior Oct 31 '13 at 08:43
-
I've added more details in the reply to vivekj011. The main objective is to create one JPA mapping (for a "master" database) and use it to access numerous identical databases (clones of the "master" database). – user1886877 Oct 31 '13 at 22:46
1 Answers
1
Using JPA(with Ebean or Hibernate) you can use multiple databases from same play-2.x application.
Some references (first one with example):
- https://github.com/cm0s/play2-jpa-multiple-persistenceunit
- Play framework 2 + JPA with multiple persistenceUnit
All you need to do is, to have multiple persistence-units(each persistence-unit will represent one database) in your persistence.xml file.
-
Thanks! That's almost what I am looking for :) The thing that I am still a little fuzzy about is how to make it generic enough so I won't have to create a separate controller method annotated with a specific persistenceUnit. Basically the issue I am trying to solve is that there maybe hundreds of identical databases (each customer get's their own database). The specific persistence unit will need to be figured out based on user credentials. Is it possible to specify persistence unit programmatically v.s. using annotation? – user1886877 Oct 31 '13 at 22:42
-
Ok, by looking at your use case, you can not work with annotations. JPA's entity manager will solve your problem. You need different entity manager instance, for different users. What you will have to do is, depending on user credentials, you will get different entitymanager instance and then all flows will be same. By default, you need one persistence unit in your persistence.xml file. And depending on user credentials, just override those properties of persistence unit, which are different for different users. – vivekj011 Nov 02 '13 at 06:34
-
Continuation to previous comment,have a look at : http://docs.oracle.com/javaee/5/api/javax/persistence/EntityManagerFactory.html#createEntityManager(java.util.Map) and http://stackoverflow.com/a/1990914/1485147 – vivekj011 Nov 02 '13 at 06:43