3.8. 热 Rod Java 客户端事务


您可以在 JTA {tx}s 中配置和使用 Hot Rod 客户端。

要参与事务,Hot Rod 客户端需要与之交互的 {tm},以及它是否通过 {sync} 或 {xa} 接口参与事务。

重要

事务在准备阶段获取条目的写锁是最佳的。为了避免数据不一致,请务必阅读有关 使用 Transactions 的冲突

3.8.1. 配置服务器

服务器中的缓存还必须是事务处理,客户端才能参与 JTA {tx}s。

需要以下服务器配置,否则只进行事务回滚:

  • 隔离级别必须是 REPEATABLE_READ
  • 锁定模式必须是 PESSIMISTIC。在以后的发行版本中,支持 OPTIMISTIC 锁定模式。
  • 事务模式应该是 NON_XANON_DURABLE_XA。热 Rod 事务不应使用 FULL_XA,因为它会降低性能。

例如:

<replicated-cache name="hotrodReplTx">
  <locking isolation="REPEATABLE_READ"/>
  <transaction mode="NON_XA" locking="PESSIMISTIC"/>
</replicated-cache>
Copy to Clipboard Toggle word wrap

热 Rod 事务有自己的恢复机制。

3.8.2. 配置 Hot Rod 客户端

在创建 {rcm} 时,您可以设置 {rc} 使用的默认 {tm} 和 {tx-mode}。

{rcm} 可让您只为事务缓存创建一个配置,如下例所示:

org.infinispan.client.hotrod.configuration.ConfigurationBuilder cb = new org.infinispan.client.hotrod.configuration.ConfigurationBuilder();
//other client configuration parameters
cb.transaction().transactionManagerLookup(GenericTransactionManagerLookup.getInstance());
cb.transaction().transactionMode(TransactionMode.NON_XA);
cb.transaction().timeout(1, TimeUnit.MINUTES)
RemoteCacheManager rmc = new RemoteCacheManager(cb.build());
Copy to Clipboard Toggle word wrap

前面的配置应用到远程缓存的所有实例。如果需要将不同的配置应用到远程缓存实例,您可以覆盖 {rc} 配置。请参阅 覆盖 RemoteCacheManager 配置

有关配置参数的文档,请参阅 {cb} Javadoc。

您还可以使用属性文件配置 Java Hot Rod 客户端,如下例所示:

infinispan.client.hotrod.transaction.transaction_manager_lookup = org.infinispan.client.hotrod.transaction.lookup.GenericTransactionManagerLookup
infinispan.client.hotrod.transaction.transaction_mode = NON_XA
infinispan.client.hotrod.transaction.timeout = 60000
Copy to Clipboard Toggle word wrap

3.8.2.1. TransactionManagerLookup Interface

TransactionManagerLookup 提供了一个入口点,用于获取 {tm}。

TransactionManagerLookup 的可用实现:

{gtml}
查找类,用于查找在 Java EE 应用服务器中运行的 {tm}。如果找不到 {tm},则默认为 {rtm}。这是 Hot Rod Java 客户端的默认设置。
提示

在大多数情况下,{gtml} 适合。但是,如果您需要集成自定义 {tm},您可以实现 TransactionManagerLookup 接口。

{rtml}
如果没有其他实现,则基本和易失性 {tm}。请注意,这个实现在处理并发事务和恢复时有很大的限制。

3.8.3. 事务模式

{tx-mode} 控制 {rc} 与 {tm} 交互的方式。

重要

在 Data Grid 服务器和您的客户端应用程序上配置事务模式。如果客户端试图对非事务缓存执行事务操作,则可能会出现运行时异常。

在 Data Grid 配置和客户端设置中,事务模式都相同。将以下模式与客户端搭配使用,请参阅服务器的 Data Grid 配置模式:

NONE
{rc} 不与 {tm} 交互。这是默认模式,不是事务处理模式。
NON_XA
{rc} 通过 {sync} 与 {tm} 交互。
NON_DURABLE_XA
{rc} 通过 {xa} 与 {tm} 交互。恢复功能被禁用。
FULL_XA
{rc} 通过 {xa} 与 {tm} 交互。启用恢复功能。调用 XaResource.recover () 方法,以检索要恢复的事务。

3.8.4. 覆盖缓存实例的配置

因为 {rcm} 不支持每个缓存实例的不同配置。但是,{rcm} 包含返回 {rc} 实例的 getCache (String) 方法,可让您覆盖一些配置参数,如下所示:

getCache (String cacheName, TransactionMode transactionMode)
返回 {rc} 并覆盖配置的 {tx-mode}。
getCache (String cacheName, boolean forceReturnValue, TransactionMode transactionMode)
与之前的相同,但也可为写操作强制执行返回值。
getCache (String cacheName, TransactionManager transactionManager)
返回 {rc} 并覆盖配置的 {tm}。
getCache (String cacheName, boolean forceReturnValue, TransactionManager transactionManager)
与之前的相同,但也可为写操作强制执行返回值。
getCache (String cacheName, TransactionMode transactionMode, TransactionManager transactionManager)
返回 {rc} 并覆盖配置的 {tm} 和 {tx-mode}。如果 transactionManagertransactionMode 为 null,则使用配置的值。
getCache (String cacheName, boolean forceReturnValue, TransactionMode transactionMode, TransactionManager transactionManager)
与之前的相同,但也可为写操作强制执行返回值。
注意

getCache (String) 方法返回 {rc} 实例,无论它们是事务。{rc} 包含 getTransactionManager () 方法,它返回了缓存使用的 {tm} 方法。如果 {rc} 不是事务处理,则方法会返回 null

3.8.5. 使用事务检测冲突

