@Transactional annotation works with saveAndFlush?

Will the previous write operations in table A and table B be rollbacked? Yes, unless your saveAndFlush() methods have their own transactions (i.e. with propagation = REQUIRES_NEW). If they’re all part of the transaction you started in saveAndGenerateResult(), all modifications made to the database will be rolled back in case of failure. For more information: … Read more

Why does @Transactional save automatically to database

This a normal JPA behavior. Once you retrieve an object via find() or so, that object is regarded as attached, or belongs to a persistence context. Once you exit the method the @Transactional triggers a Spring transaction management aspect which flushes every “dirty” object to database and commits the transaction. Since your object is already … Read more

How to use spring transaction in multithread

No, methodB() will not be executed in the same transaction as methodA(). Spring’s @Transactional only works on a single thread – it creates a session when a thread first enteres a method with @Transactional (or a method in a class with @Transactional), and then commits it when it leaves that method. In your example, the … Read more

Transactional saves without calling update method

Because hibernate will automatically detect changes made to persistent entities and update the database accordingly. This behaviour is documented in chapter 11 of the hibernate reference manual. The relevant part reads: Hibernate defines and supports the following object states: Transient – an object is transient if it has just been instantiated using the new operator, … Read more

Why we shouldn’t make a Spring MVC controller @Transactional?

TLDR: this is because only the service layer in the application has the logic needed to identify the scope of a database/business transaction. The controller and persistence layer by design can’t/shouldn’t know the scope of a transaction. The controller can be made @Transactional, but indeed it’s a common recommendation to only make the service layer … Read more

@Transactional method calling another method without @Transactional anotation?

When you call a method without @Transactional within a transaction block, the parent transaction will continue to the new method. It will use the same connection from the parent method (with @Transactional) and any exception caused in the called method (without @Transactional) will cause the transaction to rollback as configured in the transaction definition. If … Read more

Showing a Spring transaction in log

in your log4j.properties (for alternative loggers, or log4j’s xml format, check the docs) Depending on your transaction manager, you can set the logging level of the spring framework so that it gives you more info about transactions. For example, in case of using JpaTransactionManager, you set log4j.logger.org.springframework.orm.jpa=INFO (this is the package of the your transaction … Read more

Spring – @Transactional – What happens in background?

This is a big topic. The Spring reference doc devotes multiple chapters to it. I recommend reading the ones on Aspect-Oriented Programming and Transactions, as Spring’s declarative transaction support uses AOP at its foundation. But at a very high level, Spring creates proxies for classes that declare @Transactional on the class itself or on members. … Read more

Spring @Transactional – isolation, propagation

Good question, although not a trivial one to answer. Propagation Defines how transactions relate to each other. Common options: REQUIRED: Code will always run in a transaction. Creates a new transaction or reuses one if available. REQUIRES_NEW: Code will always run in a new transaction. Suspends the current transaction if one exists. The default value … Read more