java web应用程序缓存数据,如何停止它以使数据不陈旧!
嗨,我遇到了问题。我的(我猜测)持久层正在缓存我的结果,因此当我从应用程序外部更新数据库时,数据仍然陈旧。
但是,它只缓存一半的时间,我希望这是有道理的,
我现在有一个类
@Entity
@Table(name = "PROVINCE")
public class Province implements Serializable {
@Id
@GeneratedValue
int id;
String provinceName;
String provinceMoto;
Buildings buildings;
Units units;
Person person;
.... etc
,如果我更新任何不是对类对象的引用的数据,例如“省名”,那么数据就可以了我的申请并立即更新。然而,像“建筑物”这样的内部对象却没有,而且我不知道如何做到这一点,如果我进行硬刷新(例如重新部署我的应用程序),那么数据就是新鲜的。
我从这里的数据库中获取我的省份:
Query query = manager.createQuery("select p from Province p where p.person = :query");
query.setHint("toplink.refresh", "true");
query.setParameter("query", p);
province = (Province) query.getSingleResult();
那么我该如何强制它也使该类的内部对象(即“建筑物”“人”)也更新。
我的持久层是 toplink 的必需品,我修复了之前的问题,即没有数据更新。第一篇文章这里(这是另一个堆栈溢出页面)
感谢您的帮助。我希望我能很好地解释我的问题
[编辑:就像如果我没有使用这个框架一样,连接数据库中的表并在新变量中重新填充数据会更容易,但我没有]
HI, I am having a problem. My (i am guessing) persistence layer is caching my results, so when i update the database from outside my application then the data is remaining stale.
however, it is only caching half the time, i hope this makes sense,
I have a class
@Entity
@Table(name = "PROVINCE")
public class Province implements Serializable {
@Id
@GeneratedValue
int id;
String provinceName;
String provinceMoto;
Buildings buildings;
Units units;
Person person;
.... etc
now, if i update any data that isn't an a reference to a class object, like the 'provincename' then the data is fine in my application and updates straight away. however, the inner objects like 'buildings' doesn't and i cannot figure out how to, if i do a hard refresh like redeploying my app, then the data is fresh.
i get my province from the database here:
Query query = manager.createQuery("select p from Province p where p.person = :query");
query.setHint("toplink.refresh", "true");
query.setParameter("query", p);
province = (Province) query.getSingleResult();
so how do i go about forcing that to also make the inner objects of that class, i.e. the 'buildings' 'person' to also update.
my persistence layer is toplink essentials, and i fixed my earlier problem which was no data updating. and that first post here(it's another stack overflow page)
thank yous for any helps. i hope i explained my problem well enough
[EDIT: like if i wasnt using this framework, it'd just be easier to join the tables in the db and repopulate the data in a new variable, but im not]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个“医生,当我……时很痛”“不要那样做”的问题。
如果您要在 Toplink 后面更新数据库,那么您必须像以前一样刷新数据。
每次在 JPA 中使用“选择”类型查询时,它都会访问数据库。但是,正如您所知,对于相关对象,它只会从数据库中提取它们的密钥,然后优先使用缓存版本而不是访问数据库。
Toplink 有 2 个缓存。 1 级和2 级缓存。 1 级缓存是当前会话使用的基于事务的缓存。 2 级缓存类似,但适用范围广泛且是全局的。 2 级缓存是您最有可能遇到的。
因此,您要么需要继续使用 Toplink 刷新提示,要么完全禁用 2 级缓存。
This is a "Doc it hurts when I..." "Don't do that" question.
If you're updating the database behind the back of Toplink, then you have to refresh your data just like you did.
Every time you use a "select" type query in JPA, it WILL hit the database. But, as you've learned, for related objects, it will simply pull their keys from the DB, and then use the cached version in preference to hitting the database.
Toplink has 2 caches. The Level 1 and Level 2 cache. The Level 1 cache is a transaction based cache use for the current session. The Level 2 cache is similar, but application wide and global in scope. The Level 2 cache is what you are most likely bumping in to.
So you will either need to continue using the Toplink refresh hint as you are doing, or disable the Level 2 Cache completely.
将数据更新到数据库后,使您的值对象(也是子值对象)或表单使用默认值重新初始化。希望这能解决您的问题。
After you update the data to DB, make your value object (also child value objects) or form reintialise with defeault values. Hope this will slove your problem.