多线程环境中的 ACID 问题
我有一个多线程 java 应用程序,它在 MySQL 数据库上执行大量并行 CRUD 操作。正如MySQL手册中所读到的,InnoDB表结构应该确保事务按照ACID原则执行。但我仍然遇到问题,因为有时更新会丢失。这是我使用的示例存储过程之一:
DELIMITER //
CREATE PROCEDURE *** (
_*** INT,
_*** INT,
_*** INT,
_*** INT
)
BEGIN
START TRANSACTION;
UPDATE `***`
SET
`***`.`***` = `***`.`***` + _***,
`***`.`***` = `***`.`***` + _*** + _***,
`***`.`***` = DATE_ADD(NOW(), INTERVAL _*** SECOND)
WHERE `***`.`***` = _***;
COMMIT;
END;
//
DELIMITER ;
I have a multi-threaded java application which executes a lot of parallel CRUD operations on an MySQL Database. As read in MySQL manuals, InnoDB table structure should ensure, that the transactions are executed following the ACID priciples. But I still have problems, because sometimes, the Updates get lost. This is one of the example Stored Procedures that I use:
DELIMITER //
CREATE PROCEDURE *** (
_*** INT,
_*** INT,
_*** INT,
_*** INT
)
BEGIN
START TRANSACTION;
UPDATE `***`
SET
`***`.`***` = `***`.`***` + _***,
`***`.`***` = `***`.`***` + _*** + _***,
`***`.`***` = DATE_ADD(NOW(), INTERVAL _*** SECOND)
WHERE `***`.`***` = _***;
COMMIT;
END;
//
DELIMITER ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JDBC 类是不可重入的,因此您必须同步使用连接、语句等对象实例。它们不能被多个线程同时使用。
使用连接池是简化数据库并发访问的好方法。
编辑
如果您确定线程从不使用相同的连接,您可以检查
The JDBC classes are not reentrant, so you have to synchronise use of object instances of connection, statement, etc. They cannot savely be used by multiple threads concurrently.
Use of a connection pool is a good way to streamline concurrent access to a database.
Edit
If you're sure that the threads never use the same connection, you could check