为什么 Spring Data JPA 为共享 EntityManager 调用创建新的 EntityManager?

Sre*_*ddy 7 spring-boot

我有一个使用 spring boot + spring data JPA 的示例项目。在日志中,我观察到在进行第一次休息调用之前,EntityManager 已被创建了几次。请澄清为什么会发生这种情况。非常感谢。

Initialized JPA EntityManagerFactory for persistence unit 'ENTITY_MGR'
SpringBoot --> 10:05:40 8766 DEBUG org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler [main] - Creating new EntityManager for shared EntityManager invocation
SpringBoot --> 10:05:41 8963 DEBUG org.hibernate.stat.internal.StatisticsInitiator [main] - Statistics initialized [enabled=true]
SpringBoot --> 10:05:41 8977 TRACE org.hibernate.internal.SessionImpl [main] - Opened Session [f35fe4ac-8ce1-46b8-90dc-e456c07015c9] at timestamp: 15339099411
SpringBoot --> 10:05:41 8980 DEBUG org.springframework.orm.jpa.EntityManagerFactoryUtils [main] - Closing JPA EntityManager
SpringBoot --> 10:05:41 8982 TRACE org.hibernate.internal.SessionImpl [main] - Closing session [f35fe4ac-8ce1-46b8-90dc-e456c07015c9]
SpringBoot --> 10:05:41 8982 INFO  org.hibernate.engine.internal.StatisticalLoggingSessionEventListener [main] - 
SpringBoot --> 10:05:41 8983 TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl [main] - Releasing JDBC resources
SpringBoot --> 10:05:41 8983 TRACE org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl [main] - Closing logical connection
SpringBoot --> 10:05:41 8983 TRACE org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl [main] - Logical connection closed
SpringBoot --> 10:05:41 8985 DEBUG org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler [main] - Creating new EntityManager for shared EntityManager invocation
SpringBoot --> 10:05:41 8986 TRACE org.hibernate.internal.SessionImpl [main] - Opened Session [26018255-8491-4014-98ad-fbce0d3b5109] at timestamp: 15339099412
SpringBoot --> 10:05:41 8986 DEBUG org.springframework.orm.jpa.EntityManagerFactoryUtils [main] - Closing JPA EntityManager
SpringBoot --> 10:05:41 8986 TRACE org.hibernate.internal.SessionImpl [main] - Closing session [26018255-8491-4014-98ad-fbce0d3b5109]
SpringBoot --> 10:05:41 8986 INFO  org.hibernate.engine.internal.StatisticalLoggingSessionEventListener [main] - 
SpringBoot --> 10:05:41 8986 TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl [main] - Releasing JDBC resources
SpringBoot --> 10:05:41 8986 TRACE org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl [main] - Closing logical connection
SpringBoot --> 10:05:41 8986 TRACE org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl [main] - Logical connection closed
SpringBoot --> 10:05:41 9069 DEBUG org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler [main] - Creating new EntityManager for shared EntityManager invocation
SpringBoot --> 10:05:41 9069 TRACE org.hibernate.internal.SessionImpl [main] - Opened Session [4d3e7b5f-46fe-436f-9526-7ba62e332846] at timestamp: 15339099412
SpringBoot --> 10:05:41 9069 DEBUG org.springframework.orm.jpa.EntityManagerFactoryUtils [main] - Closing JPA EntityManager
SpringBoot --> 10:05:41 9069 TRACE org.hibernate.internal.SessionImpl [main] - Closing session [4d3e7b5f-46fe-436f-9526-7ba62e332846]
SpringBoot --> 10:05:41 9069 INFO  org.hibernate.engine.internal.StatisticalLoggingSessionEventListener [main] - 
SpringBoot --> 10:05:41 9070 TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl [main] - Releasing JDBC resources
SpringBoot --> 10:05:41 9070 TRACE org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl [main] - Closing logical connection
SpringBoot --> 10:05:41 9070 TRACE org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl [main] - Logical connection closed
SpringBoot --> 10:05:41 9248 DEBUG org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler [main] - Creating new EntityManager for shared EntityManager invocation
SpringBoot --> 10:05:41 9248 TRACE org.hibernate.internal.SessionImpl [main] - Opened Session [1b5f5936-fa7f-42cd-831c-78570ace0f58] at timestamp: 15339099414
SpringBoot --> 10:05:41 9248 DEBUG org.springframework.orm.jpa.EntityManagerFactoryUtils [main] - Closing JPA EntityManager
SpringBoot --> 10:05:41 9248 TRACE org.hibernate.internal.SessionImpl [main] - Closing session [1b5f5936-fa7f-42cd-831c-78570ace0f58]
SpringBoot --> 10:05:41 9249 INFO  org.hibernate.engine.internal.StatisticalLoggingSessionEventListener [main] - 
SpringBoot --> 10:05:41 9249 TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl [main] - Releasing JDBC resources
SpringBoot --> 10:05:41 9249 TRACE org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl [main] - Closing logical connection
SpringBoot --> 10:05:41 9249 TRACE org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl [main] - Logical connection closed
Run Code Online (Sandbox Code Playgroud)

小智 1

我也想知道同样的事情,这就是我的发现。

在 Spring 中的许多情况下,它被用作字段的代理EntityManager。我想这样做的原因是,与非线程安全的EntityManager接口的实际实现相比,代理被认为是线程安全的

例如,CrudRepository接口 -> SimpleJpaRepository的默认实现具有类型为 的字段EntityManager。在大多数情况下,我们只想扩展CrudRepository接口并将其作为单例 bean 注入某处。由于它是单例,因此EntityManager必须是线程安全的,因为可以从多个线程调用存储库。

在该类的文档中,SharedEntityManagerCreator我们可以看到这个代理的实际工作原理。

共享 EntityManager 的行为就像从应用程序服务器的 JNDI 环境中获取的 EntityManager 一样,如 JPA 规范所定义。它将把所有调用委托给当前事务性 EntityManager(如果有);否则它将回退到每个操作新创建的 EntityManager。

这意味着,如果创建了 EntityManager(会话),代理将使用它,如果没有,它将根据您的需要创建一个新的 EntityManager(会话)。

当我们启动 Spring Boot 应用程序时,Spring 会调用一些 EntityManager代理,但这些EntityManager是代理,因为我们没有创建真正的 EntityManager(会话),所以它会为我们创建一个并记录您请求的这些消息。

因此,在您的情况下,EntityManager创建这些共享可能是因为您Repository在项目中定义了接口,并且在初始化期间调用了EntityManager这些存储库中的(代理)。