zoukankan      html  css  js  c++  java
  • Spring Data JPA事务支持

    一 点睛

    Spring Data JPA对所有默认方法都开启了事务支持,且查询类事务默认启用readOnly=true属性。

    二 SimpleJpaRepository缩减版源码

    @Repository
    @Transactional(readOnly = true)
    public class SimpleJpaRepository<T, ID extends Serializable> implements JpaRepository<T, ID>,
            JpaSpecificationExecutor<T> {
        ......
        @Transactional
        public void delete(ID id) {
        ......
        }
     
        @Transactional
        public void delete(T entity) {
        ......
        }
     
     
        @Transactional
        public void delete(Iterable<? extends T> entities) {
        ......
        }
     
     
        @Transactional
        public void deleteInBatch(Iterable<T> entities) {
        ......
        }
     
     
        @Transactional
        public void deleteAll() {
        ......
        }
     
     
        @Transactional
        public void deleteAllInBatch() {
        ......
        }
     
     
        public T findOne(ID id) {
        ......
        }
     
     
        protected Map<String, Object> getQueryHints() {
        ......
        }
     
        private JpaEntityGraph getEntityGraph() {
        ......
        }
     
     
        @Override
        public T getOne(ID id) {
        ......
        }
     
     
        public boolean exists(ID id) {
        ......
        }
     
     
        public List<T> findAll() {
        ......
        }
     
     
        public List<T> findAll(Iterable<ID> ids) {
        ......
        }
     
     
        public List<T> findAll(Sort sort) {
        ......
        }
     
     
        public Page<T> findAll(Pageable pageable) {
        ......
        }
     
     
        public T findOne(Specification<T> spec) {
        ......
        }
     
     
        public List<T> findAll(Specification<T> spec) {
        ......
        }
     
     
        public Page<T> findAll(Specification<T> spec, Pageable pageable) {
        ......
        }
     
     
        public List<T> findAll(Specification<T> spec, Sort sort) {
        ......
        }
     
     
        public long count() {
        ......
        }
     
     
        public long count(Specification<T> spec) {
     
        ......
        }
     
     
        @Transactional
        public <S extends T> S save(S entity) {
        ......
        }
     
     
        @Transactional
        public <S extends T> S saveAndFlush(S entity) {
        ......
        }
     
        @Transactional
        public <S extends T> List<S> save(Iterable<S> entities) {
        ......
        }
     
        ......
    }

    三 源码分析

    从源码分析可以看出,SimpleJpaRepository在类级别定义了@Transactional(readOnly = true),而在和save、delete相关的操作重写了@Transactional属性,此时readOnly属性是false,其余查询操作readOnly仍然为true。

  • 相关阅读:
    20210110-正则表达式
    20210105
    C# Expression 树转化为SQL语句(一)
    5000行js db
    Keras智能
    nginx 设置多个tcp IP代理 socket 转发
    FTP连接时出现“227 Entering Passive Mode” 的解决方法
    windows nginx TCP代理 负载均衡
    nginx 代理ftp
    Intellij IDEA添加项目依赖
  • 原文地址:https://www.cnblogs.com/zouhong/p/13289130.html
Copyright © 2011-2022 走看看