2 つのスレッドが実行されているプログラムがあり、各スレッドには独自のデータベース JDBC 接続があり、以下のように同じデータベース テーブル A にアクセス/変更します。テーブル A には 2 つの列 (id、name) しかなく、主キーは id と name の組み合わせです。
statement stmt;
// first delete it if the record has exist in table
stmt.addBatch("delete from A where id='arg_id' and name='arg_name';");
// then insert it to table
stmt.addBatch("insert into A values (arg_id, arg_name);");
stmt.executeBatch();
2 つのスレッドが同じデータをテーブルに挿入する可能性があり、次の例外が発生しました。
java.sql.BatchUpdateException: Duplicate entry '0001-joey' for key 1
at com.mysql.jdbc.Statement.executeBatch(Statement.java:708)
at com.mchange.v2.c3p0.impl.NewProxyStatement.executeBatch(NewProxyStatement.java:743)
at proc.Worker.norD(NW.java:450)
この問題を解決する方法を教えてください。ありがとうございました。
よろしく、ジョーイ