0

I am using hibernate for the REST API. Right now all the transactions are handled by explicitly calling beginTransaction and transaction.commit. The transaction is rolled back in case of a failure. I am looking to use @Transactional instead of all the beginTransactions and commit transactions. Could someone tell me how can I integrate @Transactional in my hibernate. I am using mysql for querying the database.

1

2 Answers 2

1

You can annotate your query method with @Transactional so you get your transaction opened, commited and closed when your method ends.

Be careful about isolation levels (https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Transactional.html#isolation--) because it indicates when Spring will create a new session for your transaction, or simply use already opened one.

When you thrown an exception on your method, transaction gets an automatic rollback and you're good :)

Sign up to request clarification or add additional context in comments.

Comments

0

You can use Spring Framework with Hibernate Integration. The advantage is spring manages the Hibernate session's and all the low level things that we have to manage manually in hibernate like granular commit etc. Here is the example of this. this is older repository so uses older version of Spring and Hibernate but you can upgrade it here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.