有人能用简单的话解释一下Hibernate/NHibernate中的一级缓存和二级缓存是什么吗?
当前回答
1.1)一级缓存
一级缓存始终与Session对象关联。Hibernate默认使用这个缓存。这里,它处理一个 一个接一个的交易,意味着不会处理一个交易很多 次了。主要是它减少了SQL查询的数量 在给定事务中生成。这代替了之后的更新 在事务中所做的每一次修改,都会更新事务 只有在交易结束时。
1.2)二级缓存
Second-level cache always associates with the Session Factory object. While running the transactions, in between it loads the objects at the Session Factory level, so that those objects will be available to the entire application, not bound to single user. Since the objects are already loaded in the cache, whenever an object is returned by the query, at that time no need to go for a database transaction. In this way the second level cache works. Here we can use query level cache also.
引用自:http://javabeat.net/introduction-to-hibernate-caching/
其他回答
一级缓存
会话对象保存第一级缓存数据。默认启用。第一级缓存数据将对整个应用程序不可用。一个应用程序可以使用多个会话对象。
二级缓存
SessionFactory对象保存二级缓存数据。存储在第二级缓存中的数据将对整个应用程序可用。但是我们需要显式地启用它。
1.1)一级缓存
一级缓存始终与Session对象关联。Hibernate默认使用这个缓存。这里,它处理一个 一个接一个的交易,意味着不会处理一个交易很多 次了。主要是它减少了SQL查询的数量 在给定事务中生成。这代替了之后的更新 在事务中所做的每一次修改,都会更新事务 只有在交易结束时。
1.2)二级缓存
Second-level cache always associates with the Session Factory object. While running the transactions, in between it loads the objects at the Session Factory level, so that those objects will be available to the entire application, not bound to single user. Since the objects are already loaded in the cache, whenever an object is returned by the query, at that time no need to go for a database transaction. In this way the second level cache works. Here we can use query level cache also.
引用自:http://javabeat.net/introduction-to-hibernate-caching/
在streamlined Logic博客上有一个关于一级缓存的很好的解释。
基本上,第一级缓存发生在每个会话的基础上,而第二级缓存可以跨多个会话共享。
by default, NHibernate uses first level caching which is Session Object based. but if you are running in a multi-server environment, then the first level cache may not very scalable along with some performance issues. it is happens because of the fact that it has to make very frequent trips to the database as the data is distributed over multiple servers. in other words NHibernate provides a basic, not-so-sophisticated in-process L1 cache out of box. However, it doesn’t provide features that a caching solution must have to have a notable impact on the application performance.
所以所有这些问题的问题是使用与会话工厂对象相关的L2缓存。它减少了访问数据库的时间消耗,因此最终增加了应用程序的响应时间。
在二级缓存中,域hbm文件的键可以是可变的,值可以是false。 例如, 在这个领域类中,一天中的某些持续时间作为普遍真理保持不变。因此,它可以在应用程序中被标记为不可变。
推荐文章
- 如何在JSON中使用杰克逊更改字段名
- 在Hibernate中重新连接分离对象的正确方法是什么?
- 一对多、多对一、多对多的区别?
- Hibernate中不同的保存方法之间有什么区别?
- 有人能解释一下JPA和Hibernate中的mappedBy吗?
- 使Hibernate忽略未映射的实例变量
- javax.transaction.Transactional vs . org.springframework.transaction.annotation.Transactional
- buildSessionFactory()配置方法在Hibernate中已弃用?
- JPA orphanRemoval=true如何不同于ON DELETE CASCADE DML子句
- 如何映射一个组合键与JPA和Hibernate?
- 如何使用旧版本的Hibernate(~2009)来计算行数?
- 我如何使JPA OneToOne关系变懒
- 混淆:@NotNull vs. @Column(nullable = false)与JPA和Hibernate
- Hibernate的第一级和第二级缓存是什么?
- Hibernate抛出org.hibernate.AnnotationException:没有为entity指定标识符:com..domain.idea.MAE_MFEView