Spring Transaction: what happens if I don’t give @Transaction annotation by method

I use Spring-Boot, Spring Rest Controller and Spring Data JPA. If I don’t specify @Transaction, then also create a post, but I would like to understand how this happens. You understand that by default, Spring adds a transaction with default parameters, but is not sure where it is added, it adds a service level or to the repository.

 public interface CustomerRepository extends CrudRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);
 }

 @Service
 public class CustomerServiceImpl implements CustomerService> {

   List<Customer> findByLastName(String lastName){
     //read operation
    }

 // What will happen if @Transaction is missing. How record get created without the annotation
  public Customer insert(Customer customer){
  // insert operations
   }
 }
+4
source share
2 answers

Spring JPA data adds @Transactional annotation at the repository level, especially in the SimpleJpaRepository class. This is the base repository class that is distributed to all Spring Data JPA repositories.

/*
     * (non-Javadoc)
     * @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
     */
    @Transactional
    public <S extends T> S save(S entity) {

        if (entityInformation.isNew(entity)) {
            em.persist(entity);
            return entity;
        } else {
            return em.merge(entity);
        }
    }
0

Spring @Transactional DAO, . , .

0

Source: https://habr.com/ru/post/1654734/


All Articles