JDBC transaction management in Spring

In the previous tutorial, I introduced you to the concept of a transaction in the database, which helps us to ensure data integrity and consistency. Spring also supports us to manage transactions, allowing us to focus on developing business logic without worrying about the integrity of the data. Spring supports many types of transaction management but in this tutorial, I just introduced you to JDBC transaction management in Spring.

First, I will create a new Maven project as an example:

JDBC transaction management in Spring

In this project, I declare the Spring framework dependencies as follows: in addition to spring-context for Spring Core, there is spring-jdbc for using the JDBC API and spring-tx for transaction management.

with the property of “spring-framework.version” is:

I will use JDBC with MySQL database, so I will also add MySQL driver to our project dependency.

The database is defined with the following structure:

In addition, we need to mention Spring’s configuration file, spring.xml, located in the /src/main/resource directory with the following content:

In this example, we will create a small bank application that allows us to transfer funds from account A to account B. The transfer process consists of two steps: subtracting money from account A and adding money to account B. Any exceptions that occur during the transfer process, will all be rolled back.

OK, now I’m going to detail the classes in our example.

First, we must mention the AccountDAO interface and its implementation: the AccountDAOImpl.

These classes will help us manipulate the database directly.

The contents of these classes are as follows:

AccountDAO

In this interface, I declare two methods, used to directly manipulate the database to get the current amount in an account and update the amount for an account.

AccountDAOImpl:

I already declared this class in the Spring container using the @Repository annotation and the JdbcTemplate object to manipulate the database.

The second thing is the AccountService interface and its implementation AccountServiceImpl.

AccountService

This interface has only one method that helps us transfer money from one account to another.

AccountServiceImpl

For this class, I used @Service annotation to declare it in Spring’s container and used the AccountDao interface to manipulate the database.

Another thing to note is that on the transfer() method, I declare an @Transactional annotation with the rollbackFor property having the value “Exception.class”. This annotation, the purpose is to specify that this method will be executed in a transaction. For any operation that fails in this method, all previous changes in this method will be rolled back.

Next, we will look at Spring’s configuration file.

There are some declarations that we have to do:

– Enable the auto component scan for the classes that we have declared using the @Repository annotation and @Service.

If someone does not know about the auto component scan in Spring, can refer to this tutorial.

– Configure Datasource to manipulate the database.

– Enable transaction management for our application.

In the above declaration, the tx:annotation-driven tag is used to declare to Spring that we will use the annotation to handle the transaction. The transaction-manager attribute used to declare the bean ID of the class handles transactions. As you can see, here we use the DataSourceTransactionManager object to handle transactions.

OK, so we have declared and implemented the necessary classes. In the last step, we will implement this application.

For example, in our database there are two accounts A and B with the following information:

JDBC transaction management in Spring

I will now write the code to make the transfer of $10 from account A to account B. The content of the application class will be as follows:

Result:

JDBC transaction management in Spring

This is the result in case everything is OK.

If everything is not OK then?

Suppose now, after deducting money from account A, I check the remaining money in account A. If the amount is less than $20 then I do not allow the transfer by throwing an Exception as follows:

when running with the database as follows:

JDBC transaction management in Spring

An exception will occur:

and nothing changes in the database.

Add Comment