Infinispan:锁定在远程事务缓存中

时间:2018-09-28 08:25:48

标签: java caching infinispan infinispan-9

我们尝试将infinispan用作具有读取锁的远程缓存。客户端正在读取“ put”以获取键的锁定,如悲观事务缓存部分中infinispan文档中所述,“当cache.put(k1,v1)返回时, k1被锁定,并且群集中任何运行的其他事务都无法对其进行写操作;仍然可以读取k1;对k1的锁定在事务完成(提交或回滚)时释放。 这样的场景:

transactionManager.begin();
// read with put to acquire write lock
String value = getRemoteCache().get(key);
getRemoteCache().put(key, value);

// do smthing with the value

getRemoteCache().put(key, newValue);
transactionManager.commit();

远程缓存配置为具有悲观锁定的事务性:

   <local-cache name="default"> <locking isolation="REPEATABLE_READ" acquire-timeout="30000" concurrency-level="1000" striping="false"/>
        <transaction  mode="NON_XA" locking="PESSIMISTIC"/>
   </local-cache>

并且客户端正在使用以下配置作为HOTROD客户端访问remoteCacheManager:

  ConfigurationBuilder builder = new ConfigurationBuilder();
    // add more configurations ?
    builder.transaction().transactionManagerLookup(GenericTransactionManagerLookup.getInstance());
    builder.transaction().transactionMode(TransactionMode.NON_XA);
    builder.addServer().host(readServerHostConfiguration()).port(readServerPortConfiguration());
    return new RemoteCacheManager(builder.build(), true);

尽管客户端可以同时“读取并放置”一个值,但是Concurent客户端在读取值时放置该值时并没有获得异常,而只是在提交转换后才获得异常。那是预期的行为吗?

1 个答案:

答案 0 :(得分:2)

是的。

如文档(http://infinispan.org/docs/stable/user_guide/user_guide.html#hot_rod_transaction中所述),在客户端上运行的事务具有乐观的语义。仅在提交期间获取锁。

悲观锁定事务仅在嵌入式模式下有效。