Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

JTA & dynamic persistence unit

Ranch Hand
Posts: 82
Netbeans IDE Java Linux
  • 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
Posts: 131
  • 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: 82
Netbeans IDE Java Linux
  • 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?
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic