如何在使用Open-Session in View时避免性能损失?


Open-Session In View会在你即使没有使用惰性实体情况下加载且初始化并获取它们,这会导致严重的性能损失。
Open-Session in View 反模式在Spring Boot中默认是激活的。如果您更喜欢使用它,那么需要尝试尽可能减轻性能损失:一种优化是将标记Connection设置为只读,这将允许数据库服务器避免写入事务日志;另一个优化包括在您不希望某些实体被懒惰地初始化时,显式明确设置实体。

1. application.properties配置:

spring.datasource.url=jdbc:mysql://localhost:3306/db_tennis?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

spring.datasource.initialization-mode=always
spring.datasource.platform=mysql

# this is default anyway
spring.jpa.open-in-view=true

2. 父实体Tournament ,注意setXXX方法:

@Entity
public class Tournament implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @OneToMany(cascade = CascadeType.ALL, 
            mappedBy = "tournament", orphanRemoval = true)
    @JsonManagedReference
    private List<TennisPlayer> tennisPlayers = new ArrayList<>();

   
//注意这里,手工明确设置子实体集合
    public void setTennisPlayers(List<TennisPlayer> tennisPlayers) {
        this.tennisPlayers = tennisPlayers;
    }
}

子实体TennisPlayer :

@Entity
public class TennisPlayer implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tournament_id")
    @JsonBackReference
    private Tournament tournament;

 
}


3. 获取实体并显式设置惰性属性,通过调用tournament.setTennisPlayers,可以在服务或控制器层中执行此操作,具体取决于它更适合您的情况,但在显式事务之外:

  @RequestMapping("/tournament_no_players")
    public Tournament tournamentWithoutPlayers() {

        Tournament tournament = tennisService.fetchTournament();
       
// 显式明确设置实体:explicitly set Players of the Tournament
       
// 为了避免从数据库中获取它们
        tournament.setTennisPlayers(Collections.emptyList());

        return tournament;
    }

这为什么有效?为什么我们可以设置托管实体的属性而不触发刷新?那么,答案可以在其文档中找到OpenSessionInViewFilter :
 注意:默认情况下,此过滤器不会刷新Hibernate会话,刷新模式设置为FlushMode.NEVER。它假定与关注刷新的服务层事务结合使用:活动事务管理器将临时更改刷新在读写事务期间模式为FlushMode.AUTO,使用刷新模式,在每个事务结束时重置为FlushMode.NEVER。如果您打算在没有事务的情况下使用此过滤器,请考虑更改默认刷新模式(flushMode属性)

下图上面是通过默认OSIV强制加载,结果执行了两条SQL语句,它将子实体集合也加载了。下图是使用上面方式之后,
源代码可以在这里找到