Java:Hibernate 没有看到数据库中的变化

发布于 2024-10-05 08:02:52 字数 148 浏览 5 评论 0原文

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

我尝试创建一个 session.flush() 但它不起作用。唯一的方法是关闭整个会话并重新创建它,但这当然需要很长时间。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

惟欲睡 2024-10-12 08:02:52

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

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

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

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

但实际上,如果您有两个不同的并发系统使用同一条记录,那么 Hibernate 无法自行解决任何问题。这是您在系统架构中应该考虑的事情。

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

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.

寂寞笑我太脆弱 2024-10-12 08:02:52

如果我理解正确,这就是您的场景中发生的情况:

  • app1 启动一个事务
  • app2 (启动并)提交另一个事务
  • app1 期望能够读取 app2 所做的更改

如果是这种情况,您需要检查您的数据库 隔离级别

或者,当您想要读取更新的数据时,您当然可以启动一个新事务。查看 Hibernate 事务文档< /a>

If I understand correctly this is what happens in your scenario:

  • app1 starts a transaction
  • app2 (starts and) commits another transaction
  • app1 expects to be able to read the changes done by app2

If this is the case you need to check out your database Isolation levels.

Or you could of course start a new transaction when you want to read updated data. Check out the Hibernate transactions documentation

云仙小弟 2024-10-12 08:02:52

我还没有对此进行测试,但 Session 类有一个明确的方法可以驱逐所有加载的实体。这应该强制从数据库重新加载。

I haven't tested this but the Session class has a clear method that evicts all loaded entities. This should force a reload from the db.

复古式 2024-10-12 08:02:52

对我来说,它可以提交当前事务并在其他应用程序应用其修改后开始新事务。

For me it works to commit the current transaction and begin a new one after the other application has applied its modifications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文