zoukankan      html  css  js  c++  java
  • 使用Spring Data JPA自定义update执行慢的问题

    https://blog.csdn.net/wherwh/article/details/89380494

    public interface ParaRepository extends JpaRepository<Para,String>{
        @Modifying
        @Transactional 
        @Query(nativeQuery = true, value = "update clr_para set item_value = ?2 where item_name = ?1")
        int update(String itemName, String itemValue);
    }

    调用执行:

    @Transactional
    public void update() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        this.lastClearingDate = this.clearDate;
        paraRepository.update(ParaItemType.LAST_CLEARING_DATE.name().toLowerCase(),sdf.format(this.lastClearingDate));
    }

    该函数只是修改一个配置项记录. 但是,耗时30多秒.

    解决方法一:采用JdbcTemplate执行同样的命令,耗时42毫秒.测试代码如下:

    @Transactional
    public void update() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        this.lastClearingDate = this.clearDate;
        
        JdbcTemplate jdbcTemplate = (JdbcTemplate)Application.getBean(JdbcTemplate.class);
        String sql = String.format("update clr_para set item_value='%s' where item_name='last_clearing_date'",sdf.format(this.lastClearingDate));
        jdbcTemplate.execute(sql);
    }

    解决方法二:不使用Native Query【差别:nativeQuery = false. value采用JPQL方法.】

    public interface ParaRepository extends JpaRepository<Para,String>{
        @Modifying
        @Transactional 
        @Query(nativeQuery = false, value = "update Para set itemValue = ?2 where itemName = ?1")
        int update(String itemName, String itemValue);
    }

    执行Native Query之前执行了flush.

    见下文

    How does AUTO flush strategy work in JPA and Hibernate
    https://vladmihalcea.com/how-does-the-auto-flush-work-in-jpa-and-hibernate/

  • 相关阅读:
    iOS中Zbar二维码扫描的使用
    SOJ 1135. 飞跃原野
    SOJ 1048.Inverso
    SOJ 1219. 新红黑树
    SOJ 1171. The Game of Efil
    SOJ 1180. Pasting Strings
    1215. 脱离地牢
    1317. Sudoku
    SOJ 1119. Factstone Benchmark
    soj 1099. Packing Passengers
  • 原文地址:https://www.cnblogs.com/wfy680/p/15033877.html
Copyright © 2011-2022 走看看