且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL:在一个简单表上并发更新(通过线程)

更新时间:2023-01-30 15:11:46

锁定是隐式的,是的,但是executeUpdate()并未完成. MySQL中的存储引擎处理锁定和解锁.

The locking is implicit, yes, but it's not being done by executeUpdate(). The storage engine in MySQL handles the locking and the unlocking.

每次写入MyISAM表时,查询都会等待表上的写锁可用,获取写锁,完成写操作,然后释放写锁. MyISAM中没有真正的写并发,因为实际上每个工作程序都在排队等待写锁定.您不会收到错误,因为写请求已序列化.

Any time you write to a MyISAM table, your query waits the write lock on the table to be available, the write lock is acquired, the write is done, and the write lock is released. There is no genuine write concurrency in MyISAM because each worker is in fact waiting in line for the write lock. You don't get an error because the write requests are serialized.

InnoDB的情况类似,但有很大不同,因为InnoDB仅锁定表的一部分,通常在行级别,其中InnoDB可以锁定索引中的范围,从而将行锁定在索引中的该范围(以及它们之间的空白).这种锁定比表锁定更精细,可以改善并发行为,但是同一行上没有并发操作-每个工作线程都在等待它需要的一个或多个锁定.

The situation with InnoDB is similar but very different, in that InnoDB only locks a portion of the table, typically at the row level, where InnoDB can lock a range within an index, thereby locking the rows at that range in the index (and the gap that precedes them). This locking is more granular than table locking, allowing improved concurrency behavior, but there is no concurrent operation on the same row -- each worker waits for the lock or locks that it needs.

在两种情况下,锁都是隐式获取的.

In both cases, the locks are implicitly taken.