Spring Boot Transaction of Management

Yusuf BEŞTAŞ
4 min readDec 12, 2020

--

After giving a short information about transaction,we can directly pass to features of transaction.

What is Transaction ?

Transaction processes are the realization of one or more database transactions as a whole.In other words, after all transactions in the method have been successfully performed, the database processes will be able to be performed.if any problem from other processes occurs,all database process should be taken back.Thus, we preserve data integrity, We call this mechanism the rollback mechanism.

For example:Let’s imagine that we will insert to 5 different tables on the database side in one of our methods.Let’s say that we inserted to 4 tables but the code failed before the method had been completed. At this point ,the rollback mechanism runs and takes back all of the data base operations.

Features of Transaction

And now ,we can start to examine the features one by one from the chart below

1.Propagation:

In the service method, it determines the behavior of the transaction,such as whether the transaction in our hands will be initiated when we call, or if there is an existing transaction, whether it will continue with it or whether it will need to create a new transaction.So now ,we can start examining the propagation’s features.

1.1 Required :

It’s a default property of propagation. It goes on if there is an active transaction, if not ,creates a new one. If it only uses the @Transactional annotation, it behaves like @Transactional(propagation=Propagation.REQUIRED)

Let’s check the example that is given through code

function1() and function2() work in the same transaction.Until the save() method ends, the same transactions manage function1() and function2(),

  1. 2:Propagation.SUPPORTS: if there is a transaction ,uses it.If not,it acts like lazy. Doesn’t create a new transaction and works without it. Let’s check the code below.

if the addProduct method is called directly,it will not create a transaction for itself.

if the addProduct method calls another service and that service has a transaction,uses it.If the service is called doesn’t have a transaction,it doesn’t new one.

3:Propagation.NESTED: In fact,certain inferences are made from the name.If there is an existing transaction,it opens another transaction in parallel,and while this transaction becomes rollback,the other continues to run.It is used with savepoint developed by JDBC technology and I will share another writing by showing it on the code for this feature.

  1. 4:Propagation.NEVER:

İf the method is called directly,it will not create a new transaction.If the above method calls another service and this service already has a transaction,throws an exception.You can examine the error in the image below.

If the service that it calls does not have a transaction, it will not create a new transaction and continues its life without a transaction.

  1. 5:Propagation.MANDATORY: As it understood from it’s name,it works the opposite of the NEVER property.It throws an exception of there is not transaction.

If the method is called directly , throws the following error “throw an exeption”

If the method calls another service…

If that service has a transaction,it uses that transaction.If not ,it will throw an exception.

  1. 6:Propagation.REQUIRES_NEW: I tried to draw as you can see the below,I draw a little badly,but I know that you can handle it for now :)

As I have drawn in the image above,if there is an active transaction,it holds that transaction and creates new transaction.After completing the necessary transactions,it continues on the old transaction from where it left off.

  1. 7:Propagation.NOT_SUPPORTED: Let’s examine the image above again.The only difference is : If there is an existing transaction,it holds that and doesn’t creates a new transaction. After completing the process,it continues on the old transaction from where it left off.

3.TimeOut: It makes rollback when the processes do not take place within a certain period of time.For example ,you are communicating with an external system and you can make a rollback system for a long time.

@Transactional(timeout=25)

4.rollbackFor: This feature is actually my favorite feature…

While we are doing our transactions,we use them in certain situations when we want them make a rollback or not.

@Transactional(noRollbackFor = {SocketTimeoutException.class,HttpHostConnectException.class)

as you can see in the example above,I do not want it to make a rollback when ı get SocketTimeoutException and HttpHostConnectException exceptions.It is used in such cases.

In this writing, I focused more on the capabilities like

REQUIRES_NEW ,

NOT_SUPPORTED,

MANDATORY,

NEVER,

SUPPORTS,

REQUIRED,

which have transaction features. I will share another writing for other features, and I’ll give examples on the code.

--

--

No responses yet