Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jta-dev] BEAN and CONTAINER tx management

Yes and no.

More exactly, the behavior without the Transactional annotation is equivalent to TxType.SUPPORTS - it can run both in transaction if it exists, or outside of transaction if no transaction exists. The difference is in how the bean is called - if it's called from another bean that creates a transaction, then it will run in that transaction. While using UserTransaction requires that no transaction is available, even if the bean is called from another bean that creates a transaction. Therefore it's better to turn on TxType.NOT_SUPPORTED explicitly, which will suspend a transaction if it exists, and will allow using UserTransaction in all cases, not only if the bean is used outside of the transaction.

In code, it would look like this:

@RequestScoped
@Transactional
class CallingBean {
    @Inject
BeanWithUserTransaction anotherBean;
    
    void doSomethingInTransaction() {
        anotherBean.doSomethingInUserTransaction()
    }
}

With the code above, the following will give you an exception when using UserTransaction, if no @Transactional annotation is present:

@RequestScoped
class  BeanWithUserTransaction {

@Inject
UserTransaction tx;

void doSomethingInUserTransaction() {
  tx.begin();  // here will be exception
}
}

To fix this, you need to use @Transactional(Transactional.TxType.NOT_SUPPORTED) on the BeanWithUserTransaction class.

Ondro

On Tue, Feb 4, 2025 at 3:50 AM hantsy bai via jta-dev <jta-dev@xxxxxxxxxxx> wrote:
Hi,
So it is equivalent to NOT SUPPORTED by default without a Transactional annotation?
---

Regards,

Hantsy Bai

Self-employed consultant, fullstack developer, agile coach, freelancer/remote worker

GitHub: https://github.com/hantsy

Twitter: https://twitter.com/@hantsy

Medium: https://medium.com/@hantsy


On Tue, Feb 4, 2025 at 4:17 AM Ondro Mihályi via jta-dev <jta-dev@xxxxxxxxxxx> wrote:
Hi,

According to the Transactions spec in section 3.7. Transactional Annotation, using UserTransaction is only allowed with @Transactional(Transactional.TxType.NOT_SUPPORTED), it cannot be used in other Tx types. So:

@Transactional(Transactional.TxType.NOT_SUPPORTED)
@RequestScoped
public class MyBean {
   @Inject
   UserTransaction userTransaction;
}

Ondro

On Mon, Feb 3, 2025 at 7:32 PM Arjan Tijms via jta-dev <jta-dev@xxxxxxxxxxx> wrote:
Hi,

The equivalent is the default in CDI; doing nothing ;)

Kind regards,
Arjan Tijms



On Mon, 3 Feb 2025 at 15:22, hantsy bai via jta-dev <jta-dev@xxxxxxxxxxx> wrote:
Hey guys, I was trying to migrate my Cargotracker fork to Jakarta EE 11,  there is an EJB bean marked with TransactionManagement.
@TransactionManagement(TransactionManagementType.BEAN) 
What is the equivalent component when moving to CDI beans?

---

Regards,

Hantsy Bai

Self-employed consultant, fullstack developer, agile coach, freelancer/remote worker

GitHub: https://github.com/hantsy

Twitter: https://twitter.com/@hantsy

Medium: https://medium.com/@hantsy
_______________________________________________
jta-dev mailing list
jta-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jta-dev
_______________________________________________
jta-dev mailing list
jta-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jta-dev
_______________________________________________
jta-dev mailing list
jta-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jta-dev
_______________________________________________
jta-dev mailing list
jta-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jta-dev

Back to the top