Spring フレームワークで mySQL のデータにアクセスして更新する際に問題が発生しました。TABLE 行をロックする最も効率的な方法を皆さんに尋ねたいと思います。
2 つのテーブルがあるとします。1 つはすべての円を ID と位置とともに記録し、もう 1 つはすべての正方形を同じ列で記録します。各円の位置は個別に変更できます。正方形も同様です。ただし、円の位置を移動することで、正方形の位置を調整することもできます。次のような擬似コード:
Public ShapeMovingService{
@Transaction (isolation=required)
public moveCirclePosition(int id, int newPosition){
//move circle=id to a new position
//also move the square which relates to this circle accordingly
}
@Transaction (isolation=required)
public moveSquarePosition(int id, int newPosition){
//move square=id to a new position
}
}
public CircleDao extends JdbcTemplateSupport{
public updatePosition(int id, int position){
//query a circle from circle TABLE with id
//update the position of the circle
//ALSO: modify the position of the square which relates to this circle
}
}
public SquareDao extends JdbcTemplateSupport{
public updatePosition(int id, int position){
//query a square from squareTABLE with id
//update the position of the square
}
}
次のタスクを実行するためにいくつかのスレッドを作成しました。
- 2 つのスレッドが円 id=1 の位置を更新し続けます
- 1 つのスレッドが円 id=2 の位置を更新し続けます
- 1 つのスレッドが正方形 id=1 の位置を更新し続けます
- 1 つのスレッドが正方形 id=2 の位置を更新し続けます
id=2 の円の位置を移動すると、id=1 の正方形の位置に影響します。円 id=1 は他の正方形と関係がなく、正方形 id=2 も関係ありません。
私の質問は、データの破損が発生しないように、トランザクション注釈または同期キーワードのいずれかによって、データベース操作をどの時点でロックする必要があるかということです。それでも、異なるサークルを同時に更新することができます。現在 updatePosition 関数をロックしていますが、これは一度に 1 つの円/正方形しか更新できないことを意味します。
ご提案ありがとうございます。