事务使用键的初始值来检测冲突。

例如,当事务开始时,"k" 的值为 "v"。在准备阶段,事务从服务器获取"k"以读取值。如果值已更改,事务会回滚以避免冲突。

注意

事务使用版本来检测更改,而不是检查值相等。

forceReturnValue 参数控制对 {rc} 的写入操作,并帮助避免冲突。它有以下值:

  • 如果为 true,则 {tm} 在执行写入操作前从服务器获取最新的值。但是,forceReturnValue 参数仅适用于首次访问密钥的操作。
  • 如果为 false,则 {tm} 在执行写入操作前不会从服务器获取最新的值。
注意

此参数不会影响 替换 或放置 if Absent条件 写入操作,因为它们需要最新的值。

以下事务提供了一个示例,其中 forceReturnValue 参数可以防止出现冲突的写入操作:

事务 1 (TX1)

RemoteCache<String, String> cache = ...
TransactionManager tm = ...

tm.begin();
cache.put("k", "v1");
tm.commit();
Copy to Clipboard Toggle word wrap

事务 2 (TX2)

RemoteCache<String, String> cache = ...
TransactionManager tm = ...

tm.begin();
cache.put("k", "v2");
tm.commit();
Copy to Clipboard Toggle word wrap

在这个示例中,TX1 和 TX2 并行执行。"k"的初始值为 "v"。

  • 如果 forceReturnValue = true,则 cache.put () 操作从 TX1 和 TX2 的服务器获取"k"的值。首先获取"k"锁定的事务,然后提交。其他事务会在提交阶段回滚,因为事务可以检测到 "k" 的值为 "v"。
  • 如果 forceReturnValue = false,则 cache.put () 操作不会从服务器获取"k"的值并返回 null。TX1 和 TX2 都可以成功提交,这会导致冲突。这是因为事务都无法检测到初始值为 "k" 已更改。

以下事务包括 cache.get () 操作,以便在执行 cache.put () 操作前读取 "k" 的值:

事务 1 (TX1)

RemoteCache<String, String> cache = ...
TransactionManager tm = ...

tm.begin();
cache.get("k");
cache.put("k", "v1");
tm.commit();
Copy to Clipboard Toggle word wrap

事务 2 (TX2)

RemoteCache<String, String> cache = ...
TransactionManager tm = ...

tm.begin();
cache.get("k");
cache.put("k", "v2");
tm.commit();
Copy to Clipboard Toggle word wrap

在前面的示例中,TX1 和 TX2 都是读取密钥,因此 forceReturnValue 参数不会生效。一个事务提交,另一个回滚。但是 cache.get () 操作需要额外的服务器请求。如果您不要求服务器请求的 cache.put () 操作返回值效率低下。

以下示例演示了如何使用在 RemoteCacheManager 中配置的 TransactionManagerTransactionMode

//Configure the transaction manager and transaction mode.
org.infinispan.client.hotrod.configuration.ConfigurationBuilder cb = new org.infinispan.client.hotrod.configuration.ConfigurationBuilder();
cb.transaction().transactionManagerLookup(RemoteTransactionManagerLookup.getInstance());
cb.transaction().transactionMode(TransactionMode.NON_XA);

RemoteCacheManager rcm = new RemoteCacheManager(cb.build());

//The my-cache instance uses the RemoteCacheManager configuration.
RemoteCache<String, String> cache = rcm.getCache("my-cache");

//Return the transaction manager that the cache uses.
TransactionManager tm = cache.getTransactionManager();

//Perform a simple transaction.
tm.begin();
cache.put("k1", "v1");
System.out.println("K1 value is " + cache.get("k1"));
tm.commit();
Copy to Clipboard Toggle word wrap

3.8.7. 覆盖 Transaction Manager

以下示例演示了如何使用 getCache 方法覆盖 TransactionManager

//Configure the transaction manager and transaction mode.
org.infinispan.client.hotrod.configuration.ConfigurationBuilder cb = new org.infinispan.client.hotrod.configuration.ConfigurationBuilder();
cb.transaction().transactionManagerLookup(RemoteTransactionManagerLookup.getInstance());
cb.transaction().transactionMode(TransactionMode.NON_XA);

RemoteCacheManager rcm = new RemoteCacheManager(cb.build());

//Define a custom TransactionManager.
TransactionManager myCustomTM = ...

//Override the TransactionManager for the my-cache instance. Use the default configuration if null is returned.
RemoteCache<String, String> cache = rcm.getCache("my-cache", null, myCustomTM);

//Perform a simple transaction.
myCustomTM.begin();
cache.put("k1", "v1");
System.out.println("K1 value is " + cache.get("k1"));
myCustomTM.commit();
Copy to Clipboard Toggle word wrap

3.8.8. 覆盖事务模式

以下示例演示了如何使用 getCache 方法覆盖 TransactionMode

//Configure the transaction manager and transaction mode.
org.infinispan.client.hotrod.configuration.ConfigurationBuilder cb = new org.infinispan.client.hotrod.configuration.ConfigurationBuilder();
cb.transaction().transactionManagerLookup(RemoteTransactionManagerLookup.getInstance());
cb.transaction().transactionMode(TransactionMode.NON_XA);

RemoteCacheManager rcm = new RemoteCacheManager(cb.build());

//Override the transaction mode for the my-cache instance.
RemoteCache<String, String> cache = rcm.getCache("my-cache", TransactionMode.NON_DURABLE_XA, null);

//Return the transaction manager that the cache uses.
TransactionManager tm = cache.getTransactionManager();

//Perform a simple transaction.
tm.begin();
cache.put("k1", "v1");
System.out.println("K1 value is " + cache.get("k1"));
tm.commit();
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat