EntityManager没有看到其他交易中进行的更改 [英] EntityManager doesn't see changes made in other transactions

查看:73
本文介绍了EntityManager没有看到其他交易中进行的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为GlassFish 2.1.1(据我所知JavaEE 5,JPA 1.0)编写一些应用程序.我的servlet中有以下代码(我主要从Internet上的某些示例借来的):

I'm writing some application for GlassFish 2.1.1 (JavaEE 5, JPA 1.0, as far as I know). I have the following code in my servlet (which I mostly borrowed from some sample on the Internet):

@PersistenceContext(name = "persistence/em", unitName = "pu")
private EntityManager em;

@Resource
private UserTransaction utx;

@Override
protected void doPost(...) {
    utx.begin();
    . . . perform retrieving operations on em . . .
    utx.rollback();
}

web.xml包含以下内容:

<persistence-context-ref>
    <persistence-context-ref-name>persistence/em</persistence-context-ref-name>
    <persistence-unit-name>pu</persistence-unit-name>
</persistence-context-ref>   

问题是,em没有看到在另一个外部事务中所做的更改.大致而言,我从Web浏览器向Servlet发出请求,查看数据,在SQL控制台中执行一些DML,重新加载Servlet页面-它没有显示任何更改.我尝试使用em.flushutx.rollbackem.joinTransaction的许多组合,但似乎没有任何用处.

The problem is, the em doesn't see changes that have been made in another, outside transaction. Roughly, I make a request to my servlet from web browser, see data, perform some DML in SQL console, reload servlet page -- and it doesn't show any change. I've tried to use many combinations of em.flush, and utx.rollback, and em.joinTransaction, but it doesn't seem to do any good.

由于我是JPA的新手,因此情况变得很复杂,因此我对底层机制的工作方式不甚了解.因此,任何帮助,以及-更重要的是-对正在发生的事情的解释/链接,将不胜感激.谢谢!

Situation is complicated by me being a total newbie in JPA, so I do not have a clear understanding of how the underlying machinery works. So any help and -- more importantly -- explanations/links of what is happening there would be very appreciated. Thanks!

推荐答案

JPA实现维护已访问实体的缓存.在不使用JPA的情况下在其他事务中执行操作时,缓存不再是最新的,因此您将永远看不到其中所做的更改.

The JPA implementation maintains a cache of entities that have been accessed. When you perform operations in a different transaction without using JPA, the cache is no longer up to date, and hence you never see the changes made in it.

如果您确实希望看到更改,则必须刷新缓存,在这种情况下,所有实体都将从缓存中逐出.当然,您需要知道何时执行此操作(在另一个事务完成之后),否则,您将继续看到模棱两可的实体.如果这是您的业务需求,那么JPA可能不适合您的问题领域.

If you do wish to see the changes, you will have to refresh the cache, in which case all entities will be evicted from the cache. Of course, you'll need to know when to do this (after the other transaction has completed), otherwise you'll continue to see ambiguous entities. If this is your business need, then JPA is possibly not a good fit to your problem domain.

相关:

  1. 默认情况下是否在jpa中缓存了实体?
  2. 使JPA EntityManager会话无效
  1. Are entities cached in jpa by default ?
  2. Invalidating JPA EntityManager session

这篇关于EntityManager没有看到其他交易中进行的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