简体   繁体   中英

Infinispan custom interceptors not working with Hibernate L2 cache?

In my project i have to intercept Hibernate L2 cache calls in-order to set lifespan for some selected cached objects. The problem is hibenate cache calls never comes through the interceptor.

My Interceptor ( test code)

public class HibernateCacheInterceptor extends BaseCustomInterceptor {
   private static Log log = LogFactory.getLog(HibernateCacheInterceptor.class);

@Override
public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable {
    log.info(this.getClass().getName() + " intercept.");
      if (command.getValue() instanceof Car) {
          return null;
       } else {
          return invokeNextInterceptor(ctx, command);
      }
   }

 }

My Cache definition ( infinispan.xml)

 <namedCache name="mycache">
    <customInterceptors>
        <interceptor position="FIRST" class="test.HibernateCacheInterceptor">
        </interceptor>
    </customInterceptors>
</namedCache>

org.infinispan.Cache.put(key,value) calls comes to interceptor but hibernate cache calls does not comes. Does hibernate uses different API to skip interceptors ? How can i intercept hibernate cache calls ?

No, Hibernate cannot skip interceptors - all of the logic of core Infinispan is triggered from interceptors.

My guess is that Hibernate does not use the cache (when you open JConsole, can you see entries there in Infinispan?), uses another cache (without the interceptor) or buffers the entries before inserting to the cache.

You can try to set trace logging on both hibernate and infinispan.

There are easier ways to achieve this. As indicated in the Infinispan 2LC documentation (see advanced configuration section), each entity can be assigned a specific cache where you can tweak the settings declaratively. The easiest thing is to check which is the Infinispan configuration that's used in your application, copy the default cache used for entities, give it a different name and tweak it. Then, you need to define something like:

<property name="hibernate.cache.infinispan.com.acme.Person.cfg"
          value="person-entity"/>

Where person-entity is the name of the cache for that particular entity.

NOTE: Remember that if you're running on Wildfly or EAP, the property name requires indicating the deployment archive and persistence unit name. This is explained in the advanced configuration section.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM