问题描述
下面是测试代码:package org.abin.lee.test;import java.util.List;import junit.framework.TestCase;import org.abin.lee.entity.Department;import org.abin.lee.service.UserService;import org.junit.Before;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestQueryEhCache extends TestCase {public UserService userService;private ApplicationContext context;@Beforepublic void setUp() {context = new ClassPathXmlApplicationContext(new String[] {"org/abin/lee/spring/applicationContext-resource.xml","org/abin/lee/spring/applicationContext-service.xml","org/abin/lee/spring/applicationContext-dao.xml" });}public void testQueryEhCache() {userService = (UserService) this.context.getBean("userService");List<Department> list=null;System.out.println("下面是执行第一次查询");try {list= this.userService.findAllDepartment();} catch (Exception e) {e.printStackTrace();}for(Department department:list){System.out.println("did="+department.getDid()+",dname="+department.getDname()+",employee="+department.getEmployee());}System.out.println("下面是执行第二次查询");try {list= this.userService.findAllDepartment();} catch (Exception e) {e.printStackTrace();}for(Department department:list){System.out.println("did="+department.getDid()+",dname="+department.getDname()+",employee="+department.getEmployee());}}public UserService getUserService() {return userService;}public void setUserService(UserService userService) {this.userService = userService;}}下面是控制台的打印信息:2012-05-15 12:24:09 [org.springframework.context.support.ClassPathXmlApplicationContext]-[INFO] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@a0dcd9: startup date [Tue May 15 12:24:09 CST 2012]; root of context hierarchy2012-05-15 12:24:09 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[INFO] Loading XML bean definitions from class path resource [org/abin/lee/spring/applicationContext-resource.xml]2012-05-15 12:24:10 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[INFO] Loading XML bean definitions from class path resource [org/abin/lee/spring/applicationContext-service.xml]2012-05-15 12:24:10 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[INFO] Loading XML bean definitions from class path resource [org/abin/lee/spring/applicationContext-dao.xml]2012-05-15 12:24:10 [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[INFO] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1362012: defining beans [dataSource,sessionFactory,hibernateTemplate,transactionManager,transactionInterceptor,org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator#0,userService,userDao]; root of factory hierarchy2012-05-15 12:24:10 [org.hibernate.cfg.Environment]-[INFO] Hibernate 3.2.52012-05-15 12:24:10 [org.hibernate.cfg.Environment]-[INFO] hibernate.properties not found2012-05-15 12:24:10 [org.hibernate.cfg.Environment]-[INFO] Bytecode provider name : cglib2012-05-15 12:24:10 [org.hibernate.cfg.Environment]-[INFO] using JDK 1.4 java.sql.Timestamp handling2012-05-15 12:24:11 [org.hibernate.cfg.HbmBinder]-[INFO] Mapping class: org.abin.lee.entity.Department -> department2012-05-15 12:24:11 [org.hibernate.cfg.HbmBinder]-[INFO] Mapping class: org.abin.lee.entity.Employee -> employee2012-05-15 12:24:11 [org.hibernate.cfg.HbmBinder]-[INFO] Mapping collection: org.abin.lee.entity.Department.employee -> employee2012-05-15 12:24:11 [org.springframework.orm.hibernate3.LocalSessionFactoryBean]-[INFO] Building new Hibernate SessionFactory2012-05-15 12:24:11 [org.hibernate.connection.ConnectionProviderFactory]-[INFO] Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] RDBMS: MySQL, version: 5.5.152012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.0.0-beta ( $Date: 2005-11-17 16:14:47 +0100 (Thu, 17 Nov 2005) $, $Revision$ )2012-05-15 12:24:11 [org.hibernate.dialect.Dialect]-[INFO] Using dialect: org.hibernate.dialect.MySQLDialect2012-05-15 12:24:11 [org.hibernate.transaction.TransactionFactoryFactory]-[INFO] Transaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory2012-05-15 12:24:11 [org.hibernate.transaction.TransactionManagerLookupFactory]-[INFO] No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Automatic flush during beforeCompletion(): disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Automatic session close at end of transaction: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] JDBC batch size: 152012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] JDBC batch updates for versioned data: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Scrollable result sets: enabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] JDBC3 getGeneratedKeys(): enabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Connection release mode: after_transaction2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Maximum outer join fetch depth: 22012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Default batch fetch size: 12012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Generate SQL with comments: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Order SQL updates by primary key: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Order SQL inserts for batching: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory2012-05-15 12:24:11 [org.hibernate.hql.ast.ASTQueryTranslatorFactory]-[INFO] Using ASTQueryTranslatorFactory2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Query language substitutions: {}2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] JPA-QL strict compliance: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Second-level cache: enabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Query cache: enabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Cache provider: org.hibernate.cache.EhCacheProvider2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Optimize cache for minimal puts: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Structured second-level cache entries: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Query cache factory: org.hibernate.cache.StandardQueryCacheFactory2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Echoing all SQL to stdout2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Statistics: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Deleted entity synthetic identifier rollback: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Default entity-mode: pojo2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Named query checking : enabled2012-05-15 12:24:11 [org.hibernate.impl.SessionFactoryImpl]-[INFO] building session factory2012-05-15 12:24:12 [org.hibernate.cache.CacheFactory]-[DEBUG] instantiating cache region: org.abin.lee.entity.Employee usage strategy: read-only2012-05-15 12:24:12 [org.hibernate.cache.CacheFactory]-[WARN] read-only cache configured for mutable class: org.abin.lee.entity.Employee2012-05-15 12:24:12 [org.hibernate.cache.CacheFactory]-[DEBUG] instantiating cache region: org.abin.lee.entity.Department usage strategy: read-only2012-05-15 12:24:12 [org.hibernate.cache.CacheFactory]-[WARN] read-only cache configured for mutable class: org.abin.lee.entity.Department2012-05-15 12:24:12 [org.hibernate.impl.SessionFactoryObjectFactory]-[INFO] Not binding factory to JNDI, no JNDI name configured2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.SchemaUpdate]-[INFO] Running hbm2ddl schema update2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.SchemaUpdate]-[INFO] fetching database metadata2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.SchemaUpdate]-[INFO] updating schema2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] table found: abin.department2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] columns: [dname, did]2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] foreign keys: []2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] indexes: [primary]2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] table found: abin.employee2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] columns: [ename, did, eid]2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] foreign keys: [fdid, fk4722e6aefea415dc]2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] indexes: [primary, fk4722e6aefea415dc]2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.SchemaUpdate]-[INFO] schema update complete2012-05-15 12:24:12 [org.hibernate.cache.UpdateTimestampsCache]-[INFO] starting update timestamps cache at region: org.hibernate.cache.UpdateTimestampsCache2012-05-15 12:24:12 [org.hibernate.cache.StandardQueryCache]-[INFO] starting query cache at region: org.hibernate.cache.StandardQueryCache2012-05-15 12:24:12 [org.springframework.orm.hibernate3.HibernateTransactionManager]-[INFO] Using DataSource [org.apache.commons.dbcp.BasicDataSource@1fa39bb] of Hibernate SessionFactory for HibernateTransactionManager下面是执行第一次查询2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] checking cached query results in region: org.hibernate.cache.StandardQueryCache2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] key: sql: select department0_.did as did0_, department0_.dname as dname0_ from abin.department department0_ order by department0_.did desc; parameters: ; named parameters: {}2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] Element for sql: select department0_.did as did0_, department0_.dname as dname0_ from abin.department department0_ order by department0_.did desc; parameters: ; named parameters: {} is null2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] query results were not found in cacheHibernate: select department0_.did as did0_, department0_.dname as dname0_ from abin.department department0_ order by department0_.did desc2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Department#fde9fe25-ea81-4168-b9bc-4228581283582012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Department#15d95f71-f41b-4369-a1ae-441366f57318Hibernate: select employee0_.did as did1_, employee0_.eid as eid1_, employee0_.eid as eid1_0_, employee0_.ename as ename1_0_, employee0_.did as did1_0_ from abin.employee employee0_ where employee0_.did=?2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#788d9a5a-9085-4df9-a058-8027469a1bac2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#924fba4c-3a9c-4f33-9e09-63e49663f9a1Hibernate: select employee0_.did as did1_, employee0_.eid as eid1_, employee0_.eid as eid1_0_, employee0_.ename as ename1_0_, employee0_.did as did1_0_ from abin.employee employee0_ where employee0_.did=?2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#291e9165-dcc2-4648-bb05-47056ba8f3542012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#e789a510-2f80-4b4b-b25e-e7e48e4869da2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] caching query results in region: org.hibernate.cache.StandardQueryCache; timestamp=5476580773695488did=fde9fe25-ea81-4168-b9bc-422858128358,dname=department2,employee=[org.abin.lee.entity.Employee@5c98f3, org.abin.lee.entity.Employee@bef361]did=15d95f71-f41b-4369-a1ae-441366f57318,dname=department1,employee=[org.abin.lee.entity.Employee@e0e515, org.abin.lee.entity.Employee@10dc656]下面是执行第二次查询2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] checking cached query results in region: org.hibernate.cache.StandardQueryCache2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] key: sql: select department0_.did as did0_, department0_.dname as dname0_ from abin.department department0_ order by department0_.did desc; parameters: ; named parameters: {}2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] Checking query spaces for up-to-dateness: [abin.department]2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] key: abin.department2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] Element for abin.department is null2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] returning cached query results2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] key: org.abin.lee.entity.Department#fde9fe25-ea81-4168-b9bc-4228581283582012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Cache hit: org.abin.lee.entity.Department#fde9fe25-ea81-4168-b9bc-422858128358Hibernate: select employee0_.did as did1_, employee0_.eid as eid1_, employee0_.eid as eid1_0_, employee0_.ename as ename1_0_, employee0_.did as did1_0_ from abin.employee employee0_ where employee0_.did=?2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#291e9165-dcc2-4648-bb05-47056ba8f3542012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#e789a510-2f80-4b4b-b25e-e7e48e4869da2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] key: org.abin.lee.entity.Department#15d95f71-f41b-4369-a1ae-441366f573182012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Cache hit: org.abin.lee.entity.Department#15d95f71-f41b-4369-a1ae-441366f57318Hibernate: select employee0_.did as did1_, employee0_.eid as eid1_, employee0_.eid as eid1_0_, employee0_.ename as ename1_0_, employee0_.did as did1_0_ from abin.employee employee0_ where employee0_.did=?2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#788d9a5a-9085-4df9-a058-8027469a1bac2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#924fba4c-3a9c-4f33-9e09-63e49663f9a1did=fde9fe25-ea81-4168-b9bc-422858128358,dname=department2,employee=[org.abin.lee.entity.Employee@ff9053, org.abin.lee.entity.Employee@5c7734]did=15d95f71-f41b-4369-a1ae-441366f57318,dname=department1,employee=[org.abin.lee.entity.Employee@1000bcf, org.abin.lee.entity.Employee@754fc]我想知道一下,第二次查询是不是使用了hibernate的ehcache查询了?这里的Department和Employee是一对多的关系。 问题补充:jobar 写道
解决方案
引用是不是这个 key: 和Cache hit:??? 是的,cache hit就是缓存命中的意思
解决方案二:
从日志来看,确实是使用了ecache缓存,并且第二次查询从缓存命中了查询结果直接返回2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] key: org.abin.lee.entity.Department#15d95f71-f41b-4369-a1ae-441366f573182012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Cache hit: org.abin.lee.entity.Department#15d95f71-f41b-4369-a1ae-441366f57318Hibernate: select employee0_.did as did1_, employee0_.eid as eid1_, employee0_.eid as eid1_0_, employee0_.ename as ename1_0_, employee0_.did as did1_0_ from abin.employee employee0_ where employee0_.did=?