Search code examples
spring-bootjpatransactionsspring-data-jpaspring-transactions

When does a transaction get commited (by default)?


I am writing a pretty small spring boot application, without any special transaction related properties. My REST controller calls save(myEntity) on the repository.

When will database transaction be committed?

Will it be committed

  • as part of the save method? (probably too early, an exception could rise up later)
  • when my own code is done and returns to the spring boot code?
  • even later, when the serialization of the result is done?
  • or is this depending on the database driver?

Example code dbtransaction/Application.java:

package dbtransaction;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.UUID;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@RestController
@RequestMapping("/thing")
class ThingRestController {

    private ThingRepository thingRepository;

    public ThingRestController(ThingRepository thingRepository) {
        this.thingRepository = thingRepository;
    }

    @RequestMapping(method = RequestMethod.GET)
    public Thing createNew() {
        return thingRepository.save(new Thing());
    }
}

@Repository
interface ThingRepository extends JpaRepository<Thing, String> {
}

@Entity
class Thing {
    @Id
    public UUID uuid = UUID.randomUUID();
}

Example pom pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dbtransaction</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.23.1-GA</version>
        </dependency>
    </dependencies>

</project>

Solution

  • When you commit depends on your transactional scope. In the code example you provided, you did not specify any transactional scope so it will default to your repository method; it will commit after your call thingRepository.save.

    Note that the default implementation for JpaRepository is SimpleJpaRepository and if you look at that class, you'll see it annotates its save method with @Transactional. That, therefore, becomes your default boundary of your transaction. You can easily change your transactional scope by specifying @Transactional on an outer method.