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/

  • 相关阅读:
    WM_MOUSEWHEEL消息的处理
    Delphi之TStrings和TStringLists类[转]
    使用mysqladmin命令修改Mysql密码
    AP_发票与预付款核销时预付款带税码
    ORACLE EBS AP发票到付款的数据流
    .关于货币大写的探索
    Oracle SQL 空值排序(Nulls)
    实现主从关系Form中汇总行金额/数量
    巧妙的查看FORM中的 LOV查询语句
    供应商接口的使用
  • 原文地址:https://www.cnblogs.com/wfy680/p/15033877.html
Copyright © 2011-2022 走看看