java - EHCache 是如何实现它的事务的?

标签 java performance transactions ehcache

这个问题可能听起来很模糊,但我正在尝试理解 EHCache 事务能力的一般概念。
假设我将 EHCache 配置为内存缓存,并将其配置为缓存 MyObject
如果这是作为事务的一部分完成的,EHCache 是否会克隆我正在检索的 MyObject 实例?

我问这个问题主要是因为有人建议我(在我的 question 的回答中)使用 EHCache,我担心它对性能的影响。 MyObject 是一个中等重量的对象,我不想不必要地复制它。
另外,为了验证一下,EHCache 只在事务中阻止写入对象,对吗?

谢谢,
一泰

最佳答案

我认为有关 JTA 的文档的以下部分支持回答您的大部分问题:

Using a JTA Cache

All or nothing

If a cache is enabled for JTA all operations on it must happen within a transaction context, otherwise a TransactionRequiredException will be thrown.

Change Visibility

The isolation level offered in Ehcache JTA is READ_COMMITTED. Ehcache is an XAResource. Full two-phase commit is supported.

Specifically:

  • All mutating changes to the cache are transactional including put, remove, putWithWriter, removeWithWriter and removeAll.
  • Mutating changes are not visible in the local JVM to or across the cluster until COMMIT has been called.
  • Until then, read such as by cache.get(...) by other transactions will return the old copy. Reads do not block.

Write-behind and Write-through

If your XA enabled cache is being used with a writer, write operations will be queued until transaction commit time. Solely a Write-through approach would have its potential XAResource participate in the same transaction. Write-behind, while supported, should probably not be used with an XA transactional Cache, as the operations would never be part of the same transaction. Your writer would also be responsible for obtaining a new transaction...

Using Write-through with a non XA resource would also work, but there is no guarantee the transaction will succeed after the write operation have been executed successfully. On the other hand, any thrown exception during these write operations would cause the transaction to be rolled back by having UserTransaction.commit() throw a RollbackException.

关于性能,除非您的对象有数百 MB,否则我不会太担心。但如果这真的是一个问题,请衡量事物(一如既往)。

关于java - EHCache 是如何实现它的事务的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3967111/

相关文章:

performance - React 16 性能中的无状态函数与类

sql-server - SQL Server 是否将 Select...Insert 查询包装到隐式事务中?

json - Breeze Sequelize : get entities in the server + transaction

java - 了解spring xml注释元数据吗?

java - 将 C# DateTime 转换为 GregorianCalendar 日期

java - 如何处理二维数组中的特定行

mysql - 如何使用多台机器扩展 MySQL?

java - Play Framework 安全问题

r - R : efficiently count number of edges between multiple sets of vertices 中的 igraph

Node.js - 使用 Redis 原子更新进行扩展