【发布时间】:2015-11-02 09:58:22
【问题描述】:
这里是休眠新手。
据我了解,一级缓存仅在会话打开时可用。当会话关闭时,第一级中的所有缓存实体都将被逐出/删除。这是正确的吗?
我有一个使用 Hibernate 框架用 Java 开发的简单 CRUD 应用程序。每次我的应用程序启动、加载和执行它的第一个查询操作时,执行时间通常比后续的查询操作长。第一个查询通常需要 17 毫秒才能执行,而后续查询通常需要 1-2 毫秒。
我的问题是,这真的是 Hibernate 在应用程序启动时的行为吗?从第一个查询操作加载的数据是否存储在某处的缓存中? (绝对不是会话缓存,因为在执行我的第一个查询操作后,会话立即关闭)急切加载会影响这种行为吗?
我真的不知道从哪里开始,因为 Hibernate 文档没有涵盖这一点。如果我错了,请纠正我。
感谢您的帮助,因为我真的不知道从哪里开始阅读此内容。
编辑:有关更多信息,这是第一个和第二个查询操作的休眠统计信息:
第一:
100222 nanoseconds spent acquiring 1 JDBC connections; 0 nanoseconds spent releasing 0 JDBC connections; 23238430 nanoseconds spent preparing 3 JDBC statements; 8333256 nanoseconds spent executing 3 JDBC statements; 0 nanoseconds spent executing 0 JDBC batches; 0 nanoseconds spent performing 0 L2C puts; 0 nanoseconds spent performing 0 L2C hits; 0 nanoseconds spent performing 0 L2C misses; 40215588 nanoseconds spent executing 1 flushes (flushing a total of 3 entities and 3 collections); 135213 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
第二:
168597 nanoseconds spent acquiring 1 JDBC connections; 0 nanoseconds spent releasing 0 JDBC connections; 2332976 nanoseconds spent preparing 3 JDBC statements; 6427565 nanoseconds spent executing 3 JDBC statements; 0 nanoseconds spent executing 0 JDBC batches; 0 nanoseconds spent performing 0 L2C puts; 0 nanoseconds spent performing 0 L2C hits; 0 nanoseconds spent performing 0 L2C misses; 1095389 nanoseconds spent executing 1 flushes (flushing a total of 3 entities and 3 collections); 17600 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)查询执行相同,但执行时间长度不同。
【问题讨论】:
-
可能是第一个查询触发持久化单元被初始化,数据源被容器初始化,连接池被填充。
-
@Gimby 所有这些都需要比 17 毫秒更长的时间。
-
@DraganBozanovic 取决于我的本地 glassfish/payara 安装,持久性单元很快就设置好了,但它在第一次使用时会发生。这可能取决于服务器按需物理设置的内容以及不设置的内容。我的观点是第一次使用初始化命中并不少见。
标签: java hibernate postgresql caching