Spring Transaction Management Transactional in Depth - Adoc
Spring Transaction Management Transactional in Depth - Adoc
Marco Behler
2022-06-03
:page-layout: layout-guides
:linkattrs:
:page-image: "/images/guides/undraw_blooming_jtv6.png"
:page-description: You can use this guide to get a simple and practical
understanding of how Spring's transaction management with the @Transactional
annotation works.
:page-description2: The only prerequisite? You need to have a rough idea about
ACID, i.e. what database transactions are and why to use them. Also, distributed
transactions or reactive transactions are not covered here, though the general
principles, in terms of Spring, still apply.
:page-published: true
:page-tags: ["spring", "transactions"]
:page-commento_id: /guides/spring-transaction-management-unconventional-guide
== Introduction
In this guide you are going to learn about the main pillars of
https://www.marcobehler.com/guides/spring-framework[Spring core's] _transaction
abstraction framework_ (a confusing term, isn't it?) - described with a lot of code
examples:
Instead you are going to learn Spring transaction management the _unconventional
way_: From the ground up, step by step. This means, starting with plain old
https://en.wikipedia.org/wiki/Java_Database_Connectivity[JDBC transaction]
management.
Why?
Because everything that Spring does is _based on_ these very JDBC basics. And
you'll save a ton of time with Spring's @Transactional annotation later, if you
grasp these basics.
If you are thinking of skipping this section, without knowing JDBC transactions
inside-out: *don't*.
The first important take-away is this: It does not matter if you are using Spring's
@Transactional annotation, plain Hibernate, jOOQ or any other database library.
In the end, they _all do the very same thing_ to open and close (let's call that
'manage') database transactions. Plain JDBC transaction management code looks like
this:
[[plain-jdbc-example]]
[source,java]
----
import java.sql.Connection;
try (connection) {
connection.setAutoCommit(false); // <2>
// execute some SQL statements...
connection.commit(); // <3>
} catch (SQLException e) {
connection.rollback(); // <4>
}
----
<1> You need a connection to the database to start transactions.
https://docs.oracle.com/javase/7/docs/api/java/sql/
DriverManager.html[DriverManager.getConnection(url, user, password)] would work as
well, though in most enterprise-y applications you will have a data source
configured and get connections from that.
<2> This is the *only* way to 'start' a database transaction in Java, even though
the name might sound a bit off.
_setAutoCommit(true)_ makes sure that every single SQL statement automatically gets
wrapped in its own transaction and _setAutoCommit(false)_ is the opposite: You are
the master of the transaction(s) and you'll need to start calling `_commit_` and
friends. Do note, the `_autoCommit_` flag is valid for the whole time your
connection is open, which means you only need to call the method once, not
repeatedly.
<3> Let's commit our transaction...
<4> Or, rollback our changes, if there was an exception.
Yes, these 4 lines are (oversimplified) everything that Spring does whenever you
are using the @Transactional annotation.
In the next chapter you'll find out how that works. But before we go there, there's
a tiny bit more you need to learn.
If you already played with Spring's @Transactional annotation you might have
encountered something like this:
[source,java]
----
@Transactional(propagation=TransactionDefinition.NESTED,
isolation=TransactionDefinition.ISOLATION_READ_UNCOMMITTED)
----
We will cover nested Spring transactions and isolation levels later in more detail,
but again it helps to know that these parameters all boil down to the following,
basic JDBC code:
[source,java]
----
import java.sql.Connection;
// isolation=TransactionDefinition.ISOLATION_READ_UNCOMMITTED
connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); // <1>
// propagation=TransactionDefinition.NESTED
[[spring-section]]
== How Spring's or Spring Boot's Transaction Management works
As you now have a good JDBC transaction understanding, let's have a look at how
plain, https://docs.spring.io/spring/docs/current/spring-framework-reference/
core.html[core Spring] manages transactions. Everything here applies 1:1 to
https://spring.io/projects/spring-boot[Spring Boot] and Spring MVC, but
<<transactional-spring-boot, more about that>> a bit later..
Remember, transaction management simply means: How does Spring start, commit or
rollback JDBC transactions? Does this sound in any way familiar from above?
Here's the catch: Whereas with plain JDBC you only have one way
(setAutocommit(false)) to manage transactions, Spring offers you many different,
more convenient ways to achieve the same.
The first, but rather sparingly used way to define transactions in Spring is
programmatically: Either through a TransactionTemplate or directly through the
PlatformTransactionManager. Code-wise, it looks like this:
[source,java]
----
@Service
public class UserService {
@Autowired
private TransactionTemplate template;
* You do not have to mess with opening or closing database connections yourself
(try-finally). Instead you use https://docs.spring.io/spring/docs/current/javadoc-
api/org/springframework/transaction/support/TransactionCallback.html[Transaction
Callbacks].
* You also do not have to catch SQLExceptions, as Spring converts these exceptions
to runtime exceptions for you.
* And you have better integration into the Spring ecosystem. TransactionTemplate
will use a TransactionManager internally, which will use a data source. All are
beans that you have to specify in your Spring context configuration, but then don't
have to worry about anymore later on.
Back in the day, when XML configuration was the norm for Spring projects, you could
configure transactions directly in XML. Apart from a couple of legacy, enterprise
projects, you won't find this approach anymore in the wild, as it has been
superseded with the much simpler @Transactional annotation.
We will not go into detail on XML configuration in this guide, but you can use this
example as a starting point to dive deeper into it - if needed (taken straight from
the https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/
transaction.html#transaction-declarative[official Spring documentation]):
[source,xml]
----
<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below)
-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="get*" read-only="true"/>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
----
[source,xml]
----
<aop:config>
<aop:pointcut id="userServiceOperation" expression="execution(*
x.y.service.UserService.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="userServiceOperation"/>
</aop:config>
[source,java]
----
From a Java code perspective, this declarative transaction approach looks a lot
simpler than the programmatic approach. But it leads to a lot of complicated,
verbose XML, with the pointcut and advisor configurations.
So, this leads to the question: Is there a better way for declarative transaction
management instead of XML? Yes, there is: The @Transactional annotation.
Now let's have a look at what modern Spring transaction management usually looks
like:
[source,java]
----
public class UserService {
@Transactional
public Long registerUser(User user) {
// execute some SQL that e.g.
// inserts the user into the db and retrieves the autogenerated id
// userDao.save(user);
return id;
}
}
----
How is this possible? There is no more XML configuration and there's also no other
code needed. Instead, you now need to do two things:
So, to get the @Transactional annotation working, all you need to do is this:
[source,java]
----
@Configuration
@EnableTransactionManagement
public class MySpringConfig {
@Bean
public PlatformTransactionManager txManager() {
return yourTxManager; // more on that later
}
}
----
Now, when I say Spring transparently handles transactions for you. What does that
_really mean_?
Armed with the knowledge from the <<plain-jdbc-example, JDBC transaction example>>,
the @Transactional UserService code above translates (simplified) directly to this:
[source,java]
----
public class UserService {
connection.commit(); // <1>
} catch (SQLException e) {
connection.rollback(); // <1>
}
}
}
----
<1> This is all just standard opening and closing of a JDBC connection.
That's what Spring's transactional annotation does for you automatically, without
you having to write it explicitly.
<2> This is your own code, saving the user through a DAO or something similar.
This example might look a bit _magical_, but let's have a look at how Spring
inserts this connection code for you.
Spring cannot really rewrite your Java class, like I did above, to insert the
connection code (unless you are using advanced techniques like bytecode weaving,
but we are ignoring that for now).
Now whenever you are using @Transactional on a bean, Spring uses a tiny trick. It
does not just instantiate a UserService, but also a transactional _proxy_ of that
UserService.
[ditaa,document1,png]
----
+--------------------------------+ +---------+----------
+ /------------------\
| | + @Transactional + |
|
| | + UserService Proxy | | Real
UserService |
| UserRestController | +--------------------+
|------------------|
| | | | |
|
| @Autowired | | 1. open tx | |
|
| UserService userService; | +---->| |+---->|
userDao.save() |
| | | |<----+|
|
| | | 2. close tx | |
|
+--------------------------------+ +-------------------+
+ \------------------/
----
As you can see from that diagram, the proxy has one job.
*Quick Exam*
Have a look at the following source code and tell me what _type_ of UserService
Spring automatically constructs, assuming it is marked with @Transactional or has a
@Transactional method.
[source,java]
----
@Configuration
@EnableTransactionManagement
public static class MyAppConfig {
@Bean
public UserService userService() { // <1>
return new UserService();
}
}
----
<1> Correct.
Spring constructs a dynamic CGLib proxy of your UserService class here that can
open and close database transactions for you. You or any other beans won't even
notice that it is not _your_ UserService, but a proxy wrapping your UserService.
mb_ad::spring_course[]
Now there's only one crucial piece of information missing, even though we have
mentioned it a couple of times already.
Your UserService gets proxied on the fly, and the proxy manages transactions for
you. But it is not the proxy itself handling all this transactional state (open,
commit, close), the proxy delegates that work to a _transaction manager_.
It does exactly what you did so far to manage transactions, but first, let's look
at the needed Spring configuration:
[source,java]
----
@Bean
public DataSource dataSource() {
return new MysqlDataSource(); // <1>
}
@Bean
public PlatformTransactionManager txManager() {
return new DataSourceTransactionManager(dataSource()); // <2>
}
----
<1> You create a database-specific or connection-pool specific datasource here.
MySQL is being used for this example.
<2> Here, you create your transaction manager, which needs a data source to be able
to manage transactions.
Simple as. All transaction managers then have methods like "doBegin" (for starting
a transaction) or "doCommit", which look like this - taken straight from Spring's
source code and simplified a bit:
[source,java]
----
public class DataSourceTransactionManager implements PlatformTransactionManager {
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
Connection newCon = obtainDataSource().getConnection();
// ...
con.setAutoCommit(false);
// yes, that's it!
}
@Override
protected void doCommit(DefaultTransactionStatus status) {
// ...
Connection connection =
status.getTransaction().getConnectionHolder().getConnection();
try {
con.commit();
} catch (SQLException ex) {
throw new TransactionSystemException("Could not commit JDBC
transaction", ex);
}
}
}
----
So, the datasource transaction manager uses _exactly_ the same code that you saw in
the JDBC section, when managing transactions.
[ditaa,document2,png]
----
+--------------------------------+ +---------+----------------------
+ /---------------------------------\
| | + @Transactional + |
|
| | + UserService Proxy | |
PlatformTransactionManager |
| UserRestController | +--------------------------------+
|---------------------------------|
| | | | |
|
| @Autowired | | 1. txManager.getTransaction() |+---->|
dataSource.getConnection(...) |
| UserService userService; |+----->| | |
//autoCommit(false) etc. |
| | | 2. userService.registerUser() | |
|
| | | | |
|
| | | 3. txManager.commit() |<----+|
connection.commit() |
+--------------------------------+ +-------------------------------+
+ \---------------------------------/
----
[source,java]
----
@Service
public class UserService {
@Autowired
private InvoiceService invoiceService;
@Transactional
public void invoice() {
invoiceService.createPdf();
// send invoice as email, etc.
}
}
@Service
public class InvoiceService {
@Transactional
public void createPdf() {
// ...
}
}
----
Now in terms of database transactions, this should really just be *one* database
transaction. (Remember: _getConnection(). setAutocommit(false). commit()._) Spring
calls this _physical transaction_, even though this might sound a bit confusing at
first.
From Spring's side however, there's two _logical transactions_ happening: First in
UserService, the other one in InvoiceService. Spring has to be smart enough to know
that both @Transactional methods, should use the same _underlying, physical_
database transaction.
[source,java]
----
@Service
public class InvoiceService {
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void createPdf() {
// ...
}
}
----
Which basically means your code will open *two* (physical) connections/transactions
to the database. (Again: _getConnection() x2. setAutocommit(false) x2. commit()
x2_) Spring now has to be smart enough that the _two logical transactional_ pieces
(invoice()/createPdf()) now also map to two _different, physical_ database
transactions.
When looking at the Spring source code, you'll find a variety of propagation levels
or modes that you can plug into the @Transactional method.
[source,java]
----
@Transactional(propagation = Propagation.REQUIRED)
// or
@Transactional(propagation = Propagation.REQUIRES_NEW)
// etc
----
* REQUIRED
* SUPPORTS
* MANDATORY
* REQUIRES_NEW
* NOT_SUPPORTED
* NEVER
* NESTED
*Exercise:*
In the plain Java section, I showed you _everything_ that JDBC can do when it comes
to transactions. Take a minute to think about what every single Spring propagation
mode at the end _REALLY_ does to your datasource or rather, your JDBC connection.
*Answers:*
* *Required (default)*: My method needs a transaction, either open one for me or
use an existing one -> _getConnection(). setAutocommit(false). commit()_.
* *Supports*: I don't really care if a transaction is open or not, i can work
either way -> nothing to do with JDBC
* *Mandatory*: I'm not going to open up a transaction myself, but I'm going to cry
if no one else opened one up -> nothing to do with JDBC
* *Require_new:* I want my completely own transaction -> _getConnection().
setAutocommit(false). commit()_.
* *Not_Supported:* I really don't like transactions, I will even try and suspend a
current, running transaction -> nothing to do with JDBC
* *Never:* I'm going to cry if someone else started up a transaction -> nothing to
do with JDBC
* *Nested:* It sounds so complicated, but we are just talking savepoints! ->
_connection.setSavepoint()_
As you can see, most propagation modes really have nothing to do with the database
or JDBC, but more with how you structure your program with Spring and
how/when/where Spring expects transactions to be there.
[source,java]
----
public class UserService {
@Transactional(propagation = Propagation.MANDATORY)
public void myMethod() {
// execute some sql
}
}
----
In this case, Spring will _expect_ a transaction to be open, whenever you call
myMethod() of the UserService class. It _does not_ open one itself, instead, if you
call that method without a pre-existing transaction, Spring will throw an
exception. Keep this in mind as additional points for "logical transaction
handling".
This is almost a trick question at this point, but what happens when you configure
the @Transactional annotation like so?
[source,java]
----
@Transactional(isolation = Isolation.REPEATABLE_READ)
----
[source,java]
----
connection.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
----
Database isolation levels are, however, a complex topic, and you should take some
time to fully grasp them. A good start is the official Postgres Documentation and
their section on https://www.postgresql.org/docs/9.5/transaction-iso.html[isolation
levels].
Also note, that when it comes to switching isolation levels _during_ a transaction,
you *must* make sure to consult with your JDBC driver/database to understand which
scenarios are supported and which not.
[[transactional-pitfalls]]
=== The most common @Transactional pitfall
There is one pitfall that Spring beginners usually run into. Have a look at the
following code:
[source,java]
----
@Service
public class UserService {
@Transactional
public void invoice() {
createPdf();
// send invoice as email, etc.
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void createPdf() {
// ...
}
}
----
You have a UserService class with a transactional invoice method. Which calls
createPDF(), which is also transactional.
How many physical transactions would you expect to be open, once someone calls
invoice()?
Let's go back to the proxies' section of this guide. Spring creates that
transactional UserService proxy for you, but once you are inside the UserService
class and call other inner methods, there is no more proxy involved. This means, no
new transaction for you.
[ditaa,document3,png]
----
+--------------------------------+ +---------+----------
+ /------------------\
| | + @Transactional + |
|
| | + UserService Proxy | | Real
UserService |
| UserRestController | +--------------------+
|------------------|
| | | | |
|
| @Autowired | | 1. open tx | |
|
| UserService userService; | +---->| |+---->| invoice()
|
| | | |<----+| -
createPdf() |
| | | 2. close tx | |
|
+--------------------------------+ +-------------------+
+ \------------------/
----
mb_ad::spring_course[]
[[transactional-spring-boot]]
=== How to use @Transactional with Spring Boot or Spring MVC
So far, we have only talked about plain, core Spring. But what about Spring Boot?
Or Spring Web MVC? Do they handle transactions any differently?
With either frameworks (or rather: _all frameworks_ in the Spring ecosystem), you
will _always_ use the `_@Transactional_` annotation, combined with a transaction
manager and the @EnableTransactionManagement annotation. There is no other way.
The only difference with Spring Boot is, however, that it automatically sets the
`_@EnableTransactionManagement_` annotation and creates a
`_PlatformTransactionManager_` for you - with its JDBC auto-configurations. Learn
more about https://www.marcobehler.com/courses/27-spring-core-masterclass[auto-
configurations here].
The section on Spring rollbacks will be handled in the next revision of this guide.
At some point, you will want your Spring application to integrate with another
database library, such as https://hibernate.org/[Hibernate] (a popular JPA-
implementation) or https://www.jooq.org[Jooq] etc.
Let's take plain Hibernate as an example (note: it does not matter if you are using
Hibernate directly,or Hibernate via JPA).
Rewriting the UserService from before to Hibernate would look like this:
[source,java]
----
public class UserService {
@Autowired
private SessionFactory sessionFactory; // <1>
// and commit it
session.getTransaction().commit();
<1> This is a plain, old Hibernate SessionFactory, the entry-point for all
Hibernate queries.
<2> Manually managing sessions (read: database connections) and transactions with
Hibernate's API.
But we'd actually _love_ for Spring and Hibernate to integrate seamlessly, meaning
that they know about each others' transactions.
In plain code:
[source,java]
----
@Service
public class UserService {
@Autowired
private SessionFactory sessionFactory; // <1>
@Transactional
public void registerUser(User user) {
sessionFactory.getCurrentSession().save(user); // <2>
}
}
----
<1> The same SessionFactory as before
<2> But no more manual state management. Instead, getCurrentSession() and
@Transactional are _in sync_.
As always, a picture might be simpler to understand (though note, the flow between
the proxy and real service is only conceptually right and oversimplified).
[ditaa,document4,png]
----
+--------------------------------+ +---------+------------------------+
/------------------------------------\
| | + @Transactional +
| |
| | + UserService Proxy +
| Real UserService |
| HibernateTransactionManager | +----------------------------------+
|------------------------------------|
| | | |
| |
| @Autowired | | 1. sf.startTx() |
| @Transactional |
| SessionFactory sf; |+----->| 2. syncHibernateAndJdbc(ds) |
| public void hibernateMethod() { |
| | | |+----
>| hibernateDao.save(); |
| | | |
| } |
| | | |
| |
| @Autowired | | |
| |
| DataSource ds; | | |
| @Transactional |
| | | |
| public void jdbcMethod() { |
| | | |
| jdbcTemplate.save(); |
| | | |
| } |
| | | |
| |
| | | |<----
+| /* results in same tx for |
| | | 3. sf.closeTx() |
| Hibernate and plain JDBC */ |
| | | 4. desynchHibernateAndJdbc(ds) |
| |
+--------------------------------+ +---------------------------------++
\------------------------------------/
----
== Fin
By now, you should have a pretty good overview of how transaction management works
with the Spring framework and how it also applies to other Spring libraries like
Spring Boot or Spring WebMVC. The biggest takeaway should be, that it does not
matter which framework you are using in the end, it is all about the JDBC basics.
== Acknowledgements