Spring Boot : Understand Connection and Transaction

If you want to know how transaction and connection work in Spring Boot by “trying” with examples, this page is for you.

This example is related to Spring Boot with all examples. However, code in here is not in that link.

Full Code with Spring Boot 1.5.8.RELEASE:

8_spring-boot-test-db-transaction.zip

What you need for run this:

  • Maven 3
  • Java 8
  • Mysql 5.x

What the code will do:

  • The code will show you multiple examples of how to use connection and transaction in Spring.
  • This page will explain those examples case by case.

Things you should know before start:

First, you should read this Spring Boot with Database, Transaction, JDBI example before. It will explain you about @Transactional and code structure in basic.

Second, there is a class named UniqueObjectIdHolder.java. This class looks like this:

This class will just return an unique ID (just a number) of each object. It will be used to check which DB connection is using at a moment.

For example,  UserDao.insert() has this code:

Line 3 and 4 show how to use UniqueObjectIdHolder. They are just printing unique IDs of “dataSource ” and “conn” object.

Prepare to run:

create a database named “testdb” and run dbscript.sql to the database in MySql:

change application.properties to your MySql username and password:

run this in console:

Then run this to start:

You should see something like this:

Open a browswer and try to go to this url:

You should see this:

If you see this, that means it is working fine.

TIP: before trying with each case, you should clean all rows in database by call this:

And check the row by calling “list” again:

You will see nothing.

Case 1: Normal Insert

Let start with very basic one. You want to insert a row in database.

Try to call this url:

You will see a number, like this:

The code has nothing complex. It just calls TestRestController. insertOK() -> UserService.InsertOK() -> UserDao.insert(). The code looks like:

TestRestController:

UserService:

UserDao:

You will see these lines in the log or console:

Try to call the url again, then you will see other two new lines:

Note that “You get the same datasource but different connections” . In other words, Spring Boot gives you different connection in each request.

You can try to list the row by calling this url:

You will see this:

Also note that, in the log,  you will get different connection (see line 5 and 6):

The most important is that the connection is closed by Spring Boot, you do not have to do anything. You can check by checking processes that connect to database (I suggest HeidiSQL, I have been using this nice lightweight MySql Client for 10 years. Please donate to the developer if you please).

Case 2: Exception and Rollback

Let say, you want to make sure that if there is something wrong, any insert/delete/update should be rollbacked.

We will call the code in UserService which looks like this:

This method will just insert a row in database but then it will throw a RuntimeException.

Try to run insertAndFail() by calling:

Then you will see this:

Try to check if the row is in database:

You will see nothing.

So, Spring Boot takes care rollback for you. And again  the connection is closed by Spring Boot, you do not have to do anything.

But BEWARE it will take care only “unchecked exception” such as RuntimeException. What happen if it is checked exception? It will not rollback, see the detail in the next case.

Anyways, you may ask what “checked exception” and “unchecked exception”.  Here quick explanation:

  • checked exception is the exception that you declare in method signature, for example:

  • unchecked exception is the exception that you do not (do not have to) declare  in method signature, for example RuntimeException or its subclass.

Case 3 : Exception but NOT rollback

If you have checked exception like this;

The inserted row in line 5 will not be rollbacked.

You can try by calling:

You will see this:

But the transaction is not rollbacked. You can check query in the database or by just calling this url:

You may ask “Even it does not rollback, will the Spring boot close the connection for me?” Yes it will. You can check by checking processes that connect to database.  They will not increase.

Then , how to make Spring Boot rollback the checked exception?

You can just change @Transactional to @Transactional(rollbackFor = Exception.class). Like this:

Try to call this url:

You will see the error page and transaction also rollback.

Case 4 : Is it the same transaction ?

We know from the Case 1 that you will get different connection from the different request, but you may doubt that:

  • Will I get the same connection across method call?
  • Will I get the same connection across different DAO classes?

To answer the first question, in UserService, I have this method:

It will just call the same DAO object twice. The purpose of this is just to see if different method call still give use the same connection.

You can try by calling this url:

You will see from the log that you will get the same ID of connection:

To answer the second question, in UserService, I have this method:

It will call the TWO DAO objects. The purpose of this is to see if different object call still give use the same connection.

You can call the method by calling this url:

You will see from the log that you will get the same ID of connection:

What you just know:

You should get some basic idea about how connection and transaction behave in Spring Boot.

You should see how powerful Spring Boot helps us about connection and transaction. “Great power comes with great responsibility” so before using it, you must understand it.

These cases should cover 90% (I guess) of basic needs of web applications with database.

Believe it or not, many developers do not know such this basic things and create a production-like applications. Database transaction is too important to just let some framework handles without understanding.