springboot创建多实体管理器

djp7away  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(414)

在spring boot应用程序中,当我启动它时,我会看到68次这一行:

tor$SharedEntityManagerInvocationHandler [main] - Creating new EntityManager for shared EntityManager invocation

我有8个jpa实体。
为什么要创建这么多实体管理器?
编辑

spring.jpa.open-in-view=false
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=false

spring.datasource.url=jdbc:oracle:thin:@localhost:1521/cnn
spring.datasource.username=cnn
spring.datasource.password=test
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maximum-pool-size=5

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true

spring.jpa.generate-ddl=false

spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.use-new-id-generator-mappings=true
z5btuh9x

z5btuh9x1#

2
容器管理的实体管理器与当前jta事务一起自动传播,Map到同一持久性单元的entitymanager引用提供对该事务中持久性上下文的访问。因此,从单例共享实体管理器不是一个好的做法,除了并发性问题之外,这将导致对bean调用的每个方法使用相同的事务上下文。一个简单的解决方案是在bean中注入entitymanagerfactory引用,并创建调用createentitymanager()方法的entitymanager对象。缺点是您应该手动管理事务,而不再依赖容器。否则,另一种方法可以是将所有实体管理器注入到主企业bean中,并使用传递适当管理器的方法在服务bean中实现业务逻辑

相关问题