Transactionality

Transactionality

By default, methods inherited from CrudRepository inherit the transactional configuration from SimpleJpaRepository. For read operations, the transaction configuration readOnly flag is set to true. All others are configured with a plain @Transactional so that default transaction configuration applies. Repository methods that are backed by transactional repository fragments inherit the transactional attributes from the actual fragment method.

If you need to tweak transaction configuration for one of the methods declared in a repository, redeclare the method in your repository interface, as follows:

Example 1. Custom transaction configuration for CRUD

public interface UserRepository extends CrudRepository<User, Long> {

  @Override
  @Transactional(timeout = 10)
  public List<User> findAll();

  // Further query method declarations
}        


Doing so causes the findAll() method to run with a timeout of 10 seconds and without the readOnly flag.

Another way to alter transactional behaviour is to use a facade or service implementation that (typically) covers more than one repository. Its purpose is to define transactional boundaries for non-CRUD operations. The following example shows how to use such a facade for more than one repository:

Example 2. Using a facade to define transactions for multiple repository calls

@Service
public class UserManagementImpl implements UserManagement {

  private final UserRepository userRepository;
  private final RoleRepository roleRepository;

  public UserManagementImpl(UserRepository userRepository,
    RoleRepository roleRepository) {
    this.userRepository = userRepository;
    this.roleRepository = roleRepository;
  }

  @Transactional
  public void addRoleToAllUsers(String roleName) {

    Role role = roleRepository.findByName(roleName);

    for (User user : userRepository.findAll()) {
      user.addRole(role);
      userRepository.save(user);
    }
  }
}!        

This example causes call to addRoleToAllUsers(…) to run inside a transaction (participating in an existing one or creating a new one if none are already running). The transaction configuration at the repositories is then neglected, as the outer transaction configuration determines the actual one used. Note that you must activate <tx:annotation-driven /> or use @EnableTransactionManagement explicitly to get annotation-based configuration of facades to work. This example assumes you use component scanning.

Note that the call to save is not strictly necessary from a JPA point of view, but should still be there in order to stay consistent to the repository abstraction offered by Spring Data.

Transactional query methods

Declared query methods (including default methods) do not get any transaction configuration applied by default. To run those methods transactionally, use @Transactional at the repository interface you define, as shown in the following example:

Example 3. Using @Transactional at query methods

@Transactional(readOnly = true)
interface UserRepository extends JpaRepository<User, Long> {

  List<User> findByLastname(String lastname);

  @Modifying
  @Transactional
  @Query("delete from User u where u.active = false")
  void deleteInactiveUsers();
}        


Typically, you want the readOnly flag to be set to true, as most of the query methods only read data. In contrast to that, deleteInactiveUsers() makes use of the @Modifying annotation and overrides the transaction configuration. Thus, the method runs with the readOnly flag set to false.




Example when calling Stored procedure

  @GetMapping("/SP/{userage}")
  @Transactional(readOnly = true)
  public ResponseEntity<Integer> getUserBySP(@PathVariable("userage") int userage){        


You can use transactions for read-only queries and mark them as such by setting the readOnly flag. Doing so does not, however, act as a check that you do not trigger a manipulating query (although some databases reject INSERT and UPDATE statements inside a read-only transaction). The readOnly flag is instead propagated as a hint to the underlying JDBC driver for performance optimizations. Furthermore, Spring performs some optimizations on the underlying JPA provider. For example, when used with Hibernate, the flush mode is set to NEVER when you configure a transaction as readOnly, which causes Hibernate to skip dirty checks (a noticeable improvement on large object trees).

要查看或添加评论,请登录

Ahmed Abdelaziz的更多文章

  • Java 23

    Java 23

    New and updated Java language features, core API, and the JVM – Java 23 packs it all – for new Java developers to…

  • Kafka Producer And Consumer In Spring Boot

    Kafka Producer And Consumer In Spring Boot

    Appache Kafka is a distributed event streaming platform that is widely used for handling real-time data streams in…

  • Docker with Spring Boot in a simple way

    Docker with Spring Boot in a simple way

    This guide walks you through the process of building a Docker image for running a Spring Boot application. We start…

  • Quarkus Framework and Comparison with Spring Boot

    Quarkus Framework and Comparison with Spring Boot

    In this article, we’ll give an overview of the Quarkus framework and compare it with Spring Boot – the most popular…

  • Spring AI

    Spring AI

    Spring AI supports ChatGPT, the AI language model by OpenAI. ChatGPT has been instrumental in sparking interest in…

  • Aspect Oriented Programming and AOP in Spring Framework

    Aspect Oriented Programming and AOP in Spring Framework

    Aspect-Oriented Programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation…

    2 条评论
  • Spring Boot Caching with Redis

    Spring Boot Caching with Redis

    Spring Boot Cache Providers Cache providers allow us to transparently and clearly configure the cache in the…

  • The DispatcherServlet

    The DispatcherServlet

    The Engine of Request Handling in Spring Boot. 1.

  • Spring Data with MongoDB

    Spring Data with MongoDB

    1. Overview This article will be a quick and practical introduction to Spring Data MongoDB.

  • What is the use of @Qualifier annotation in Spring?

    What is the use of @Qualifier annotation in Spring?

    he use of @Qualifier is to support the @Autowired annotation when it needs help. Typically, @Autowired can implicitly…

社区洞察

其他会员也浏览了