Multiple Modules
So far we've seen how to use Guice Persist and JPA with a single persistence unit. This typically maps to one data store. Guice Persist supports using multiple persistence modules using the private modules API.
This means that you must be careful to install all JPA modules only in individual private modules:
Module one = new PrivateModule() {
protected void configure() {
install(new JpaModule("persistenceUnitOne"));
// other services...
}
};
Module two = new PrivateModule() {
protected void configure() {
install(new JpaModule("persistenceUnitTwo"));
// other services...
}
};
Guice.createInjector(one, two);The injector now contains two parallel sets of bindings which are isolated from each other. When you bind services in module one those services will get the EntityManager from persistenceUnitOne and transactions around its database.
Similarly services in module two will have their own transactions, EntityManagers etc., and these should typically not cross.
What about if you require multiple persistence units in a single module? I.e. I have one database for configuration data, another database for logs. Whenever I change some configuration data, I need to also add a log to the other database.
The same story. Why can't I just use @EntityManager?("myPersistenceUnit1") in one place and @EntityManager?("anotherPersistenceUnit2") in another ?
How do you inject each persistence module ? is there something like @Inject("myPersistenceUnit1") EntityManager? em ;
@Inject @Named("myPersistenceUnit1") EntityManager? em;
After a some headaches imho due to sparse documentation on that topic I found an explanation how to setup PersistFilter? in a ServletModule with multiple persist modules:
https://groups.google.com/forum/#!topic/google-guice/2VK-bdsnjZc/discussion
Furthermore I want to point at the following post, as it helped me to understand that persisting objects to the database requires the Session or EntityManager? not being exposed but kept in private space:
https://groups.google.com/forum/?fromgroups#!searchin/google-guice/@Transactional$20guice-persist$20not/google-guice/kbXr4k0N2QE/W_6VAvo5Mn0J