EHCache 是否需要 put() 才能反映实例的更改(XA)?
我已经开始使用 EHCache 作为 JTA UserTransaction 中的事务缓存(XAResource),并且我看到了一些有点奇怪的东西,至少在我看来是这样,我想了解我的“所见”是否错误或者我的理解。
以下代码将返回 false
ut = getUserTransaction();
ut.begin();
MyClass a = myChache.get(key).getValue();
a.changeSomeInnerReferrence(newRefference);
ut.commit();
ut = getUserTransaction();
ut.begin();
MyClass b = myChache.get(key).getValue();
ut.commit();
return a.equals(b);
让我们假设 MyClass
有一个 MyOtherClass
类型的成员,并且 changeSomeInnerReferrence
更改了引用从当前值到参数;还假设 equals 考虑了该成员。
我注意到,除非我在 ut.commit()
之前添加 myChache.put(key,a)
,否则上述代码将返回 false
。
这是为什么?这是缓存的一般行为吗?我认为一旦调用提交,更改内部引用就会传播到缓存中。
谢谢,
一泰
I've started working with EHCache as a transactional cache (XAResource) in a JTA UserTransaction and I'm seeing something which is a bit strange, at least in my mind, and I'd like to understand whether my "seeing" is wrong or my understanding.
The following code will return false
ut = getUserTransaction();
ut.begin();
MyClass a = myChache.get(key).getValue();
a.changeSomeInnerReferrence(newRefference);
ut.commit();
ut = getUserTransaction();
ut.begin();
MyClass b = myChache.get(key).getValue();
ut.commit();
return a.equals(b);
Let's assume MyClass
has a member of the type MyOtherClass
and that changeSomeInnerReferrence
changes the reference from the current value to the parameter; Also assume that equals takes that member into consideration.
I noticed that unless I add myChache.put(key,a)
before the ut.commit()
the above code will return false
.
Why is that? Is this the general behavior of caches? I would think that changing an inner reference would propagate into the cache once commit is called.
Thanks,
Ittai
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里先说一下,我没有在 JTA 的上下文中使用过 EHCache。它有可能在用户事务中做了一些聪明的事情,但我有点怀疑。
一般规则是cache.get(key)返回的元素是按值返回的。对它的更改不一定反映在底层缓存中。如果您想象根本没有内存存储,而只有磁盘存储,那么其背后的原因就变得非常清楚了。磁盘存储需要序列化缓存条目,因此一对 put/get 操作将返回一个不同的 Java 实例。此外,在这种情况下,不清楚何时对 cache.get() 返回的实例进行任何更改将/应该写回磁盘。使用 put() 可以清楚地表明这一点。
最后,你从 get() 得到的东西就是你的责任。你可以通过 put() 来告诉 EHCache 接管。
Bit of a preface here, I haven't used EHCache in the context of JTA. It is possible it does something clever within a user transaction, but I kinda doubt it.
The general rule is that the element returned by cache.get(key) is by value. Changes to it aren't necessarily reflected in the underlying cache. The reasoning behind it becomes pretty clear if you imagine not having an in-memory store at all, but only a disk store. The disk store requires serializing the cache entries, so a put/get pair of operations will return you a different Java instance. Further, in such a situation it's not clear when any changes to the instance returned by cache.get() would/should get written back to disk. Using a put() makes that clear.
In the end, the thing you get from get() is your responsibility. You tell EHCache to take over by saying put().