Spring boot + JPA - SharedEntityManagerCreator在启动时打开会话

时间:2017-01-10 16:54:22

标签: java spring hibernate spring-mvc jpa

给出以下设置:

  • Spring boot 1.4
  • Hibernate 5 /多租户是ON / MultiTenancyStrategy是DataBase。
  • spring.jpa.open-in-view=truespring.jpa.properties.hibernate.hbm2ddl.auto=nonespring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=falsespring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

启用跟踪时,我注意到hibernate会在启动时打开n个会话。

n数字奇怪等于@org.springframework.transaction.annotation.Transactional(readOnly=true)注释的方法数。

我还注意到负责打开这些会话的组件是org.springframework.orm.jpa.SharedEntityManagerCreator,正好是以下代码部分:

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    //...

    boolean isNewEm = false;
    if (target == null) {
        logger.debug("Creating new EntityManager for shared EntityManager invocation");
        target = (!CollectionUtils.isEmpty(this.properties) ?
                this.targetFactory.createEntityManager(this.properties) :
                this.targetFactory.createEntityManager());
        isNewEm = true;
    }

    //...
}

这里有一个link截图,显示了被代理的方法。

话虽如此,我的问题是:

是否有任何对此行为的解释,因为我希望在应用程序完全启动之前不会打开任何会话,或者在启动期间需要数据操作操作(加载架构)(这是不是我的情况)...

0 个答案:

没有答案