JPA sleep caching

I have a table called Master_Info_tbl . His lookup table:

Here is the table code:

@Entity @Table(name="MASTER_INFO_T") public class CodeValue implements java.io.Serializable { private static final long serialVersionUID = -3732397626260983394L; private Integer objectid; private String codetype; private String code; private String shortdesc; private String longdesc; private Integer dptid; private Integer sequen; private Timestamp begindate; private Timestamp enddate; private String username; private Timestamp rowlastchange; //getter Setter methods 

I have a service level that calls a method

service.findbycodeType ("Code1");

in the same way, this table is requested for other types of code, as well as for example code2, code3, etc. to code 10 , which gets the result set from the same table and appears in the jsp page dropdown list, since these dropdown pages are on 90% of the pages that I believe cache them around the world,

Any idea how to achieve this?

FYI: I use JPA and Hibernate with Struts2 and Spring. The database used is DB2 UDB8.2


@Pascal
Thanks so much for all your answers. It really helped me. I implemented everything that I had to implement (I think). However, I do not know if second level caching works or not. Since I can’t see anything in the log4j logging from the cache, and also nothing is displayed in the console. To show that the implementation of the second level cache works, I need to have some kind of confirmation and show it to my manager. So I'm kinda stuck.
Please, help!
I know that I am very close to finish it, but just ....
Here is my code (if you think something is missing or something should not be there, please let me know):
Entity class

 @Entity @Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) @Table(name = "CODEVALUE_T") public class CodeValue implements java.io.Serializable { //all the field name with getter and setter } 

persistence.xml:

 <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" /> <property name="hibernate.show_sql" value="false" /> <property name="hibernate.cache.use_second_level_cache" value="true"/> <property name="hibernate.cache.use_query_cache" value="true"/> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" /> </properties> 

DAO Service Level:

 try { System.out.println("Calling the findByCodetype() method"); final String queryString = "select model from CodeValue model where model.codetype" + "= :propertyValue" + " order by model.code"; Query query = em.createQuery(queryString); query.setHint("org.hibernate.cacheable", true); query.setParameter("propertyValue", propertyName); return query.getResultList(); } catch (RuntimeException re) { logger.error("findByCodetype failed: ", re); throw re; } 

Log4j.property value to display verbose
log4j.category.org.hibernate.cache = DEBUG

ehcache.xml (this place is in the src / folder where my struts.xml file is located)

 <ehcache> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="6000" timeToLiveSeconds="12000" overflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /> </ehcache> 
+4
source share
4 answers

(...) since these dropdown pages are 90% of the pages that I think caches them all over the world.

Using a query cache will be very suitable for this use case. So, activate the second level cache, cache the CodeValue object (see 2.2.8. Cache objects ) and put the findbycodeType query in the query cache . To do this, use:

 javax.persistence.Query query = manager.createQuery(...); query.setHint("org.hibernate.cacheable", true); 

And to clarify, the combination of the request and the values ​​provided as parameters of this request is used as a key, and the value is a list of identifiers for this request. Schematically, something like this:

  * ------------------------------------------------- ------------------------- *
 |  Query Cache |
 | ------------------------------------------------- ------------------------- |
 |  ["from CodeValue c where c.codetype =?", ["Code1"]] -> [1, 2, ...] |
 |  ["from CodeValue c where c.codetype =?", ["Code2"]] -> [3, 5, 6, ...] |
 * ------------------------------------------------- ------------------------- *

Thus, calling your method with different arguments will not "clear previous data", the argument is part of the key (or the query cache does not work).

Note that this is Hibernate specific, JPA 1.0 does not define second-level caching and query caching.

see also

+12
source

Sleep mode sadly writes SQL queries to the console, even if the hit comes from the cache.

False, Hibernate does not register SQL commands, if the cache was used, it is confirmed with the following configuration:

persistence.xml:

 <property name="hibernate.cache.use_second_level_cache" value="true"/> <property name="hibernate.cache.use_query_cache" value="true" /> 

Entity Class:

 @NamedQuery(name = "byNameLike", query = "SELECT p FROM Person p WHERE name like ?1", hints = { @QueryHint(name = "org.hibernate.cacheable", value = "true") }) 

Logging:

 Enabled org.hibernate.SQL category. 
+6
source

Take a look at the hibernate query cache .

You can also use a little lightweight offline caching like OSCache . It gives you full control over what, when and how it is cached, but you also need to think about when to update the cache.

0
source

You do not see anything in the log, because Cache does not inform you that you are deleting / skipping by default. Use statistics="true" in the defaultCache element to include information about this.

Sleep mode sadly writes SQL queries to the console, even if the hit comes from the cache.

-2
source

Source: https://habr.com/ru/post/1310423/


All Articles