Java:Hibernate在数据库中看不到更改 [英] Java: Hibernate does not see changes in DataBase

查看:80
本文介绍了Java:Hibernate在数据库中看不到更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个共享相同数据库的不同应用程序.问题是,当我有一个应用程序更改数据库中的某些内容时,另一个不会更新.

I have two different applications that share the same database. The problem is that when I have an application change something in the database, the other does not update.

我试图制作一个session.flush(),但是没有用.唯一的方法是关闭整个会话并重新创建它,但这当然会花费太长时间.

I tried to make a session.flush() but it didn't work. The only way is to close the entire session and recreate it, but of course, that takes too long.

推荐答案

简短答案:每次要显示某些对象时,请发出session.refresh(obj).它将迫使Hibernate进入数据库.另一个解决方案是使用StatelessSession,它不会缓存任何内容(甚至不缓存一级缓存),从而在每次需要记录时强制您的应用程序进入数据库:

Short answer: issue a session.refresh(obj) every time you want to display some object. It will force Hibernate to go to the database. Another solution is to use a StatelessSession, which won't cache anything (not even 1st level cache), forcing your applications to go the database every time a record is needed:

http://docs. jboss.org/hibernate/core/3.3/reference/en/html/batch.html#batch-statelesssession

但是,当然,如果太多,那么您可以考虑使用某种锁定(悲观或乐观):

But of course, if that's too much, then you can consider using some sort of locking (pessimistic or optimistic):

http://docs. jboss.org/hibernate/core/3.3/reference/en/html/transactions.html#transactions-optimistic

但是,实际上,如果您有两个使用同一记录的不同并发系统,则Hibernate本身无法解决任何问题.在系统的体系结构中应该考虑这一点.

But really, if you have two different concurrent systems using the same record, there's nothing that Hibernate can solve by itself. It is something that you should consider in the architecture of your systems.

这篇关于Java:Hibernate在数据库中看不到更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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