Hi, Romain,


Yep. By "create a cdi bean" I meant get the beanmanager and use 
injectionTargetFactory to populate it, as does DeltaSpike.

I'll go for your solution and upload the branch as soon as I get the unit tests 
working.

Best regards,

Paulo








De: Romain Manni-Bucau <[email protected]>
Para: <[email protected]>
Cc: "Paulo Araújo"<[email protected]>
Data: sex., 20 nov. 2026 14:24:44 -0300
Assunto: Re: Clarification on JPA spec - OPENJPA-3089



Hi Paulo, 
 
> My first implementation was creating a CDI injected bean for each listener 
 
 
you didn't mean create a CDI bean but just an injection target? 
(getInjectionTargetFactory 
etc). Trick there is to get the bean manager, there is no CDI.current() but 
it is not portable so we need a CdiBeanManagerProvider service in openjpa 
you can hook in to provider your instance and we can have a default which 
uses CDI.current() once when the EMF is created to capture it and not pay 
the price everytime. 
 
In terms of perf can't we check lazily at first injection if the instance 
has some not injected state -> do it everytime, any not normal scoped 
injection -> do it everytime, else -> cache the listener (it is assumed 
stateless). 
 
What is weird is "Entity listeners that do not make use of CDI injection 
are stateless. The lifecycle of such entity listeners is unspecified.", 
tend to mean they can be singleton when not using CDI which is likely true 
but with CDI you will quickly inject @RequestScoped instance combined 
with @Dependent so it can be risky if we don't try to detect it. 
 
In terms of perf the detection can browse the full cdi graph so either we 
say we do it best effort and bounded to 2 in terms of depth for ex or that 
we just enable to tune it with a custom annotation for ex on the listener. 
 
Hope it helps a bit. 
 
Romain Manni-Bucau 
@rmannibucau < https://x.com/rmannibucau > | .NET Blog 
< https://dotnetbirdie.github.io/ > | Blog < https://rmannibucau.github.io/ > | 
Old 
Blog < http://rmannibucau.wordpress.com > | Github 
< https://github.com/rmannibucau > | LinkedIn 
< https://www.linkedin.com/in/rmannibucau > | Book 
< 
https://www.packtpub.com/en-us/product/java-ee-8-high-performance-9781788473064 
> 
Javaccino < https://javaccino.dev/ > founder (Java/.NET service - contact via 
linkedin) 
 
 
Le ven. 20 nov. 2026 à 16:35, Paulo Araújo via dev < 
mailto:[email protected] > 
a écrit : 
 
> Hi, folks! 
> 
> As I told earlier, I'm working on OpenJPA support of CDI injection on 
> entity listeners. 
> 
> 
> 
> The spec states that: 
> 
> 
> 
> An entity listener is a noncontextual object. 
> In supporting injection into entity listeners, the persistence provider 
> must behave as if it carries out the following steps involving the use 
> of the CDI SPI. (See 
> https://jakarta.ee/specifications/persistence/3.2/jakarta-persistence-spec-3.2.html#a19500
>   
> ). 
> 
> Obtain a BeanManager instance. (See 
> https://jakarta.ee/specifications/persistence/3.2/jakarta-persistence-spec-3.2.html#a12802
>   
> ) 
> 
> 
> Create an AnnotatedType instance for the entity listener class. 
> 
> 
> Create an InjectionTarget instance for the annotated type. 
> 
> 
> Create a CreationalContext. 
> 
> 
> Instantiate the listener by calling the InjectionTarget produce method. 
> 
> 
> Inject the listener instance by calling the InjectionTarget inject method 
> on the instance. 
> 
> 
> Invoke the PostConstruct callback, if any, 
> by calling the InjectionTarget postConstruct method on the instance. 
> 
> 
> 
> 
> When the listener instance is to be 
> destroyed, the persistence provider must behave as if it carries out the 
> following steps. 
> 
> 
> Call the InjectionTarget preDestroy method on the instance. 
> 
> 
> Call the InjectionTarget dispose method on the instance 
> 
> 
> Call the CreationalContext release method. 
> 
> 
> 
> 
> Persistence providers may optimize the steps 
> above, e.g. by avoiding calls to the actual CDI SPI and relying on 
> container-specific interfaces instead, as long as the outcome is the 
> same. 
> 
> 
> Entity listeners that do not make use of CDI 
> injection are stateless. The lifecycle of such entity listeners is 
> unspecified. 
> 
> 
> 
> My first implementation was creating a CDI injected bean for each listener 
> call (and it can be burdensome and have some performance penalty). Trying 
> the same application using eclipselink, the behavior was different: it 
> seems their contextual injected entity listeners are created only once and 
> used for all JPA container life, so a @Dependent created bean is injected 
> once and does not get updated in subsequent calls, even if it should. The 
> common use case is the injection of a userId per request. There is a 
> workarround for this behavior, though: the entity listener may use @Inject 
> Instance<YourRequestOrDependentBean> to get current state. 
> 
> Given the performance penalty, I'm switching the impl to behave as 
> eclipselink behaves - cache the created listener, destroying it only when 
> the container is shutdown. 
> 
> Do you agree? 
> 
> 
> Best regards, 
> 
> Paulo

Reply via email to