The properties seems all right to me. But I think I found...thanks to your suggestion.
I entered "qr.getResultList()", IntelliJ decompiles for me the .class files... deep inside unknown world !!!
In Tomcat, the queryCache is correctly loaded in hibernate.loader.Loader.listUsingQueryCache() , just like in junit. But I found that there was a difference at UpdateTimestampsCache.isUpToDate() ! So I had the idea that in junit, there are 2 entity managers created, one for each test, while in tomcat there is only one. After some tests I found the following conclusion :
In tomcat AND in junit, this code:
Code:
EntityManager entityManager=factory.createEntityManager();
// open transaction
entityManager.getTransaction().begin();
// create user
User user=new User();
user.setName("a");
entityManager.persist(user);
// commit transaction
entityManager.getTransaction().commit();
// finish
entityManager.close();
//
//
entityManager=factory.createEntityManager();
// request first
Query qr=entityManager.createQuery("select u from User u");
qr.setHint("org.hibernate.cacheable",true);
List<User> users2=(List<User>)qr.getResultList();
User firstuser=users2.get(0);
// request second
qr=entityManager.createQuery("select u from User u");
qr.setHint("org.hibernate.cacheable",true);
users2=(List<User>)qr.getResultList();
firstuser=users2.get(0);
// finish
entityManager.close();
produces the following mysql requests :
Code:
2017-10-16T15:02:11.851499Z 677 Query SET autocommit=0
2017-10-16T15:02:11.851791Z 677 Query SET autocommit=0
2017-10-16T15:02:11.852733Z 677 Query insert into testdata (name) values ('a')
2017-10-16T15:02:11.854003Z 677 Query commit
2017-10-16T15:02:11.856762Z 677 Query SET autocommit=1
2017-10-16T15:02:11.858292Z 677 Query SET autocommit=0
2017-10-16T15:02:11.858716Z 677 Query select user0_.id as id1_0_, user0_.name as name2_0_ from testdata user0_
2017-10-16T15:02:11.885065Z 677 Query SET autocommit=1
while this code (commenting the close/create in the middle):
Code:
EntityManager entityManager=factory.createEntityManager();
// open transaction
entityManager.getTransaction().begin();
// create user
User user=new User();
user.setName("a");
entityManager.persist(user);
// commit transaction
entityManager.getTransaction().commit();
// finish
//entityManager.close();
//
//
//entityManager=factory.createEntityManager();
// request first
Query qr=entityManager.createQuery("select u from User u");
qr.setHint("org.hibernate.cacheable",true);
List<User> users2=(List<User>)qr.getResultList();
User firstuser=users2.get(0);
// request second
qr=entityManager.createQuery("select u from User u");
qr.setHint("org.hibernate.cacheable",true);
users2=(List<User>)qr.getResultList();
firstuser=users2.get(0);
// finish
entityManager.close();
produces the follwing queries in mysql :
Code:
2017-10-16T15:03:26.750906Z 678 Query SET autocommit=0
2017-10-16T15:03:26.751182Z 678 Query SET autocommit=0
2017-10-16T15:03:26.752156Z 678 Query insert into testdata (name) values ('a')
2017-10-16T15:03:26.753986Z 678 Query commit
2017-10-16T15:03:26.754612Z 678 Query SET autocommit=1
2017-10-16T15:03:26.756820Z 678 Query SET autocommit=0
2017-10-16T15:03:26.757320Z 678 Query select user0_.id as id1_0_, user0_.name as name2_0_ from testdata user0_
2017-10-16T15:03:26.803988Z 678 Query SET autocommit=1
2017-10-16T15:03:26.805503Z 678 Query SET autocommit=0
2017-10-16T15:03:26.806285Z 678 Query select user0_.id as id1_0_, user0_.name as name2_0_ from testdata user0_
2017-10-16T15:03:26.816678Z 678 Query SET autocommit=1
I don't understand the reason for that. Why should I close the entity manager between the add and the query ? As long as the commit is done I should be able to use the same entity manager for all my queries ? (in one http request).
Here the code that demonstrated double request to mysql with junit template :
https://www.5flow.com/tmp/ehcache-2017-10-16.2.zipThanks for any explanation...