简体   繁体   English

Wildfly-Infinispan Transactions配置

[英]Wildfly - Infinispan Transactions configuration

I am using Wildfly 8.2 with its included Infinispan (6.0.2) and I am trying to cache all values from some Oracle database table in an Infispan cache. 我正在使用Wildfly 8.2及其随附的Infinispan(6.0.2),并且试图将来自某个Oracle数据库表的所有值都缓存在Infispan缓存中。 In most cases, it seems to work, but sometimes it does not. 在大多数情况下,它似乎有效,但有时却无效。 When accessing the cache.values() (which also may not be a good idea for performance, but is an example) it appears sometime to be empty, sometimes it contains the values correctly. 当访问cache.values() (对于性能而言可能也不是一个好主意,但这只是一个示例),它有时看上去是空的,有时它正确地包含了这些值。 Therefore I think it might a problem with the configuration of the transactions. 因此,我认为这可能与事务的配置有关。 When making the Infinispan cache non-transactional, the problem disappears. 使Infinispan缓存为非事务性时,问题消失了。

The service which accesses the cache and the DB is an EJB bean with container-managed transactions. 访问缓存和数据库的服务是具有容器管理事务的EJB bean。 On initialzation of the Service, all data is loaded from the DB (it does not contain many entries). 服务初始化时,所有数据都从DB加载(其中不包含许多条目)。 According to what's new in ejb 3.2 it should be possible to access the DB transactionally in a EJB Singleton bean. 根据ejb 3.2中的新功能,应该可以在EJB Singleton bean中以事务方式访问数据库。

Is the configuration of the data source and the Infinispan cache correct? 数据源和Infinispan缓存的配置是否正确? Can I use a non-XA datasource with Infinispan and expect it work consistently? 我可以将非XA数据源与Infinispan一起使用,并期望它能始终如一地工作吗? According to the Infinispan doc , NON_XA means that Infinispan is registering as a Synchronization, which should be ok, shouldn't it? 根据Infinispan的文档NON_XA表示Infinispan正在注册为同步,这应该没问题吗?

The cache is configured in the standalone-full.xml as follows (when removing <transaction mode="NON_XA" locking="PESSIMISTIC"/> the problem disappears, at the price of having no transactional cache): 缓存在standalone-full.xml中的配置如下(当删除<transaction mode="NON_XA" locking="PESSIMISTIC"/> ,问题消失了,但没有事务缓存):

        <cache-container name="cacheContainer" start="EAGER">
            <local-cache name="my_table_cache">
                <locking isolation="REPEATABLE_READ"/>
                <transaction mode="NON_XA" locking="PESSIMISTIC"/>
            </local-cache>
        </cache-container>

The Oracle DS is defined as follows Oracle DS定义如下

           <datasource jndi-name="java:jboss/datasources/myDataSource" pool-name="dataSource" enabled="true">
                <connection-url>jdbc:oracle:thin:@127.0.0.1:1523:orcl</connection-url>
                <driver>ojdbc7.jar</driver>
                <pool>
                    <max-pool-size>25</max-pool-size>
                </pool>
                <security>
                    <user-name>myuser</user-name>
                    <password>myuser</password>
                </security>
                <timeout>
                    <blocking-timeout-millis>5000</blocking-timeout-millis>
                </timeout>
            </datasource>

My Service (the dao is using simple JDBC operations, not Hibernate or similar) 我的服务(Dao使用的是简单的JDBC操作,而不是Hibernate或类似方法)

@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class MyService {

@Resource(lookup = "java:jboss/infinispan/container/cacheContainer")
protected EmbeddedCacheManager cm;

protected Cache<String, MyEntity> cache;

@PostConstruct
private void init() {
    try {
        cache = getCache();
    } catch (SQLException ex) {
        log.fatal("could not initialize caches", ex);
        throw new IllegalStateException(ex);
    }
}

public Cache<String, MyEntity> getCache() {
    Cache<String, MyEntity> cache = cm.getCache(getCacheName(), true);
    fillCache(cache);
    return cache;
}

protected void fillCache(Cache<String, MyEntity> cache) {
    List<MyEntity> entities = myDao.getEntities();
    for (MyEntity e : entities) {
        cache.put(e.getKey, e);
    }
}

public MyEntity getEntity(String key) {
    return cache.get(key);
}
public void insert(MyEntity entity) {
    myDao.insert(entity);
    cache.put(entity.getKey(), entity);
}
public void debug() {
    log.debug(cache.values());
}
}

When using NON_XA transactions, failure to commit TX in cache may let the transaction to commit, and you would not get any exception that would tell you that the cache is inconsistent. 当使用NON_XA事务时,未能在缓存中提交TX可能会使该事务得以提交,并且您将不会收到任何异常通知您缓存不一致。

As for cache.values() , prior to Infinispan 7.0 it returns only local entries, however that should not matter in your case - with local cache all entries are local. 至于cache.values() ,在Infinispan 7.0之前,它仅返回本地条目,但是在您的情况下这并不重要-对于本地缓存,所有条目都是本地的。 The transactional consistency of this operation should hold. 此操作的事务一致性应保持不变。 I don't see anything wrong in your configuration. 我看不到您的配置有任何问题。

Generally I would recommend to use Infinispan module in Hibernate ORM rather than trying to do the caching on your own, as you show here. 通常,我建议您在Hibernate ORM中使用Infinispan模块,而不是尝试自己进行缓存,如此处所示。

According to the accepted answer, the configuration is correct. 根据接受的答案,配置正确。 If anyone has similar problems: The problem seemed to be that one of the read methods like MyService.getEntity() is in some application-specific contexts being called extremly often (which I was not aware of), so using optimistic locking and READ_COMMITTED instead of REPEATABLE_READ seems to make it working as expected. 如果有人遇到类似的问题:问题似乎是MyService.getEntity()类的读取方法之一在某些特定于应用程序的上下文中经常被极端调用(我不知道),因此使用乐观锁定和READ_COMMITTED代替的REPEATABLE_READ似乎可以按预期工作。

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

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