0

Is there a CDI callback like @PostConstruct but that is instead invoked after the class is put into service?

I have a UserDAO that injects a EntityManager and a EJB UserBean that injects the UserDAO, which is invoked remotely.

When the UserBean.save(u) is invoked, it is executed within the transactional EJB scope. But the EntityManager injected into UserDAO isn't associated with the transaction by the injection time.

If I declare a init method in the UserDAO and annotate with @PostContruct, the TransactionSynchronizationRegistry isn't the same when the EJB method is invoked due to the @PostConstruct nature.

This is all correct, because the @PostConstruct callback is defined as "Initializing a managed bean specifies the lifecycle callback method that the CDI framework should call after dependency injection but before the class is put into service."

But, if I use the TransactionSynchronizationRegistry inside the method UserDAO.update(u), the transaction is the same as UserBean.

So, is there a callback method/annotation that I can use after the bean is put "into service"? That way I'd be able to put this method in the UserDAO, so I can use the TransactionSynchronizationRegistry before any method invocation of UserDAO.

Edit: The JTA transaction is created when my userBean.save(u) method is invoked. But at @PostContruct time of UserDAO I'm able to inject a valid instance of TransactionSynchronizationRegistry, but it is a different transaction with status STATUS_NO_TRANSACTION and another transaction-id (different from transaction from userBean.save(u)). But I would like to use some informations about the same transaction that originated from the invocation of the userDAO.save(u) method in the @PostConstruct method of UserDAO.

PS: English is not my main language. I'm sorry if I did not make myself clear enough.

ethanxyz_0
  • 713
  • 12
  • 37
  • 1
    Just a wild thought - have you considered either interceptor or decorator? You can hook it directly onto the method (or all of them) effectively allowing you to do stuff before calling the method. You could wire the EN the first time it is used then do no-op. – Siliarus Dec 09 '16 at 07:12
  • Does your persistence.xml specify JTA transactions or RESOURCE_LOCAL? – Steve C Dec 09 '16 at 09:57
  • @Siliarus yes, I wrote with the interceptor before. But I was looking for a less invasive approach. tks. – ethanxyz_0 Dec 10 '16 at 03:14
  • @SteveC using with JTA – ethanxyz_0 Dec 10 '16 at 03:15
  • The lifecycle of your beans should be irrelevant. You should not be able to invoke any business methods before the entire object graph is "put into service". I would normally expect that the current JTA transaction that is created when your `userBean.save(u)` method is invoked (or perhaps before depending upon where this call originates) will be associated with the current thread and picked up by your entity manager. – Steve C Dec 10 '16 at 04:11
  • @SteveC The JTA transaction is created when my `userBean.save(u)` method is invoked. But at `@Postcontruct` time of `UserDAO` I'm able to inject a valid instance of `TransactionSynchronizationRegistry`, but at this time the transaction status is `STATUS_NO_TRANSACTION` and with other transaction-id (different from transaction from `userBean.save(u)`) . I would like to use some informations about the (real) transaction at the creation of `UserDAO` (when its associated with JTA) – ethanxyz_0 Dec 10 '16 at 05:27
  • 1
    There is no transaction at creation time. You cannot access the transaction until the business method is invoked. – Steve C Dec 10 '16 at 05:36
  • @SteveC yes, I realized that :( I'm back to the Interceptor approach. Tks! – ethanxyz_0 Dec 10 '16 at 05:44

0 Answers0