
software saboteur
   
Профиль
Группа: Участник
Сообщений: 4367
Регистрация: 7.10.2005
Репутация: 28 Всего: 159
|
продолжая попытки разобраться с кэшем запросов, я начал экспериментировать с транзакциями, что дало несколько неожиданных результатов. выключаем транзакции: | Код | @ContextConfiguration(locations = "classpath*:/META-INF/spring/*.xml") //@TestExecutionListeners(TransactionalTestExecutionListener.class) //@Transactional(propagation = Propagation.REQUIRED) public class SimpleEntityDaoTest extends AbstractTestNGSpringContextTests { @Resource(name = "simpleEntityDao") private SimpleEntityDao simpleEntityDao;
@Test public void queryCache() { final SimpleEntity entity = simpleEntityDao.create(new SimpleEntity());
simpleEntityDao.findById(entity.getId()); simpleEntityDao.findById(entity.getId());
System.out.println(simpleEntityDao.getStatistics()); } }
|
результат: | Цитата | Running TestSuite 22:18:10,700 INFO main RegionFactoryCacheProviderBridge:<init>:61 - Cache provider: org.hibernate.cache.EhCacheProvider 22:18:10,771 WARN main EhCacheProvider:buildCache:93 - Could not find configuration [com.mycompany.dbtest.entity.SimpleEntity]; using defaults. 22:18:10,784 DEBUG main EhCacheProvider:buildCache:96 - started EHCache region: com.mycompany.dbtest.entity.SimpleEntity 22:18:10,803 INFO main SimpleEntity:<init>:36 - create new simple entity 22:18:11,096 INFO main UpdateTimestampsCache:<init>:57 - starting update timestamps cache at region: org.hibernate.cache.UpdateTimestampsCache 22:18:11,097 WARN main EhCacheProvider:buildCache:93 - Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults. 22:18:11,098 DEBUG main EhCacheProvider:buildCache:96 - started EHCache region: org.hibernate.cache.UpdateTimestampsCache 22:18:11,098 INFO main StandardQueryCache:<init>:76 - starting query cache at region: org.hibernate.cache.StandardQueryCache 22:18:11,099 WARN main EhCacheProvider:buildCache:93 - Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults. 22:18:11,099 DEBUG main EhCacheProvider:buildCache:96 - started EHCache region: org.hibernate.cache.StandardQueryCache 22:18:11,124 INFO main SimpleEntity:<init>:36 - create new simple entity 22:18:11,254 DEBUG main StandardQueryCache:get:125 - checking cached query results in region: org.hibernate.cache.StandardQueryCache 22:18:11,255 DEBUG main EhCache:get:74 - key: sql: select simpleenti0_.id as id0_ from SimpleEntity simpleenti0_ where simpleenti0_.id=?; parameters: 0, ; named parameters: {} 22:18:11,255 DEBUG main EhCache:get:83 - Element for sql: select simpleenti0_.id as id0_ from SimpleEntity simpleenti0_ where simpleenti0_.id=?; parameters: 0, ; named parameters: {} is null 22:18:11,255 DEBUG main StandardQueryCache:get:130 - query results were not found in cache Hibernate: select simpleenti0_.id as id0_ from SimpleEntity simpleenti0_ where simpleenti0_.id=? 22:18:11,261 DEBUG main StandardQueryCache:put:96 - caching query results in region: org.hibernate.cache.StandardQueryCache; timestamp=5399282857005056 22:18:11,264 DEBUG main StandardQueryCache:get:125 - checking cached query results in region: org.hibernate.cache.StandardQueryCache 22:18:11,264 DEBUG main EhCache:get:74 - key: sql: select simpleenti0_.id as id0_ from SimpleEntity simpleenti0_ where simpleenti0_.id=?; parameters: 0, ; named parameters: {} 22:18:11,264 DEBUG main StandardQueryCache:isUpToDate:183 - Checking query spaces for up-to-dateness: [SimpleEntity] 22:18:11,264 DEBUG main EhCache:get:74 - key: SimpleEntity 22:18:11,264 DEBUG main EhCache:get:83 - Element for SimpleEntity is null 22:18:11,264 DEBUG main StandardQueryCache:get:140 - returning cached query results Statistics[start time=1318184290719,sessions opened=4,sessions closed=0,transactions=0,successful transactions=0,optimistic lock failures=0,flushes=0,connections obtained=1,statements prepared=1,statements closed=1,second level cache puts=0,second level cache hits=0,second level cache misses=0,entities loaded=0,entities updated=0,entities inserted=0,entities deleted=0,entities fetched=0,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,queries executed to database=1,query cache puts=1,query cache hits=1,query cache misses=1,max query time=5] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.58 sec
|
profit?  А в рамках транзакции нельзя? Дальнейшие манипуляции привели меня в идее сохранить сущность в одной, законченной транзакции. Запросы на выборку делать после её завершения, что снова привело к успеху. | Код | public class SimpleEntityDaoImpl extends HibernateDaoSupport implements SimpleEntityDao { @Override @Transactional(propagation = Propagation.REQUIRES_NEW) // выполняем метод в рамках отдельной транзакции public SimpleEntity create(SimpleEntity entity) { getSession().persist(entity); return entity; }
@Override public Statistics getStatistics() { return getSession().getSessionFactory().getStatistics(); }
@Override public List<SimpleEntity> findAll() { final Query query = getSession().createQuery("from SimpleEntity"); query.setCacheMode(CacheMode.NORMAL); query.setCacheable(true); return query.list(); }
@Override public SimpleEntity findById(long id) { final Query query = getSession().createQuery("from SimpleEntity as se where se.id = ?"); query.setParameter(0, id); query.setCacheMode(CacheMode.NORMAL); query.setCacheable(true); return (SimpleEntity) query.uniqueResult(); } }
|
| Код | @ContextConfiguration(locations = "classpath*:/META-INF/spring/*.xml") @TestExecutionListeners(TransactionalTestExecutionListener.class) // вернем транзакционные атрибуты для тестов @Transactional(propagation = Propagation.REQUIRED) public class SimpleEntityDaoTest extends AbstractTestNGSpringContextTests { @Resource(name = "simpleEntityDao") private SimpleEntityDao simpleEntityDao;
@Test public void queryCache() { final SimpleEntity entity = simpleEntityDao.create(new SimpleEntity());
simpleEntityDao.findById(entity.getId()); simpleEntityDao.findById(entity.getId());
System.out.println(simpleEntityDao.getStatistics()); } }
|
| Цитата | Running TestSuite 22:29:44,808 INFO main RegionFactoryCacheProviderBridge:<init>:61 - Cache provider: org.hibernate.cache.EhCacheProvider 22:29:44,886 WARN main EhCacheProvider:buildCache:93 - Could not find configuration [com.mycompany.dbtest.entity.SimpleEntity]; using defaults. 22:29:44,900 DEBUG main EhCacheProvider:buildCache:96 - started EHCache region: com.mycompany.dbtest.entity.SimpleEntity 22:29:44,920 INFO main SimpleEntity:<init>:36 - create new simple entity 22:29:45,230 INFO main UpdateTimestampsCache:<init>:57 - starting update timestamps cache at region: org.hibernate.cache.UpdateTimestampsCache 22:29:45,230 WARN main EhCacheProvider:buildCache:93 - Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults. 22:29:45,231 DEBUG main EhCacheProvider:buildCache:96 - started EHCache region: org.hibernate.cache.UpdateTimestampsCache 22:29:45,232 INFO main StandardQueryCache:<init>:76 - starting query cache at region: org.hibernate.cache.StandardQueryCache 22:29:45,232 WARN main EhCacheProvider:buildCache:93 - Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults. 22:29:45,232 DEBUG main EhCacheProvider:buildCache:96 - started EHCache region: org.hibernate.cache.StandardQueryCache 22:29:45,292 INFO main SimpleEntity:<init>:36 - create new simple entity Hibernate: insert into SimpleEntity (id) values (default) Hibernate: values identity_val_local() 22:29:45,322 DEBUG main UpdateTimestampsCache:preinvalidate:71 - Pre-invalidating space [SimpleEntity] 22:29:45,327 DEBUG main UpdateTimestampsCache:invalidate:95 - Invalidating space [SimpleEntity], timestamp: 5399285699899392 22:29:45,439 DEBUG main StandardQueryCache:get:125 - checking cached query results in region: org.hibernate.cache.StandardQueryCache 22:29:45,439 DEBUG main EhCache:get:74 - key: sql: select simpleenti0_.id as id0_ from SimpleEntity simpleenti0_ where simpleenti0_.id=?; parameters: 1, ; named parameters: {} 22:29:45,439 DEBUG main EhCache:get:83 - Element for sql: select simpleenti0_.id as id0_ from SimpleEntity simpleenti0_ where simpleenti0_.id=?; parameters: 1, ; named parameters: {} is null 22:29:45,440 DEBUG main StandardQueryCache:get:130 - query results were not found in cache Hibernate: select simpleenti0_.id as id0_ from SimpleEntity simpleenti0_ where simpleenti0_.id=? 22:29:45,445 INFO main SimpleEntity:<init>:36 - create new simple entity 22:29:45,447 DEBUG main EhCache:get:74 - key: com.mycompany.dbtest.entity.SimpleEntity#1 22:29:45,447 DEBUG main EhCache:get:83 - Element for com.mycompany.dbtest.entity.SimpleEntity#1 is null 22:29:45,449 DEBUG main StandardQueryCache:put:96 - caching query results in region: org.hibernate.cache.StandardQueryCache; timestamp=5399285700399104 22:29:45,449 DEBUG main StandardQueryCache:get:125 - checking cached query results in region: org.hibernate.cache.StandardQueryCache 22:29:45,449 DEBUG main EhCache:get:74 - key: sql: select simpleenti0_.id as id0_ from SimpleEntity simpleenti0_ where simpleenti0_.id=?; parameters: 1, ; named parameters: {} 22:29:45,449 DEBUG main StandardQueryCache:isUpToDate:183 - Checking query spaces for up-to-dateness: [SimpleEntity] 22:29:45,449 DEBUG main EhCache:get:74 - key: SimpleEntity 22:29:45,450 DEBUG main UpdateTimestampsCache:isUpToDate:122 - [SimpleEntity] last update timestamp: 5399285699899392, result set timestamp: 5399285700399104 22:29:45,450 DEBUG main StandardQueryCache:get:140 - returning cached query results Statistics[start time=1318184984824,sessions opened=2,sessions closed=1,transactions=2,successful transactions=1,optimistic lock failures=0,flushes=1,connections obtained=2,statements prepared=3,statements closed=3,second level cache puts=1,second level cache hits=0,second level cache misses=0,entities loaded=1,entities updated=0,entities inserted=1,entities deleted=0,entities fetched=0,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,queries executed to database=1,query cache puts=1,query cache hits=1,query cache misses=1,max query time=7] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.7 sec
|
Выводы: Пока транзакция не завершена, кэширование её изменений в рамках query cache не имеет смысла, что скорее всего правильно.
--------------------
 нет времени думать - нужно писать КОД!
|