transactions icon indicating copy to clipboard operation
transactions copied to clipboard

Convenient method to wrap an unit of work into a transaction.

Open hantsy opened this issue 1 year ago • 7 comments

The JPA adds EMF.runInTransaction/EMF.calInTransaction and EM.doInConnection, and Spring also provides TransactionTemplate and TransactionalOperator(for reactive streams API).

If possible add such a method to UserTransaction or create a new TransactionalOperator for this purpose.

T doInTransaction(Function<TransactionStatus, T>)

As developers, we do not need to call begin and commit when using it.

hantsy avatar Dec 27 '24 03:12 hantsy

Please can I ask what the difference to something like CMT or https://jakarta.ee/specifications/transactions/2.0/apidocs/jakarta/transaction/transactional would be? Perhaps you can raise this topic on our community mailing list to potentially reach the wider Jakarta Transactions community: https://accounts.eclipse.org/mailing-list/jta-dev

tomjenkinson avatar Jan 02 '25 11:01 tomjenkinson

@Transactional can not be applied in a code block.

But the methods in EMF, EM can run something like this, https://github.com/hantsy/jakartaee11-sandbox/blob/master/hibernate/src/test/java/com/example/SampleTest.java#L75

In the Jakarta Trasanction, we could provide a general purposes API like this.

public void doSomeThing() {
   txOperator.doInTransaction(status -> {
      wrap the unit of work in a tx.
   });

   // do other work outside of transation.
}

hantsy avatar Jan 03 '25 00:01 hantsy

Thank you for providing the example. Though I can understand the code snippet you shared, I am not sure if it is substantially different enough from something like:

@Transactional (Transactional.TxType.NOT_SUPPORTED)
public void doSomeThing() {
   myRunInTransaction();
    // do other work outside of transaction.
}

@Transactional (Transactional.TxType.REQUIRES_NEW)
public void myRunInTransaction() {
}

such that it would requiring adding a new approach for transaction demarcation?

Please can I ask you to elaborate a bit on what would make the new method better? Would it be to avoid the need for CDI here? Please can you also help to define a bit what TransactionStatus would look like? Is it similar to https://jakarta.ee/specifications/transactions/2.0/apidocs/jakarta/transaction/status or does it have some more functionality?

tomjenkinson avatar Jan 03 '25 10:01 tomjenkinson

@tomjenkinson Your code snippet can archive the purpose.

What I want here is to provide a simple programmatic approach to wrap the unit of work in a tx.

The transaction status in spring and Micronaut are similar. Spring: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/TransactionStatus.html Micronaut: https://micronaut-projects.github.io/micronaut-data/3.0.0/api/index.html?io/micronaut/transaction/TransactionStatus.html

hantsy avatar Jan 03 '25 11:01 hantsy

Thank you for reviewing the snippet. I think perhaps it would be useful to introduce this topic to the jta-dev mailing list for wider feedback from our community on this feature request?

tomjenkinson avatar Jan 03 '25 12:01 tomjenkinson

I think it makes sense to provide this functionality in UserTransaction and replicate the same methods as are in EMF - runInTransaction and callInTransaction, so that the method names and semantics should be the same. This would provide a more general approach than with EMF, which would work also without JPA. EMF would then seem as it extends these methods with a better integration with EntityManger.

While it seems that separating a code snippet into a method annotated with @Transactional (Transactional.TxType.REQUIRES_NEW) would serve the same purpose, it wouldn't work. The @Transactional interceptor is applied on a proxy, not directly on the bean object. Calling annotated method directly within the same object would bypass the interceptor and thus no transaction would be created. So, with the current API, the only way to do this is to use begin, commit, rollback methods in UserTransaction, which is tedious.

OndroMih avatar Feb 04 '25 09:02 OndroMih

+1, this would be a good thing to add.

gavinking avatar May 03 '25 18:05 gavinking