简体   繁体   中英

How to use the write behind method of hibernate-infinispan in JBoss AS7

I have created a REST API in a standalone JBoss AS7, using hibernate as JPA provider and Infinispan as second level cache.

I've seen in https://docs.jboss.org/author/display/ISPN/Write-Through+And+Write-Behind+Caching that Infinispan can use the write-behind method, to persist data asynchronously from the cache to the db and this is something that I need for my project.

I want to enable this feature, but I cannot find anywhere how to do it.

In case it helps, I can explain why I need this behavior. I have an entity class called Stat. In that class, I have an int balance value, something like:

@Entity
public class Stat{
    private int balance;
}

One of the REST methods accesses concurrently that balance, subtract one from it and update it in the database. This creates a bottleneck in the project, because many threads try to acquire the lock to read and update the balance to the database.

So, I thought that I could probably use Infinispan to update the balance in memory and leave Infinispan persist the changes to the database asynchronously.

Any help will be very much appreciated.

From what I'm seeing, there is not property you can pass to either the JPA configuration or Hibernate configuration that would let you enable this functionality (the list of properties for hibernate can be found here ).

You'll need to make your own infinispan xml configuration file (possibly copying this ), create a new namedCache with the properties you want taken from the inifnispan configuration you gave, and then in your JPA or hibernate configuration add the "hibernate.cache.infinispan.cfg" property with the location of your new configuration xml.

You will also need to change the cache used for your Stat entity by adding the following property to your hibernate or JPA config:

<property name="hibernate.cache.infinispan.com.package.Stat.cfg"  value="yourNewNamedCache"/>

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