“选择...进行更新”不适用于 Hibernate 和 MySQL

发布于 2024-08-21 12:45:05 字数 410 浏览 4 评论 0原文

我们有一个系统,必须在一个实体中使用悲观锁定。我们使用的是hibernate,所以我们使用LockMode.UPGRADE。但是,它不会锁定。

  • 这些表是 InnoDB
  • 我们已经检查了锁定在数据库(5.0.32)中是否正常工作,因此这个错误 http://bugs.mysql.com/bug.php?id=18184 似乎没有问题。
  • 我们已检查数据源是否包含 autoCommit = false 参数。
  • 我们检查了 hibernate(版本 3.2)生成的 SQL 是否包含“FOR UPDATE”。

谢谢,

We have a system in which we must use pessimistic locking in one entity. We are using hibernate, so we use LockMode.UPGRADE. However, it does not lock.

  • The tables are InnoDB
  • We have checked that locking works correctly in the database (5.0.32), so this bug http://bugs.mysql.com/bug.php?id=18184 seems to be no problem.
  • We have checked that datasource includes the autoCommit = false parameter.
  • We have checked that the SQL hibernate (version 3.2) generates includes the " FOR UPDATE".

Thanks,

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

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

发布评论

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

评论(3

薆情海 2024-08-28 12:45:05

我遇到了非常相似的事情。我在 Spring 中使用 @Transactional 注释,并且发出更新选择,但未获取更新锁(我有其他线程发出相同的更新选择,并已验证它们不会阻塞一把锁)。当我显式获取 Hibernate 会话并发出 beginTransaction 并在代码块周围提交时,一切正常。

这并没有给我带来 Spring 容器管理事务的温暖和模糊的感觉。

I'm running into something very similar. I'm using the @Transactional annotation in Spring and I'm issuing a select for update and the update lock is not being acquired (I have other threads that are issuing the same select for update and have verified that they're not blocking on a lock). When I explicitly get the Hibernate session and issue a beginTransaction and commit around the code block, it all works.

This does not give me a warm and fuzzy feeling about Spring container managed transactions.

最冷一天 2024-08-28 12:45:05

当我遇到类似的问题时,那是因为我错误配置了 Spring 管理的事务。仔细检查您的 Spring tx 配置。

<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean>

您需要 autocommit = false 的事实也可能表明您没有在事务中进行操作。当您尝试访问一对多集合时,是否也会遇到延迟初始化异常?

我发现弄清楚 Spring tx 方面是否真正工作的最直接方法是使用调试器。在发出 FOR UPDATE SQL 的方法中放置一个断点。向上堆栈,直到找到您的 @Transactional 类/方法。您应该在调用堆栈中的下一个调用中看到 Spring 方面代理。

When I've had similar problems, it was because I had the Spring managed transactions misconfigured. Double check your Spring tx config.

<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean>

The fact that you need autocommit = false may also indicate that you're not operating within a transaction. Do you also get lazy initialization exceptions when you try to access one-to-many collections?

The most direct way I've found to figure out if the Spring tx aspect is actually working is to use the debugger. Put a breakpoint in the method that issues the FOR UPDATE SQL. Upstack until you hit your @Transactional class/method. You should see the Spring aspect proxy in the next call up in the call stack.

梦开始←不甜 2024-08-28 12:45:05

最近我遇到了这样的问题。我们使用 2 个 tx 管理器,因为我们有不同的数据库,问题是事务使用配置到另一个数据库的 txmanager,因此在执行选择之前,与查询数据库的连接没有禁用自动提交模式用于更新。使用正确的 txmanager 解决了这个问题。希望它将来能帮助其他人。

Lately I have had an issue like this. We where working with 2 tx managers coz we had different databases and the problem was that the transaction was using the txmanager configured to the other database and because of that on the connection to the queried database was not being disabled the autocommit mode before performing the select for update. Using the correct txmanager solved that. Hope it helps others in future.

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