First off, I would be reluctant to create an EntityManagerFactory on the fly. I suppose you would have to do that in every single DAO instance creation (or whatever EJB object you are using to access your db). If that's the case, think twice about it: creating an EntityManagerFactory is very expensive and should usually be done once in an app lifecycle.As an alternative I would probably create different DAOs for each persistence unit, moving the responsibility of using one or the other to an upper layer, so that you can still rely on the container for creating the EntityManager.
First off, I would be reluctant to create an EntityManagerFactory on the fly. I suppose you would have to do that in every single DAO instance creation (or whatever EJB object you are using to access your db). If that's the case, think twice about it: creating an EntityManagerFactory is very expensive and should usually be done once in an app lifecycle.As an alternative I would probably create different DAOs for each persistence unit, moving the responsibility of using one or the other to an upper layer, so that you can still rely on the container for creating the EntityManager.
That being said, in your context and assuming that the method produceEM() is in an EJB and you are using container managed security, you can inject the EJBContext via: @Resource EJBContext ejbContext; From here you can get the Principal and create your EntityManager depending on it. If any of the assumptions is not valid, let me know and will try to update my answer. EDITED: @Stateless public class EntityManagerService { @Resource EJBContext ejbContext; @Produces public EntityManager produceEM() { EntityManagerFactory managerFactory = Persistence.
CreateEntityManagerFactory(ejbContext. GetCallerPrincipal().getName()); return managerFactory. CreateEntityManager(); } } You can also create your own mapping between user names and db names which is probably a good idea if you want some decoupling between those.
Well, you're right regarding the EntityManagerFactory. In my case, I do not know how many persistence units there will be in in the production environment. We have a database for each client but only one deployed webapp.So the number of clients will increase and i'm very very sure the operating will not create DAOs for each new client =).
What I need is a generic way to bind an EntityManager to a persistence unit where the Principal information points to the persistence unit to be used. – martin Dec 6 at 13:58 I edited my answer accordingly – Gonzalo Garcia Lasurtegui Dec 6 at 16:09 This is not solving my problem with the SQLException. Its still there saying " java.sql.
SQLException: You cannot set autocommit during a managed transaction! " – martin Dec 7 at 9:16.
AFAIK both bernate and EclipseLink support it.
Thanks for the hint, but thats the other way round =) I need a database for each customer. Thats how my boss wants it – martin Dec 7 at 8:44 This guy here shows how to do what you want in to/Bloggers/MultitenancyIn Essentially a ConnectioProvider that takes into account your db assignments. Looks like you will need to have adifferent persistence-unit for your user information, you will need to set your tenant id as soon as possible and if you do cross-thread or cross-VM calls you might get into trouble.
– Tassos Bassoukos Dec 7 at 9:15.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.