需求:演示hibernate的session缓存机制
上代码:
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--数据库连接-->
<property name="connection.url">jdbc:mysql:///</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!--Hibernate配置-->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!--设置Hibernate的事务的隔离级别-->
<property name="connection.isolation">2</property>
<mapping resource="cn/limbo/hibernate/entity/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
News.java
package cn.limbo.hibernate.entity;
import java.sql.Date;
/**
* Created by Limbo on 16/7/19.
*/
public class News {
private int id;
private String title;
private String author;
private Date date;
public News() {
}
public News(String title, String author, Date date) {
this.title = title;
this.author = author;
this.date = date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
News news = (News) o;
if (id != news.id) return false;
if (title != null ? !title.equals(news.title) : news.title != null) return false;
if (author != null ? !author.equals(news.author) : news.author != null) return false;
if (date != null ? !date.equals(news.date) : news.date != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (title != null ? title.hashCode() : 0);
result = 31 * result + (author != null ? author.hashCode() : 0);
result = 31 * result + (date != null ? date.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "News{" +
"id=" + id +
", title='" + title + '\'' +
", author='" + author + '\'' +
", date=" + date +
'}';
}
}
junitTest.java
package cn.limbo.hibernate.entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.sql.Date;
/**
* Created by Limbo on 16/7/19.
*/
public class junitTest {
//现在只是测试,实际生产中是不能把这些当作成员变量的,因为会出现并发的问题
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init()
{
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void destroy()
{
transaction.commit();
session.close();
sessionFactory.close();
}
/**
* flush:是数据库表中的记录和Session缓存中的对象的状态保持一致,则可能会发送对应的SQL语句
* 1.在Traansaction的commit方法中,先调用session的的flush方法,再提交事务
* 2.flush方法肯能会发送SQL语句但是不会提交事务
* 3.注意:在未提交事务或显式调用session.flush方法之前,也可能会进行flush操作
* 1).执行HQL或QBC查询,会先进行flush操作,得到数据表最新的记录
* 2).若记录的ID室友底层数据库使用自增的方式生成的,则在调用save方法时立即发送Insert语句
* 因为在save方法后,必须保证对象的ID是存在的
*/
@Test
public void testSessionFlush2()
{
News news = new News("Java","SUN",new Date(new java.util.Date().getTime()));
session.save(news);
}
/**
* refresh:会强制发送SELECT语句,以使用Session缓存中对象的状态和数据表中对应的记录保持一致!
*
*/
@Test
public void testRefresh()
{
News news = (News) session.get(News.class,1);
System.out.println(news);
session.refresh(news);
System.out.println(news);
}
@Test
public void testSessionFlush()
{
News news = (News) session.get(News.class,1);
news.setAuthor("SUN");
//session.flush();
//HQL查询
News news2 = (News)session.createCriteria(News.class).uniqueResult();
System.out.println(news2);
}
/**
* 直接把缓存清理了
*/
@Test
public void testClear()
{
News news = (News) session.get(News.class,1);
session.clear();
News news2 = (News) session.get(News.class,1);
}
@Test
public void test()
{
News news = (News) session.get(News.class,1);
System.out.println(news);
}
}
主要的显示都在控制台中,不过都挺简单的,下面我附上我在网上找的一些资料
Hibernate Session概述
Session接口是Hibernate向应用程序提供的操纵数据库的最主要的接口,它提供了基本的保存、更新、删除和加载Java对象的方法。
Session具有一个缓存,位于缓存中的对象称为持久化对象,它和数据库中的相关记录对应。Session能够在某些时间点,按照缓存中对象的变化来执行相关的SQL语句,来同步更新数据库,这一过程被称为刷新缓存(flush)。
站在持久化的角度,Hibernate把对象分为4种状态:持久化状态、临时状态、游离状态、删除状态。Session的特定方法能使对象从一个状态转换到另一个状态。
Session缓存
在Session接口的实现中包含一系列的Java集合,这些Java集合构成了Session缓存。只要Session实例没有结束生命周期,且没有清理缓存,则存放在它缓存中的对象也不会结束生命周期。
Session缓存可减少Hibernate应用程序访问数据库的频率。
操作Session缓存:
flush():Session按照缓存中对象的属性来同步更新数据库
默认情况下Session会在以下时间点刷新缓存:
显式调用Session的flush()方法
当应用程序调用Transaction的commit()方法时,该方法先刷新缓存(flush),然后向数据库提交事务
当应用程序执行一下儿查询(HQL,Criteria)操作时,如果缓存中持久化对象的属性已经发生了变化,会先flush()缓存,以保证查询结果能够反映持久化对象的最新状态
flush缓存的例外情况:如果对象使用native生成器生成OID,那么当调用Session的save()方法保存对象时,会立即执行向数据库插入改实体的insert语句。
commit()和flush()方法的区别:flush执行一系列的sql语句,但不提交事务;commit方法先调用flush方法,然后提交事务,意味着提交事务,意味着对数据库操作永久保存下来。
refresh():会强制发送SELECT 语句,以使Session缓存中对象的状态和数据表中对应的记录保持一致!
clear():清理缓存