{{announcement.body}}
{{announcement.title}}

Transaction Savepoints in Spring JDBC

DZone 's Guide to

Transaction Savepoints in Spring JDBC

This post will look at how you can use savepoints within Spring JDBC.

· Database Zone ·
Free Resource

Savepoints allow you to create markers within a transaction that you can rollback to, without preventing the transaction from being committed at a later point. These can be treated like intermediate transactions within a single overarching transaction. At the end of the day, you either commit the transaction and persist all the changes to the database or rollback everything. Using savepoints, you can handle potential database errors and return to a safe point within the transaction and carry on.

This post will look at how you can use savepoints within Spring JDBC.

Spring provides a few options to manage savepoints within your transactions, we will look at the following choices:

  • Using JdbcTemplate
  • Using TransactionTemplate
  • The @Transactional annotation

Savepoints With JdbcTemplate

Let’s start with the JdbcTemplate:

Kotlin
 




x
31


 
1
@Component
2
class PersonManager(private val jdbcTemplate: JdbcTemplate) {
3
 
           
4
 
           
5
  fun process() {
6
    jdbcTemplate.execute { connection: Connection ->
7
      connection.autoCommit = false
8
 
           
9
      connection.save(1, dan)
10
      connection.save(2, laura)
11
 
           
12
      val savepoint = connection.setSavepoint()
13
      try {
14
        connection.save(1, george)
15
      } catch (e: SQLException) {
16
        log.info("There was an exception, rolling back to savepoint: ${e.message}")
17
        connection.rollback(savepoint)
18
      } finally {
19
        connection.releaseSavepoint(savepoint)
20
      }
21
    }
22
  }
23
 
           
24
  private fun Connection.save(id: Int, person: Person) {
25
    prepareStatement("INSERT INTO people(id, name, age) VALUES (?, ?, ?)").apply {
26
      setInt(1, id)
27
      setString(2, person.name)
28
      setInt(3, person.age)
29
    }.executeUpdate()
30
  }
31
}



Which outputs the following (the same is output in all following sections of this post):

Java
 




x


 
1
There was an exception, rolling back to savepoint: ERROR: duplicate key value violates unique constraint "people_pkey"
2
People => [Person(id=1, name=Dan, age=26), Person(id=2, name=Laura, age=25)]



Enabling savepoints using JdbcTemplate requires a bit of effort. You need to make sure that you disable the Connection’s autoCommit mode, otherwise you will see an error like the following:

Java
 




xxxxxxxxxx
1
15


 
1
Caused by: org.springframework.jdbc.UncategorizedSQLException: ConnectionCallback; uncategorized SQLException; SQL state [25P01]; error code [0]; 
2
    Cannot establish a savepoint in auto-commit mode.; nested exception is org.postgresql.util.PSQLException: Cannot establish a savepoint in auto-commit mode.
3
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:89) ~[spring-jdbc-5.2.0.RELEASE.jar:5.2.0.RELEASE]
4
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.2.0.RELEASE.jar:5.2.0.RELEASE]
5
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.2.0.RELEASE.jar:5.2.0.RELEASE]
6
    at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1443) ~[spring-jdbc-5.2.0.RELEASE.jar:5.2.0.RELEASE]
7
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:336) ~[spring-jdbc-5.2.0.RELEASE.jar:5.2.0.RELEASE]
8
    at dev.lankydan.jdbc.PersonManager.processWithJdbcTemplate(Application.kt:73) ~[main/:na]
9
    at dev.lankydan.jdbc.PersonManager$$FastClassBySpringCGLIB$$56c95970.invoke(<generated>) ~[main/:na]
10
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.0.RELEASE.jar:5.2.0.RELEASE]
11
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:685) ~[spring-aop-5.2.0.RELEASE.jar:5.2.0.RELEASE]
12
    at dev.lankydan.jdbc.PersonManager$$EnhancerBySpringCGLIB$$d54410c6.processWithJdbcTemplate(<generated>) ~[main/:na]
13
    at dev.lankydan.jdbc.Application.run(Application.kt:32) ~[main/:na]
14
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
15
    ... 6 common frames omitted



After that is handled, you can create a savepoint using Connection.setSavepoint.

SQL statements can then be executed as usual. Any pieces of code that you think are going to cause issues (but will only lead to soft failures) should be wrapped with a try/catch block. If any errors occur, they can then be handled within the catch. This is what the code example above demonstrates.

An attempt to save a record with an ID that already exists is made thus, throwing an exception. Without the savepoint, this would put the transaction into a terminal state, preventing it from ever successfully committing. Instead, as long as the exception is caught, the transaction can be rolled back to the savepoint. This is achieved by calling rollback while referencing the savepoint. The transaction can then continue as usual and achieve its goal of committing its contents to the database.

Before committing the transaction, its worth releasing the savepoint using releaseSavepont. Saying that, not all the JDBC drivers actually support releasing savepoints. The Javadocs note that SQLFeatureNotSupportedException is thrown by drivers that do not support releaseSavepoint. So, you might need to keep your eye out for these potential errors.

Savepoints With TransactionTemplate

TransactionTemplate works similarly to JdbcTemplate but with a greater focus on support for transactions, the name gives it away, right?

Kotlin
 




