@Transactional Quick Summary


 

DB requires approvement from us, for CREATE and DELETE operations. 

With a transaction, we can manage a process with one multiple db queries. It executes queries, if it works and everything is good (like validations, data) it commits to db. If not, it rollbacks.


It can be used with adding @Transactional (class or method level). 

JpaRepository, CrudRepository etc. is already using transactions in themselves for each method. When we need multiple create or update operations, we need this annotation on the method or class which we are trying to do multiple operations in.

There can be multiple transaction in a moment. We can handle that using propagation property of @Transactional annotation. By default it's Propagation.REQUIRED, which makes the method or class use the existing transaction. If not exist, creates new one.

Propagation.SUPPORTS makes it use the existing transaction too, but if not exist, it doesn't create new one.

Propagation.MANDATORY throws an exception if there is not existing transaction.

Propagation.REQUIRES_NEW creates new transaction and suspends the other transactions.


There are some other properties of @Transactional annotation:

readOnly makes the transaction unable to commit create and update.

timeOut makes it throw exception if no response from db in defined time. (milliseconds)

rollbackFor, maybe I edit this one later. Can't remember right now.

Comments