Hibernate_二级缓存2_二级缓存详解
org.hibernate.cache.HashtableCacheProvider true
org.hibernate.dialect.MySQLDialect jdbc:mysql://localhost:3306/hibernate_20170423 com.mysql.jdbc.Driver root root true false update 2 org.hibernate.connection.C3P0ConnectionProvider 5 20 120 3000 org.hibernate.cache.HashtableCacheProvider true
package cn.itcast.l_second_cache;import java.util.Iterator;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;/** * 应用程序操作类 * * @author 风清杨 * @version V3.0 * */public class App { private static SessionFactory sessionFactory = new Configuration()// .configure()// .addClass(Department.class)// .addClass(Employee.class)// .buildSessionFactory(); // 测试一级缓存(一级缓存[查询出二条查询语句]) @Test public void testSessionCache() throws Exception { // ====================================第1个Session Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); // ------------------------------------ // 获取一方数据,并显示另一方信息 Employee employee = (Employee) session.get(Employee.class, 1); // System.out.println(employee); // ------------------------------------ tx.commit(); } catch (RuntimeException e) { tx.rollback(); throw e; } finally { session.close(); } // ====================================第二2个Session Session session2 = sessionFactory.openSession(); Transaction tx2 = null; try { tx2 = session2.beginTransaction(); // ------------------------------------ // 获取一方数据,并显示另一方信息 Employee employee2 = (Employee) session2.get(Employee.class, 1); // System.out.println(employee2); // ------------------------------------ tx2.commit(); } catch (RuntimeException e) { tx2.rollback(); throw e; } finally { session2.close(); } } // 测试二级缓存 @Test public void testSecodeCache() throws Exception { // ====================================第1个Session Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); // ------------------------------------ // 获取一方数据,并显示另一方信息 Department department = (Department) session.get(Department.class, 1); System.out.println(department.getName()); System.out.println(department.getEmployee()); // ------------------------------------ tx.commit(); } catch (RuntimeException e) { tx.rollback(); throw e; } finally { session.close(); } // ====================================第二2个Session Session session2 = sessionFactory.openSession(); Transaction tx2 = null; try { tx2 = session2.beginTransaction(); // ------------------------------------ // 获取一方数据,并显示另一方信息 Department department2 = (Department) session2.get(Department.class, 1); // department2.setName("研发部"); System.out.println(department2.getName()); System.out.println(department2.getEmployee()); // ------------------------------------ tx2.commit(); } catch (RuntimeException e) { tx2.rollback(); throw e; } finally { session2.close(); } } // 测试查询缓存 // 当使用Query.list()时,默认不会使用二级缓存 @Test public void testQueryCache() throws Exception { // ====================================第1个Session Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); // ------------------------------------ List list = session.createQuery("from Employee e where e.id<10").list(); System.out.println(list); Employee employe5 = (Employee) session.get(Employee.class, 5); System.out.println(employe5); // ------------------------------------ tx.commit(); } catch (RuntimeException e) { tx.rollback(); throw e; } finally { session.close(); } // ====================================第二2个Session Session session2 = sessionFactory.openSession(); Transaction tx2 = null; try { tx2 = session2.beginTransaction(); // ------------------------------------ List list2 = session2.createQuery("from Employee e where e.id<10").list(); System.out.println(list2); // ------------------------------------ tx2.commit(); } catch (RuntimeException e) { tx2.rollback(); throw e; } finally { session2.close(); } } // 测试查询缓存 // 在使用HQL方式查询时,如果用iterate()方法,就会使用缓存 // 因为这个方法是先查询所有符合条件的id集合,再一个一个的按id查找数据,就能用上缓存了。 // 但是这个方法会有N+1次查询的问题,提升性能有限,不太常用 @Test public void testQueryCache2() throws Exception { // ====================================第1个Session Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); // ------------------------------------ Iterator iterator = session.createQuery("from Employee e where e.id<10").iterate(); while (iterator.hasNext()) { Employee e = iterator.next(); System.out.println(e); } Employee employe5 = (Employee) session.get(Employee.class, 5); System.out.println(employe5); // ------------------------------------ tx.commit(); } catch (RuntimeException e) { tx.rollback(); throw e; } finally { session.close(); } // ====================================第二2个Session Session session2 = sessionFactory.openSession(); Transaction tx2 = null; try { tx2 = session2.beginTransaction(); // ------------------------------------ Iterator iterator2 = session2.createQuery("from Employee e where e.id<10").iterate(); while (iterator2.hasNext()) { Employee e = iterator2.next(); System.out.println(e); } // ------------------------------------ tx2.commit(); } catch (RuntimeException e) { tx2.rollback(); throw e; } finally { session2.close(); } } // 测试查询缓存 @Test public void testQueryCache3() throws Exception { // ====================================第1个Session Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); // ------------------------------------ List list = session.createQuery(// "from Employee e where e.id<8")// .setCacheable(true)// 是否使用查询缓存,需要在hibernate.cfg.xml中开启查询缓才行 .list(); System.out.println(list); // ------------------------------------ tx.commit(); } catch (RuntimeException e) { tx.rollback(); throw e; } finally { session.close(); } // ====================================第二2个Session Session session2 = sessionFactory.openSession(); Transaction tx2 = null; try { tx2 = session2.beginTransaction(); // ------------------------------------ System.out.println("\n------------------\n"); List list2 = session2.createQuery(// "from Employee e where e.id<8")// .setCacheable(true)// .list(); System.out.println(list2); // ------------------------------------ tx2.commit(); } catch (RuntimeException e) { tx2.rollback(); throw e; } finally { session2.close(); } } // 测试Update与Delete格式的HQL语句对二级缓存的影响 // 会让二级缓存中相关的数据失效,下次使用这些数据时会重新到数据库中加载 @Test public void testUpdateTimestampCache() throws Exception { // ====================================第1个Session Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); // ------------------------------------ Employee employee = (Employee) session.get(Employee.class, 1); System.out.println(employee.getName()); int update = session.createQuery("update Employee e set e.name=:Name where e.id=:Id")// .setParameter("Name", "张三1")// .setParameter("Id", 1)// .executeUpdate();// 执行更新 // 再显示这个员工的名称 // session.refresh(employee); System.out.println(employee.getName()); // ------------------------------------ tx.commit(); } catch (RuntimeException e) { tx.rollback(); throw e; } finally { session.close(); } // ====================================第二2个Session Session session2 = sessionFactory.openSession(); Transaction tx2 = null; try { tx2 = session2.beginTransaction(); // ------------------------------------ System.out.println("\n------------------\n"); Employee employee2 = (Employee) session2.get(Employee.class, 1); System.out.println(employee2.getName()); // ------------------------------------ tx2.commit(); } catch (RuntimeException e) { tx2.rollback(); throw e; } finally { session2.close(); } }}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~