浏览 1854 次
锁定老帖子 主题:hibernate 缓存 初试
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-11-02
最后修改:2010-01-20
配置二级缓存 hibernate.cfg.xml <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:jtds:sqlserver://localHost:1433/DSO</property> <property name="hibernate.connection.username">sa</property> <property name="hibernate.connection.password">sa</property> <property name="show_sql">false</property> <mapping resource="ComPermission.hbm.xml" /> </session-factory> </hibernate-configuration> ehcache.xml <ehcache> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="true" /> </ehcache>
ComPermission.hbm.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="ComPermission" table="JB_COM_PERMISSION" dynamic-insert="true" dynamic-update="true"> <cache usage="read-write" include="all" /> <id name="permissionId" type="java.lang.String"> <column name="PERMISSION_ID" length="5" /> <generator class="assigned"></generator> </id> <property name="permissionCode" type="java.lang.String"> <column name="PERMISSION_CODE" length="5" not-null="true" /> </property> <property name="permissionName" type="java.lang.String"> <column name="PERMISSION_NAME" length="30" not-null="true" /> </property> <property name="remark" type="java.lang.String"> <column name="REMARK" length="255" /> </property> </class> </hibernate-mapping> Java实体类 ComPermission public class ComPermission implements java.io.Serializable { // Fields private String permissionId; private String permissionCode; private String permissionName; private String remark; } log4j设置 log4j.category.org.hibernate.cache=DEBUG,CA log4j.category.org.hibernate.SQL=DEBUG,CA
测试代码 public static void main(String[] args) { System.out.println(HibernateSessionFactory.getConfiguration()); System.out.println("|||||||||||||||||||||||||||||||||||||||||||"); Session session = HibernateSessionFactory.getSessionFactory().openSession(); long start = System.currentTimeMillis(); try { ComPermission permission = (ComPermission) session.load(ComPermission.class, "10001"); System.out.println(permission.getPermissionName()); } catch (RuntimeException re) { throw re; } finally { session.close(); } System.out.println("time :" + (System.currentTimeMillis() - start)); System.out.println("-----------------------------------------------"); Transaction tran = null; Session session2 = HibernateSessionFactory.getSessionFactory().openSession(); start = System.currentTimeMillis(); try { tran = session2.beginTransaction(); ComPermission permission = (ComPermission) session2.load(ComPermission.class, "10001"); permission.setPermissionName(permission.getPermissionName() + "!"); System.out.println(permission.getPermissionName()); session2.update(permission); tran.commit(); } catch (RuntimeException re) { tran.rollback(); throw re; } finally { session2.close(); } System.out.println("time :" + (System.currentTimeMillis() - start)); System.out.println("---------------------------------------------------------"); Session session3 = HibernateSessionFactory.getSessionFactory().openSession(); start = System.currentTimeMillis(); try { ComPermission permission = (ComPermission) session3.load(ComPermission.class, "10001"); System.out.println(permission.getPermissionName()); } catch (RuntimeException re) { throw re; } finally { session3.close(); } System.out.println("time :" + (System.currentTimeMillis() - start)); System.out.println("---------------------------------------------------------"); Session session4 = HibernateSessionFactory.getSessionFactory().openSession(); start = System.currentTimeMillis(); try { ComPermission permission = (ComPermission) session4.load(ComPermission.class, "10001"); System.out.println(permission.getPermissionName()); } catch (RuntimeException re) { throw re; } finally { session4.close(); } System.out.println("time :" + (System.currentTimeMillis() - start)); }
log信息 DEBUG [org.hibernate.cache.CacheFactory.createCache:39] instantiating cache region: ComPermission usage strategy: read-write WARN [org.hibernate.cache.EhCacheProvider.buildCache:103] Could not find configuration [ComPermission]; using defaults. DEBUG [org.hibernate.cache.EhCacheProvider.buildCache:106] started EHCache region: ComPermission org.hibernate.cfg.Configuration@c4fe76 ||||||||||||||||||||||||||||||||||||||||||| DEBUG [org.hibernate.cache.ReadWriteCache.get:75] Cache lookup: ComPermission#10001 //尝试在缓存中查找对象 ComPermission#10001 DEBUG [org.hibernate.cache.EhCache.get:104] key: ComPermission#10001 DEBUG [org.hibernate.cache.EhCache.get:113] Element for ComPermission#10001 is null DEBUG [org.hibernate.cache.ReadWriteCache.get:91] Cache miss: ComPermission#10001 //找不到 DEBUG [org.hibernate.jdbc.AbstractBatcher.log:346] select compermiss0_.PERMISSION_ID as PERMISSION1_0_0_, compermiss0_.PERMISSION_CODE as PERMISSION2_0_0_, compermiss0_.PERMISSION_NAME as PERMISSION3_0_0_, compermiss0_.REMARK as REMARK0_0_ from JB_COM_PERMISSION compermiss0_ where compermiss0_.PERMISSION_ID=? DEBUG [org.hibernate.cache.ReadWriteCache.put:148] Caching: ComPermission#10001 DEBUG [org.hibernate.cache.EhCache.get:104] key: ComPermission#10001 DEBUG [org.hibernate.cache.EhCache.get:113] Element for ComPermission#10001 is null DEBUG [org.hibernate.cache.ReadWriteCache.put:160] Cached: ComPermission#10001 //将查询到的缓存起来 测试信息 time :47 ----------------------------------------------- DEBUG [org.hibernate.cache.ReadWriteCache.get:75] Cache lookup: ComPermission#10001 DEBUG [org.hibernate.cache.EhCache.get:104] key: ComPermission#10001 DEBUG [org.hibernate.cache.ReadWriteCache.get:85] Cache hit: ComPermission#10001 //找到了 测试信息! DEBUG [org.hibernate.cache.ReadWriteCache.lock:113] Invalidating: ComPermission#10001 DEBUG [org.hibernate.cache.EhCache.get:104] key: ComPermission#10001 DEBUG [org.hibernate.jdbc.AbstractBatcher.log:346] update JB_COM_PERMISSION set PERMISSION_NAME=? where PERMISSION_ID=? DEBUG [org.hibernate.cache.ReadWriteCache.release:195] Releasing: ComPermission#10001 //由于对象被更新 将缓存中的对象释放 DEBUG [org.hibernate.cache.EhCache.get:104] key: ComPermission#10001 time :31 --------------------------------------------------------- DEBUG [org.hibernate.cache.ReadWriteCache.get:75] Cache lookup: ComPermission#10001 DEBUG [org.hibernate.cache.EhCache.get:104] key: ComPermission#10001 DEBUG [org.hibernate.cache.ReadWriteCache.get:94] Cached item was locked: ComPermission#10001 //lockable.isGettable(txTimestamp)==false DEBUG [org.hibernate.jdbc.AbstractBatcher.log:346] select compermiss0_.PERMISSION_ID as PERMISSION1_0_0_, compermiss0_.PERMISSION_CODE as PERMISSION2_0_0_, compermiss0_.PERMISSION_NAME as PERMISSION3_0_0_, compermiss0_.REMARK as REMARK0_0_ from JB_COM_PERMISSION compermiss0_ where compermiss0_.PERMISSION_ID=? DEBUG [org.hibernate.cache.ReadWriteCache.put:148] Caching: ComPermission#10001 DEBUG [org.hibernate.cache.EhCache.get:104] key: ComPermission#10001 DEBUG [org.hibernate.cache.ReadWriteCache.put:160] Cached: ComPermission#10001 //重新缓存对象 测试信息! time :0 --------------------------------------------------------- DEBUG [org.hibernate.cache.ReadWriteCache.get:75] Cache lookup: ComPermission#10001 DEBUG [org.hibernate.cache.EhCache.get:104] key: ComPermission#10001 DEBUG [org.hibernate.cache.ReadWriteCache.get:85] Cache hit: ComPermission#10001 //缓存命中 测试信息! time :0
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |