Tuesday, 7 October 2014

Invalid content was found starting with element 'shared-cache-mode'

Whenever you try to run a persistence-unit with shared-cache-mode tag, you should be careful with your persistence xml header.

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">

Add hibernate-jpa-2.0-api-1.0.0.Final.jar into your class path

Otherwise you will get a invalid persistence.xml error.

In your entity class, @Cacheable annotation should be used to cache the particular entity.
Shared Cache Mode Options
ALL - All entities and entity-related state and data are cached.
DISABLE_SELECTIVE - Caching is enabled for all entities except those for which Cacheable(false) is specified.
ENABLE_SELECTIVE - Caching is enabled for all entities for Cacheable(true) is specified.
NONE -  Caching is disabled for the persistence unit.

My Personal Experience : When you open a connection, a session will be created. Then what ever db operations you perform, those entities will be cached in 1st level cache by default. you can update the cached entity to db, but not in the reverse way. For example, you are executing a select query, first time data is fetched from db and saved in cache. then if you execute the same query, entity object will be fetched from cache. Later you change the data manually in db, and execute the select query via application, then you will not get the updated values. Since the old entity is available in 1st level cache (session will be available).
You can try evict method to remove the old entity object from cache (didnt work in my case).

But the same will work, if you close the connection immediately after the fetch. then whenever needed open connection, select data & close connection. Because when closing connection, session will be destroyed.

It depends on the application's need, if your data will not be changed by any external factor, then caching will be a best option to improve the performance.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Recent Posts

Micro VMs & Unikernels

This post is a follow up of this post . In Previous Post, we discussed about Virtual Machines & Containers architecture. In this post, w...

Older Posts