标签:隔离 toc buffer 闲置 action 框架 reg 应用 code
<dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.8.1</version> </dependency>
<!--配置二级缓存--> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory</property>
<?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"> <![CDATA[ jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC ]]> </property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property> <!--是否显示SQL--> <property name="show_sql">true</property> <!--是否格式化SQL--> <property name="format_sql">true</property> <!--是否自动提交事务--> <property name="connection.autocommit">true</property> <property name="hbm2ddl.auto">update</property> <property name="hibernate.id.new_generator_mappings">false</property> <!--配置c3p0--> <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider </property> <property name="hibernate.c3p0.max_size">2</property> <property name="hibernate.c3p0.min_size">2</property> <property name="hibernate.c3p0.timeout">5000</property> <property name="hibernate.c3p0.max_statements">100</property> <property name="hibernate.c3p0.acquire_increment">2</property> <!--配置二级缓存--> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory</property> <!--设置隔离级别--> <property name="hibernate.connection.isolation">4</property> <!--配置JavaBean和表的映射关系--> <mapping class="com.sunxiaping.domain.Customer"/> <mapping class="com.sunxiaping.domain.Order"/> </session-factory> </hibernate-configuration>
package com.sunxiaping.domain; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import javax.persistence.*; import java.io.Serializable; import java.util.HashSet; import java.util.Set; /** * 客户 */ @Setter @Getter @NoArgsConstructor @Entity @Table(name = "`customer`") @Cacheable @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) public class Customer implements Serializable { /** * 主键 */ @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; /** * 客户的名称 */ private String name; /** * 一个客户有许多订单 * 一方放弃外键的维护权 */ @OneToMany(mappedBy = "customer", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY) @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) private Set<Order> orders = new HashSet<>(); }
package com.sunxiaping.domain; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import org.hibernate.annotations.CacheConcurrencyStrategy; import javax.persistence.*; import java.io.Serializable; import java.math.BigDecimal; /** * 订单 */ @Setter @Getter @NoArgsConstructor @Entity @Table(name = "`order`") @Cacheable @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY) public class Order implements Serializable { /** * 主键 */ @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; /** * 订单名称 */ private String name; /** * 价格 */ private BigDecimal price; /** * 一个订单属于一个客户,多方维护外键 */ @ManyToOne(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER) @JoinColumn(name = "cid") @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE) private Customer customer; }
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <!-- 设置硬盘目录 --> <diskStore path="D:\\temp"/> <!-- maxElementsInMemory 缓存最大个数 --> <!-- eternal 是否永久有效,如果该值设置为true,则timeout将失效 --> <!-- timeToIdleSeconds 失效前的闲置时间 --> <!-- timeToLiveSeconds 失效前的存活时间 --> <!-- diskSpoolBufferSizeMB 缓存区的大小 默认30M --> <!-- maxElementsOnDisk 硬盘最大缓存个数 --> <!-- overflowToDisk 超出缓存最大个数时写入硬盘 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> </ehcache>
package com.sunxiaping.test; import com.sunxiaping.domain.Customer; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; public class HibernateTest { @Test public void test() { Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); //执行查询后,放在一级缓存和二级缓存中 Customer c1 = session.get(Customer.class, 1); System.out.println(c1); session.clear(); //如果二级缓存中有数据,将从二级缓存中取数据 Customer c2 = session.get(Customer.class, 1); System.out.println(c2); transaction.commit(); session.close(); sessionFactory.close(); } }
package com.sunxiaping.test; import com.sunxiaping.domain.Customer; import com.sunxiaping.domain.Order; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; import java.util.Set; public class HibernateTest { @Test public void test() { Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); //执行查询后,放在一级缓存和二级缓存中 Customer c1 = session.get(Customer.class, 1); System.out.println(c1); Set<Order> orders = c1.getOrders(); System.out.println(orders); session.clear(); //如果二级缓存中有数据,将从二级缓存中取数据 Customer c2 = session.get(Customer.class, 1); System.out.println(c2); //默认二级缓存里面缓存的是Order的id,然后根据ID从类级别缓存中查找 Set<Order> orders1 = c2.getOrders(); System.out.println(orders1); transaction.commit(); session.close(); sessionFactory.close(); } }
package com.sunxiaping.test; import com.sunxiaping.domain.Customer; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.query.Query; import org.junit.Test; import java.util.List; public class HibernateTest { @Test public void test() { Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Query query = session.createQuery(" FROM Customer "); query.setCacheable(true);//设置允许缓存 List<Customer> list = query.list(); for (Customer customer : list) { System.out.println(customer); } Customer customer1 = session.get(Customer.class, 1); System.out.println(customer1); transaction.commit(); session.close(); sessionFactory.close(); } }
package com.sunxiaping.test; import com.sunxiaping.domain.Customer; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.query.Query; import org.junit.Test; public class HibernateTest { @Test public void test() { Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session1 = sessionFactory.openSession(); Transaction transaction = session1.beginTransaction(); /** * 先修改用户信息 */ Query query = session1.createQuery(" update Customer set name =:name where id =:id"); query.setParameter("name", "李四"); query.setParameter("id", 1); query.executeUpdate(); transaction.commit(); session1.close(); Session session2 = sessionFactory.openSession(); Transaction transaction2 = session2.beginTransaction(); Customer customer = session2.get(Customer.class, 1); System.out.println(customer); transaction2.commit(); session2.close(); sessionFactory.close(); } }
标签:隔离 toc buffer 闲置 action 框架 reg 应用 code
原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/11562579.html