简体   繁体   English

如何在JBoss AS7中使用hibernate-infinispan的write behind方法

[英]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. 我在独立的JBoss AS7中创建了一个REST API,使用hibernate作为JPA提供程序,使用Infinispan作为二级缓存。

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. 我在https://docs.jboss.org/author/display/ISPN/Write-Through+And+Write-Behind+Caching中看到,Infinispan可以使用后写方法,将数据从缓存中异步保存到db和这是我的项目所需要的东西。

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. 我有一个名为Stat的实体类。 In that class, I have an int balance value, something like: 在那个类中,我有一个int余额值,如:

@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. 其中一个REST方法同时访问该平衡,从中减去一个并在数据库中更新它。 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. 所以,我认为我可以使用Infinispan来更新内存中的平衡,并让Infinispan异步地保持对数据库的更改。

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 ). 从我所看到的,没有属性可以传递给JPA配置或Hibernate配置,这将允许您启用此功能(可以在此处找到hibernate的属性列表)。

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. 你需要使自己的Infinispan的XML配置文件(可能复制 ),创建一个新的namedCache你想要取自属性inifnispan配置你给,然后在您的JPA或Hibernate配置添加“hibernate.cache。 infinispan.cfg“具有新配置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: 您还需要通过将以下属性添加到hibernate或JPA配置来更改用于Stat实体的缓存:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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