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.