xxxxxxxxxx
1
32


 
1
@Component
2
class PersonManager(
3
  private val jdbcTemplate: JdbcTemplate,
4
  private val transactionTemplate: TransactionTemplate
5
) {
6
 
           
7
  fun process() {
8
    transactionTemplate.execute { status: TransactionStatus ->
9
      jdbcTemplate.save(1, dan)
10
      jdbcTemplate.save(2, laura)
11
 
           
12
      val savepoint = status.createSavepoint()
13
      try {
14
        jdbcTemplate.save(1, george)
15
      } catch (e: DataAccessException) {
16
        // [DataAccessException] because spring converts the underlying [SQLException] inside of [update]
17
        log.info("There was an exception, rolling back to savepoint: ${e.message}")
18
        status.rollbackToSavepoint(savepoint)
19
      } finally {
20
        status.releaseSavepoint(savepoint)
21
      }
22
    }
23
  }
24
 
           
25
  private fun JdbcTemplate.save(id: Int, person: Person) {
26
    update("INSERT INTO people(id, name, age) VALUES (?, ?, ?)") { statement ->
27
      statement.setInt(1, id)
28
      statement.setString(2, person.name)
29
      statement.setInt(3, person.age)
30
    }
31
  }
32
}



This code is very similar to that shown in the JdbcTemplate example. The outcome is exactly the same, and with only a few differences between them. TransactionTemplate provides support for transactions which is not offered by JdbcTemplate without the code shown before.

To use savepoints with TransactionTemplate, you should interact with the TransactionStatus passed into the execute lambda. This allows you to create a savepoint using createSavepoint, rollback with rollbackToSavepoint and then release it by calling releaseSavepoint. Not much else to say really.

Note that JdbcTemplate is used within TransactionTemplate.execute and the underlying Connection is not being directly interacted with.

Savepoints With @Transactional

This option is the one that stands out from the others. Focusing on creating and committing transactions through the use of an annotation:

Kotlin
 




xxxxxxxxxx
1
37


 
1
@Component
2
class PersonManager(
3
  private val jdbcTemplate: JdbcTemplate,
4
  private val transactionalAnnotationManager: TransactionalAnnotationPersonManager
5
) {
6
 
           
7
  @Transactional
8
  fun process() {
9
    jdbcTemplate.save(1, dan)
10
    jdbcTemplate.save(2, laura)
11
 
           
12
    try {
13
      transactionalAnnotationManager.processWithTransactionalAnnotationSavepoint()
14
    } catch (e: DataAccessException) {
15
      log.info("There was an exception, rolling back to savepoint: ${e.message}")
16
    }
17
  }
18
}
19
 
           
20
@Component
21
class TransactionalAnnotationPersonManager(private val jdbcTemplate: JdbcTemplate) {
22
 
           
23
  // By default, auto rollbacks for runtime exceptions but not checked exceptions
24
  // [DataAccessException] is a runtime exception, hence the rollback
25
  @Transactional(propagation = Propagation.NESTED)
26
  fun processWithSavepoint() {
27
    jdbcTemplate.save(1, george)
28
  }
29
}
30
 
           
31
private fun JdbcTemplate.save(id: Int, person: Person) {
32
  update("INSERT INTO people(id, name, age) VALUES (?, ?, ?)") { statement ->
33
    statement.setInt(1, id)
34
    statement.setString(2, person.name)
35
    statement.setInt(3, person.age)
36
  }
37
}



This example looks nothing like the first two.

@Transactional enables transaction management starting from the first function called with the annotation. Subsequent calls to JdbcTemplate functions will be executed within a transaction created by the function annotated with @Transactional. This seems to be the recommended way of enabling transactions within Spring JDBC.

Enabling savepoints with @Transactional is a bit strange. To explain why, you first need a bit of background context. When a Spring component contains a function annotated with @Transactional, a proxy instance is generated that stands in front of the original class (PersonManager and TransactionalAnnotationPersonManager in the example above). Calls to these classes will instead communicate with the proxy, which wraps the original function calls in transactions. The proxy only intercepts calls from external classes. Any calls from other functions within the class will not apply the transaction wrapping that the proxy does.

This makes it harder to enable savepoints when using @Transactional. If there is a call to a function marked with @Transactional(propagation = Propagation.NESTED) (which is what denotes the use of a savepoint), it will not do anything unless called from an external class (outside the proxy). This is why there are two classes in the example. One creates the original, overarching transaction. The other will create an inner transaction (using a savepoint) when called from an existing transaction.

When you call functions in this particular way, savepoints and the overall transaction will be managed for you.

Despite there being two classes, the amount of code isn’t particularly bad. That being said, its a bit confusing at first glance and it hides a lot behind a curtain while waving its hands and saying “behold Spring’s magic”. I am generally okay with the Spring’s magic, but this one was too much for me.

Summary

Spring provides you with multiple ways to manage savepoints within transactions:

  • Using JdbcTemplate and controlling the Connection gives you
  • Leveraging TransactionTemplate and the abstraction it provides over the above
  • Annotating functions with @Transactional(propagation = Propagation.NESTED) to have Spring sprinkle some magic for you

If you enjoyed this post or found it helpful (or both) then please feel free to follow me on Twitter at @LankyDanDev and remember to share with anyone else who might find this useful!

Topics:
database, java, jdbctemplate, kotlin, spring, spring data, spring jdbc, sql, tutorial

Published at DZone with permission of Dan Newton , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}