Propagation setting is REQUIRED.
@Transactional(propagation = Propagation.REQUIRED)
Transaction is read/write.
In which scenario those are used? Please give me explain with example
Propagation setting is REQUIRED.
@Transactional(propagation = Propagation.REQUIRED)
Transaction is read/write.
In which scenario those are used? Please give me explain with example
Spring transaction default is
@Transactional(propagation = Propagation.REQUIRED)
So you do not need to specify the propagation property.
So, What does it mean by @Transactional
annotation for a spring component ?
Spring framework will start a new transaction and executes all the method and finally commit the transaction.
But If no transaction is exists in the application context
then spring container will start a new transaction.
Propagation.REQUIRED
then transactional behavior assigned in a nested way to each method in logically but they are all under the same physical transaction.So, What is the result ?
The result is if any nested transaction fail, then the whole transaction will fail and rolled back (do not insert any value in db) instead of commit.
Example:
@Service
public class ServiceA{
@Transactional(propagation = Propagation.REQUIRED)
public void foo(){
fooB();
}
@Transactional(propagation = Propagation.REQUIRED)
public void fooB(){
//some operation
}
}
Explanation :
In this example foo()
method assigned a transactional behavior and inside foo()
another method fooB()
called which is also transactional.
Here the fooB()
act as nested transaction in terms of foo()
. If fooB()
fails for any reason then foo()
also failed to commit. Rather it roll back.
This annotation is just to help the Spring framework to manage your database transaction.
Lets say you have a service bean that writes to your database and you want to make sure that the writing is done within a transaction then you use
@Transactional(propagation = Propagation.REQUIRED)
Here is a small example of a Spring service bean.
@Service
class MyService {
@Transactional(propagation = Propagation.REQUIRED)
public void writeStuff() {
// write something to your database
}
}
The Transactional
annotation tells Spring that: