Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hantsy opened this issue Dec 27, 2024 · 5 comments
Open

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

hantsy opened this issue Dec 27, 2024 · 5 comments

Comments

@hantsy
Copy link

hantsy commented Dec 27, 2024

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.

@tomjenkinson
Copy link
Contributor

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

@hantsy
Copy link
Author

hantsy commented Jan 3, 2025

@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.
}

@tomjenkinson
Copy link
Contributor

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?

@hantsy
Copy link
Author

hantsy commented Jan 3, 2025

@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

@tomjenkinson
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants