Its possible but its way more complicated.
In Fact the Provider<EntityManager>, UnitOfWork and PersistService are all implemented in the single class JpaPersistService.
And the JpaPersistService has a static inner class which is the Provider<EntityManagerFactory> which it uses directly and not over injection.

This implementation couples all the above interfaces so tightly that I don't see a way to get in between them.
This means you would have to rewrite all of them.

It is not that hard to do. My opinion is that you would need two classes:
- one which implements PersistService and Provider<EntityManagerFactory>
- one which implements UnitOfWork and Provider<EntityManager>
Most of the code can be copied from the JpaPersistService.

And finally you would have to write you own XxxPersistModule which binds the above classes.

Since creating an EntityManagerFactory is expensive it might be worth storing the created ones in a map for later reuse.



On 09/27/2012 10:01 AM, Martin Schayna wrote:
Thanks a lot. In my case I have to parametrize creation of EntityManagerFactory, not EntityManager, because each request may work with different database. Persistence unit defined in persistence.xml is the same one, but properties like hibernate.connection.url are passed to Persistence.createEntityManagerFactory("persistence-unit", myProperties).

Can I use my own Provider<EntityManager> implementation and bind it to EntityManager interface, instead of default implementation in guice-persist?


On Wednesday, September 26, 2012 3:15:55 PM UTC+2, scl wrote:
Ok

I'm not sure I understand completely what you need. But here is my first
proposal:
Write your own PersistFilter (see below) and use
EntityManager.setProperty to set the request specific properties.

@Singleton
public final class MyPersistFilter implements Filter {
     private final UnitOfWork unitOfWork;
     private final Provider<EntityManager> emProvider;
     private final PersistService persistService;

     @Inject
     public PersistFilter(UnitOfWork unitOfWork, Provider<EntityManager>
emProvider, PersistService persistService) {
         this.unitOfWork = unitOfWork;
         this.emProvider = emProvider;
         this.persistService = persistService;
     }

     public void init(FilterConfig filterConfig) throws ServletException {
         persistService.start();
     }

     public void destroy() {
         persistService.stop();
     }

     public void doFilter(final ServletRequest servletRequest, final
ServletResponse servletResponse, final FilterChain filterChain) throws
IOException, ServletException {

         unitOfWork.begin();
         try {
             final EntityManager em = emProvider.get();
             final Map<String, String> props =
getPropertiesForRequest(servletRequest);
             for(String key: props.keySet()) {
                 em.setProperty(key, props.get(key));
             }
             filterChain.doFilter(servletRequest, servletResponse);
         } finally {
             unitOfWork.end();
         }
     }

     private Map<String, String> getPropertiesForRequest(final
ServletRequest servletRequest) {
         // TODO
         return null;
     }
}
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/BRd0U6Urq94J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Reply via email to