This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTA & dynamic persistence unit

 
daniele licitra
Ranch Hand
Posts: 81
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all.
In my application, users select the year and manage data for that year (for example, they can choose 2014 for managing invoices for 2014, then switch to 2015 and work with that year). Each year as his own database.
I'am able to change persistence unit connection at runtime with


This procedure build a non-jta environment.

It is possible to create entityManager / entityManagerFactory with JTA ?
I don't want to create new persistence unit each year or each time a new database is required
 
Michael Remijan
Author
Ranch Hand
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done some research on this and so far the solution I've found is to combine CDI producers with an injected Instance. I'll give some cliff notes on what I mean.

The code that needs an EntityManager would inject an Instance instead of an EntityManager directly. here is what it woiuld look like:



Then when an EntityManager is needed, call the get() method.



You couple this with a class which @Produces an EntityManager. For example, here is one I put together doing some R&D.



The @Produces public EntityManager getEntityManager() {} method is resposible for producing the EntityManager. The setDatabaseDirectory(...) responsible for changing the configuration to attach to a different database. In my case I have a single PU in my persistence.xml and this class simply changes the file system directory of the Derby database to connect to. In my application, I @Inject EntityManagerProducer just like any other bean and call the setDatabaseDirectory() method to switch to a different database. Once any db activity happens after that it' happens on the new database.

How you change the internals of EntityManagerProducer to point to a different database is up to you. using a method like setDatabaseDirectory() is easy. Another option is to create your own annotations (not CDI qualifiers, just regular annotations) to pass additional information to the @Produces producer method. I'd take the easiest approach first. One you have somet8ing working then you can make it fancier.



Hopefully this will give you enough info to apply to your use case.



 
daniele licitra
Ranch Hand
Posts: 81
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried your solution.

The instruction


produce an Entity manager factory application managed. Maybe i missing something
How can I build it Container Managed?

My final goal is to use CMT (Container Managed Transaction) with JTA datasource.

An alternatives would be to define all possible persistence unit (stop the application, modify persistence.xml and restart application each time a new DB is required), without re-compiling the code, but how can I give the correct EntityManager to a Bean?
For example, the entity Invoice is the same entity for DB2014 and DB2015.
The EJB InvoiceFacade inject an EntityManager to manage the entities:



If it is possible to change the context of EntityManager at runtime , this will be a solution.

Any ideas?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!