2

Is it possible to create a jpa persistence unit on the fly in runtime withowt having it defined in persistance.xml?

I have a use case where I use a one instance of an application for different customers, for each customer I setup a new database, I nead to setup a new customer withowt the nead to redeploy the application, this is why I nead to setup a persistance unit in runtime and not in persistence.xml

I use seam2, jsf1.2, hibernate3

Any hints on this?

simonC
  • 4,101
  • 10
  • 50
  • 78

2 Answers2

1

Not part of the JPA spec. Some JPA implementations allow a way, for example what we allow in DataNucleus JPA see the end of this doc http://www.datanucleus.org/products/accessplatform_3_0/jpa/persistence_unit.html Consult the docs for your JPA provider to see if it does

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
1

Seems like you know which persistence unit to use before deployment.If that is the case then you are not really after creating a PU at runtime .I think you have two choices.

  1. You could just use a place holder using ${PUNAME} and then in properties file set it according to your client. If you are using maven you could have a maven profile to fill these properties during development as well as packaging.
  2. Use some kind of locale based system have different persistence unit for every client e.g. persistenceunit_cient1.xml and persistenceunit_cient2.xml and let system pick one based on Client Locale . Yes you will have to tweak Locale setups.

ok..Just clarifying what I have understood. You have a running an application where some client can come and register and once signed up they will have their own database. I have let my imagination run little wild here :) but seems to be the case. What you can do is to change\update the properties file with things like connection url, password etc and relaod session factory.

This question can help you in updating the properties file at run time java update properties file run time

This question here and here has info how to reload session factory at run-time.

Community
  • 1
  • 1
Shahzeb
  • 4,745
  • 4
  • 27
  • 40
  • hmm no...my use case is that I would like to setupa a new customer in runtime, withowt the nead to redeploy the application...I would like to make a automated customer setup. So I dont know all the clients before deployment. The alternative is to keep all the clients in one database but I personaly dont like this solution for various reasons. – simonC Feb 22 '12 at 14:14
  • See my updated answer and some links that can help you further. – Shahzeb Feb 22 '12 at 21:50
  • Jap your imagination is correct that is my use case, tnx for tehe useful info, I will nead to digg deeper in your info to see if it works for my case, Your idea seems interesting, the problem that I can see is that I use seam framework which probably controls the session factory and I get the Entitymanager (EM) as a seam component, I have also the diferent definitions of managed-persistence-context for easc client which are seam controlled EM componenst via components.xml and I would nead also this to be dynamic, so that I could create those Entity maangers in runtime to achive my use case. – simonC Feb 24 '12 at 09:21