理论部分参见:http://tpglzu2015.iteye.com/blog/2206134
测试结果,与以上理论基本一致。
测试相关代码
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">org.postgresql.Driver</property> <property name="connection.url">jdbc:postgresql://127.0.0.1:5432/XXXXXX</property> <property name="connection.username">XXXXXX</property> <property name="connection.password">XXXXXX</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">30</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.PostgresPlusDialect</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hibernate.jdbc.batch_size">100</property> <property name="hibernate.jdbc.fetch_size">50</property> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.use_query_cache">true</property> <property name="net.sf.ehcache.configurationResourceName">ehcache.xml</property> <!-- Enable Hibernate's automatic session context management --> <!--<property name="current_session_context_class">thread</property>--> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">create</property> --> <!-- Disable the second-level cache --> <!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> --> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <mapping resource="hibernateDemo/hbms/testtable.hbm.xml"/> </session-factory> </hibernate-configuration>
testtable.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="hibernateDemo.beans"> <class name="TestTable"> <cache usage="read-write"/> <id name="id"></id> <property name="name"></property> </class> </hibernate-mapping>
注意事项,如果cache节点未配置的话,是不会对该对象进行二级缓存的。
所以,需要配置缓存策略的对象需添加cache节点,不需要配置缓存策略的对象需删除cache节点
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache> <diskStore path="java.io.tmpdir" /> <cache name="hibernateDemo.beans.TestTable" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <defaultCache maxElementsInMemory="0" eternal="false" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /> </ehcache>
TestMain.java
package hibernateDemo; import hibernateDemo.beans.TestTable; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class TestMain { private Configuration cfg; private SessionFactory sf; Session session; public void setup() { cfg = new Configuration(); sf = cfg.configure().buildSessionFactory(); session = sf.openSession(); } public void close() { session.close(); sf.close(); } public void insert(Long id, String name) { TestTable s = new TestTable(); s.setId(id); s.setName(name); session.beginTransaction(); session.save(s); session.getTransaction().commit(); } public void delete(Long id) { TestTable s = new TestTable(); s.setId(id); session.beginTransaction(); session.delete(s); session.getTransaction().commit(); } public void update(Long id, String name) { TestTable s = new TestTable(); s.setId(id); s.setName(name); session.beginTransaction(); session.update(s); session.getTransaction().commit(); } public void search(Long id) { session.beginTransaction(); String queryStr = "from TestTable "; if (id != null) { queryStr += " where id = " + id; } Query query = session.createQuery(queryStr); List<TestTable> testTables = query.list(); for (TestTable testTable : testTables) { System.out.println(testTable.getId() + "\t" + testTable.getName()); } session.getTransaction().commit(); } public void getTest_SessionCache() { System.err.println("getTest_SessionCache ... do"); setup(); // get method test TestTable testTable = (TestTable) session.get(TestTable.class, 2L); System.out.println("id = " + testTable.getId()); System.out.println("name = " + testTable.getName()); System.out.println("--------------------------"); testTable = (TestTable) session.get(TestTable.class, 2L); System.out.println("id = " + testTable.getId()); System.out.println("name = " + testTable.getName()); close(); System.err.println("getTest_SessionCache ... end"); } public void getTest_FactoryCache() throws InterruptedException { setup(); System.err.println("getTest_FactoryCache ... do"); // get method test TestTable testTable = (TestTable) session.get(TestTable.class, 2L); System.out.println("id = " + testTable.getId()); System.out.println("name = " + testTable.getName()); session.close(); System.out.println("--------------------------"); Thread.sleep(10000); session = sf.openSession(); testTable = (TestTable) session.get(TestTable.class, 2L); System.out.println("id = " + testTable.getId()); System.out.println("name = " + testTable.getName()); System.err.println("getTest_FactoryCache ... end"); close(); } public void loadTest_SessionCache() { setup(); System.err.println("loadTest_SessionCache ... do"); // get method test TestTable testTable = (TestTable) session.load(TestTable.class, 2L); System.out.println("id = " + testTable.getId()); System.out.println("name = " + testTable.getName()); System.out.println("--------------------------"); testTable = (TestTable) session.load(TestTable.class, 2L); System.out.println("id = " + testTable.getId()); System.out.println("name = " + testTable.getName()); System.err.println("loadTest_SessionCache ... end"); close(); } public static void main(String[] args) throws InterruptedException { TestMain testMain = new TestMain(); // testMain.insert(3L, "s3"); // testMain.search(null); // testMain.update(id, name); // testMain.delete(1L); // testMain.getTest_SessionCache(); // testMain.loadTest_SessionCache(); testMain.getTest_FactoryCache(); } }
Log4J.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="rootAppender" class="org.apache.log4j.varia.NullAppender"/> <appender name="TEST" class="org.apache.log4j.RollingFileAppender"> <param name="MaxFileSize" value="5MB" /> <param name="MaxBackupIndex" value="65535" /> <param name="file" value="D:/log/test.log" /> <param name="append" value="true" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy/MM/dd HH:mm:ss.SSS} %x LOCATION=%C{1}.%M(Line:%L) %m%n" /> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="DEBUG" /> </filter> </appender> <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy/MM/dd HH:mm:ss.SSS} %p %c %x %m%n" /> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="DEBUG" /> </filter> </appender> <logger name="net.sf.ehcache" class="org.apache.log4j.Logger" additivity="false"> <level value="DEBUG" /> <appender-ref ref="TEST" /> <appender-ref ref="CONSOLE"/> </logger> <!-- <logger name="org.hibernate.SQL" class="org.apache.log4j.Logger" additivity="false"> <level value="DEBUG" /> <appender-ref ref="TEST" /> <appender-ref ref="CONSOLE"/> </logger> --> <logger name="org.hibernate.cache" class="org.apache.log4j.Logger" additivity="false"> <level value="DEBUG" /> <appender-ref ref="TEST" /> <appender-ref ref="CONSOLE"/> </logger> <root> <level value="WARN"/> <appender-ref ref="APP"/> <appender-ref ref="CONSOLE"/> </root> </log4j:configuration>
相关推荐
Hibernate+ehcache二级缓存技术 Hibernate+ehcache二级缓存技术
7.Spring+Junit4单元测试,优点:不会破坏数据库现场,等等。 2)Demo 导入说明: 1.Eclipse Encoding:GBK 2.Eclipse 导入后可能需要在 Xml Catalog 手动添加:ehcache-spring-1.2.xsd(ehcache-spring-annotations...
struts2+hibernate+ehcache二级缓存的 步骤和需要的代码及jar包
spring+springmvc+hibernate+ehcache JavaWeb后台框架,不仅提高了开发程序的速度,且其中还是用到hibernate和ehcache缓存的使用,加快了程序运行的数据,该框架亲测好用。值得注意的是该种框架现在还算是用的比较多...
spring+springMVC+hibernate+cxf+quartz(定时器)+ehcache(缓存)
NULL 博文链接:https://llying.iteye.com/blog/492307
3.Ehcache方法缓存及页面缓存。见applicationContext-cache.xml及web.xml的pageEhCacheFilter 4.MyBatis+Maven代码生成工具。见bin目录 5.作为项目或者技术研究的基础架构。必要的jar包依赖都已配置好,基本都采用较...
3.Ehcache方法缓存及页面缓存。见applicationContext-cache.xml及web.xml的pageEhCacheFilter 4.MyBatis+Maven代码生成工具。见bin目录 5.作为项目或者技术研究的基础架构。必要的jar包依赖都已配置好,基本都采用较...
hibernate使用的一些例子,二级缓存ehcache的使用 一些设计模式的demo代码
此配置和包,是springMVC4.3.3 +spring4+hibernate5.1.3+二级缓存ehcache(不用可以关闭)+fastjson。 是正常运行的项目里拷出来的,方便大家使用。
Hibernate配置EhCache缓存之annotation注解[归类].pdf
在Spring+Hibernate集成环境中使用EhCache缓存做的开发测试,在线程中打开数据库查询数据,及关闭数据库后查询数据的结果.
NULL 博文链接:https://dreamoftch.iteye.com/blog/1985366
hibernate-ehcache-4.1.0.Final.jar 是hibernate4.1使用缓存的jar包
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。 Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,...
EHCache缓存的说明文档是到处找来的内容,都有参考链接指向原地址。有三个测试项目也在里面:一个整合了Struts2或Hibernate,一个整合了MyBatis3,这两个是我做的;另一个ehcachetest是下载了別人的。
SSH三层架构MVC(struts1.3+spring2.x+hibernate3.2),Hibernate(ehcache)二级缓存技术,Spring 注解形式依赖注入,ehcache缓存 源代码,内有MySql anbyke.sql文件,方便创建数据库演示效果!
里面有目前的流行的前台springmvc框架,持久化层采用hibernate,安全方面采用shiro框架,页面风格采用bootstrap,同时里面还集成了javamelody监控,ehcache缓存机制,druid连接池管理,以及spring事务管理,spring...
代码是Ehcache的页面缓存、spring的缓存和Hibernate的缓存的应用,采用的是springMVC框架,数据库是MySQL