2016-08-27 12 views
-1

並行性制御について質問があります。列更新後のJavaオブジェクトの更新

ここにシナリオがあります。

  1. Java API /スレッドは、データベースからエンティティ/行を読み取ります。
  2. 別のスレッドが同じ行を更新します。
  3. ここで、手順1から読み取ったオブジェクト/行を更新するにはどうしたらいいですか?
+0

こんにちはSuman;あなたの質問を少しはっきりさせることができますか? [Concurrency control](https://en.wikipedia.org/wiki/Concurrency_control)について心配しているようですが、ステップ3で何をしたいのか分かりません。 2番目のスレッドの更新を上書きするにもかかわらず、行を更新できますか?これは失われた更新問題と呼ばれ、古典的な並行性制御の問題です。 –

+0

質問の文章や書式を微調整して読みやすくしました。 –

答えて

1

分離レベルを設定する必要があります。下記の例が役立つ場合があります。 たとえば、3つの同時プロセスA、B、Cがあります。Aはトランザクションを開始し、結果をもとにデータとコミット/ロールバックを書き込みます。 Bは単にSELECT文を実行してデータを読み込みます。 Cはデータを読み取り、更新します。これらのプロセスはすべて同じテーブルで動作します。

READ UNCOMMITTED - no lock on table. You can read data in the table while writing on it. This means, A writes data (uncommited) and B can read this uncommited data and use it (for any purpose). If A executes a rollback, B still has read the data and used it. This is the fastest but most insecure way to work with data since can lead to data holes in not physically related tables (yes, two tables can be logically but not physically related in real world apps =\). 
READ COMMITTED - lock on committed data. You can read the data that was only commited. This means, A writes data and B can't read the data saved by A until A executes a commit. The problem here is that C can update data that was read and used on B and B client won't have the updated data. 
REPEATABLE READ - lock on block of sql(which is selected by using select query). This means, B reads the data under some condition i.e. WHERE aField > 10 AND aField < 20, A inserts data where aField value is between 10 and 20, then B reads the data again and get a different result. 
SERIALIZABLE - lock on full table(on which Select query is fired). This means, B reads the data and no other transaction can modify the data on the table. This is the most secure but slowest way to work with data. Also, since a simple read operation locks the table, this can lead to heavy problems on production: imagine that T table is an Invoice table, user X wants to know the invoices of the day and user Y wants to create a new invoice, so while X executes the read of the invoices, Y can't add a new invoice (and when it's about money, people get really mad, specially the bosses). 

希望します。

関連する問題