Return-Path: X-Original-To: apmail-deltaspike-users-archive@www.apache.org Delivered-To: apmail-deltaspike-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5B2C617F6A for ; Fri, 17 Apr 2015 16:30:51 +0000 (UTC) Received: (qmail 25419 invoked by uid 500); 17 Apr 2015 16:30:51 -0000 Delivered-To: apmail-deltaspike-users-archive@deltaspike.apache.org Received: (qmail 25386 invoked by uid 500); 17 Apr 2015 16:30:51 -0000 Mailing-List: contact users-help@deltaspike.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@deltaspike.apache.org Delivered-To: mailing list users@deltaspike.apache.org Received: (qmail 25368 invoked by uid 99); 17 Apr 2015 16:30:50 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 17 Apr 2015 16:30:50 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of rmpestano@gmail.com designates 209.85.192.169 as permitted sender) Received: from [209.85.192.169] (HELO mail-pd0-f169.google.com) (209.85.192.169) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 17 Apr 2015 16:30:46 +0000 Received: by pdbnk13 with SMTP id nk13so132943080pdb.0 for ; Fri, 17 Apr 2015 09:30:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=it5MY1dmle5+kC9ZUn5PqQlbT8SL6yvgWNkigt5YLfU=; b=ssj0qoBaXKFw9RRT1EFeRjqASBZMjU70gzFt+jcBt1LYHhOWfgR1KwHWoUlyBoKGsW ZN1NPC8ANDRxjVyK/gkkUBV+mXbNeU5cZnJbHI/8JAD+O57qdAaEjK76Llw2+mXSNmZ7 zefn2cSeccNJWOg+s9GLtUm5u38iOa1081MKYI4dAm+pfK7pAobY7tTer5+K7zOuXX5m bGhZxu9/SZIURhoq9/z/pNmMTk96K3thbFwoEyXd3XWG52LoYzsSCosAeyXc5K1pvyaF 9iWh0h3B5E/mwjBViXHRc4I3e2ZETnlqE1RZNWDvlj9WSAauyec1+Q8jkvNUbhaoUEEk Yulw== X-Received: by 10.68.239.199 with SMTP id vu7mr6720058pbc.113.1429288226717; Fri, 17 Apr 2015 09:30:26 -0700 (PDT) MIME-Version: 1.0 Received: by 10.66.16.161 with HTTP; Fri, 17 Apr 2015 09:30:06 -0700 (PDT) In-Reply-To: <5531321D.8030006@gmail.com> References: <5530D44D.4050603@senat.fr> <5531321D.8030006@gmail.com> From: Rafael Pestano Date: Fri, 17 Apr 2015 13:30:06 -0300 Message-ID: Subject: Re: Extended EntityManager To: users@deltaspike.apache.org Content-Type: multipart/alternative; boundary=047d7b33d5bc0909980513ee1972 X-Virus-Checked: Checked by ClamAV on apache.org --047d7b33d5bc0909980513ee1972 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi Denis, for situations where the scope is not active we use an interceptor to activate it, see this example: http://gist.asciidoctor.org/?14312d475ab402768783 2015-04-17 13:17 GMT-03:00 titou10 : > Ludovic, > I don't know if it may be of some help for you, but we have a lot of seam= 2 > apps that we migrated to CDI and we had to face the problems you describe > (With many other..) > We tried first to use CODI then DeltaSpike, in its early days but we ende= d > to create our own CDIUtils libs.. > To solve the problem, we exposed the EM in RequestScoped but it is kept > internally a ConversationScoped Bean holder: > So when a LRC is on, we always get the same EM > > @ConversationScoped > public class EntityManagerProducer { > private transient EntityManager em; > > @PersistenceUnit > private EntityManagerFactory emf; > > @Produces > @RequestScoped > protected EntityManager creerEntityManager() { > // On n'a pas de EM en scope conversation, on en r=C3=A9cup=C3=A8re= un... > if (em =3D=3D null) { > em =3D emf.createEntityManager(); > } > return this.em; > } > > @PreDestroy > public void terminer() { > if (em !=3D null) { > if (em.isOpen()) { > log.trace("close em {}", em); > em.close(); > } > } > em =3D null; > } > } > > Also the EM is not directly injected into Beans as our "Business Objects" > are held in a jar and the jar may be used in 3 different contexts: > - online/web in EJB3 SFSB,SLSB > - online in MDB or Web Services beans (No conversationscope here, but > @PersistenceUnit IS available) > - in batchs/JUnit (No conversationscope here, and no@PersistenceUnit > available from J2SE) > > We have an interface called "EMResolver" that is injected in BO beans: > public interface EntityManagerResolver { > EntityManager getEm(); > } > > with 2 different implentations, one onlie, one for batchs (that is > declared as an @Alternative) > The online implementation is: > @Stateless > public class EntityManagerResolverOnline implements EntityManagerResolver= { > @PersistenceContext > private EntityManager emFromEJB; > > @Inject > private Instance emLazyInstance; > > public EntityManager getEm() { > Boolean estMDBouWS =3D ThreadLocalContextHolder.isMDBorWS(); > if (estMDBouWS) { > log.trace("Contexte online MDB ou WS - EntityManager vient de > @PersistenceContext."); > return emFromEJB; > } else { > log.trace("Contexte online non MDB ni WS - EntityManager vient d= e > CDI. +joinTransaction"); > EntityManager em =3D emLazyInstance.get(); > em.joinTransaction(); > return em; > } > } > } > The ThreadLocalContextHolder keep a boolean "threadlocal" variable set by > MDBs (from MDB own superclass) or WS (JAX-WS own handler) > > The batch implementation: > @Alternative > @ApplicationScoped > public class EntityManagerBatchResolver implements EntityManagerResolver = { > > private EntityManagerFactory emf; > private EntityManager em; > > @PostConstruct > public void initialiser() { > log.debug("@PostConstruct. Utilisation de > EntityManagerBatchResolver"); > emf =3D Persistence.createEntityManagerFactory(); > em =3D emf.createEntityManager(); > } > > @PreDestroy > public void terminer() { > log.debug("@PreDestroy. terminer"); > emf.close(); > } > > public EntityManager getEm() { > return em; > } > } > > Maybe it is not very academic but it works perfectly well for us and made > our move from seam 2 smotth.. > > Hope this helps.. > > Denis > > > Le 2015-04-17 05:37, l.penet@senat.fr a =C3=A9crit : > >> Dear all, >> >> I am actually trying to switch from "raw" Hibernate to JPA. >> >> I have the typical "keep the same entitymanager" during a scenario >> problem. IMHO, merging huge collections of entities is a nightmare that = is >> not worth the cost when you deal with applications with a lot of busines= s >> logic but few users and a low load. I fully endorse the "entitymanager p= er >> request" pattern for application having high trafic, and so need to be >> stateless, replicated, etc. but it does not appear to me to be the magic >> solution to all problems. >> >> With hibernate, I historically solved it with the "open session in view" >> pattern. It is ugly when evaluated according to actual standards, but it >> works. >> >> https://developer.jboss.org/wiki/OpenSessionInView >> >> When I tried to find the equivalent for JPA, I basically found >> >> @PersistenceContext(type=3DPersistenceContextType.EXTENDED) >> >> ...which could be a fit if I was using container managed datasources >> >> When digging the Hibernate 4.3.8 jpa implementation, I noticed that when >> I use application managed EntityManager-s, they are all created with >> PersistenceContextType.EXTENDED, and that this attribute is finally pass= ed >> to : >> AbstractEntityManagerImpl base class constructor : >> >> protected AbstractEntityManagerImpl( >> EntityManagerFactoryImpl entityManagerFactory, >> PersistenceContextType type, // TODO: remove as no longer >> used >> SynchronizationType synchronizationType, >> >> and is just ignored. :-) >> >> On this blog (I like good authors ;-) ) >> https://struberg.wordpress.com/2012/04/25/is-there-a-way-to-fix-the-jpa-= entitymanager/ >> , I found mention of Avaje, which could be a fit if it was more of a >> standard. >> >> I also fond on the net references to CODI, MyFaces Orchestra and Seam >> solutions to this problem. Most of the type it was also indicated that >> Deltaspike is "the way to go". An affirmation that is clearly also mine = in >> general. >> However, I did not find a ready-to-use solution in the doc >> >> >> http://deltaspike.apache.org/documentation/jpa.html#_extended_persistenc= e_contexts >> >> I was a bit surprised, as some people stated that Seam code was to be >> merged in Deltaspike not to find a turn key solution. >> >> Did I miss / misunderstood something ? >> >> This is not a blocking problem to me. I can just use plain hibernate or >> unwrapped jpa/hibernate sessions and it will work... But I would like to >> understand. :-) >> >> Thanks in advance, >> >> Ludovic >> >> >> | >> | AVANT D'IMPRIMER, PENSEZ A L'ENVIRONNEMENT. >> | >> >> > --=20 Att, Rafael M. Pestano Desenvolvedor Java Cia. de Processamento de Dados do Rio Grande do Sul http://rpestano.wordpress.com/ @realpestano --047d7b33d5bc0909980513ee1972--