1

複数のプリペアドステートメントがあります。すべてのステートメントに対して「connection.commit」を実行する必要がありますか、それとも最後のステートメントの後に1回だけ呼び出す必要がありますか?

Connection connection = datasource.getConnect();
connection.setAutoCommit(false);
PreparedStatement pstmt1 = connection.prepareStatement(query1);
pstmt1.executeUpdate();
connection.commit();

PreparedStatement pstmt2 = connection.prepareStatement(query2);
pstmt2.executeUpdate();
connection.commit();

PreparedStatement pstmt3 = connection.prepareStatement(query3);
pstmt3.executeUpdate();
connection.commit();


Or

Connection connection = datasource.getConnect();
connection.setAutoCommit(false);
PreparedStatement pstmt1 = connection.prepareStatement(query1);
pstmt1.executeUpdate();

PreparedStatement pstmt2 = connection.prepareStatement(query2);
pstmt2.executeUpdate();

PreparedStatement pstmt3 = connection.prepareStatement(query3);
pstmt3.executeUpdate();

connection.commit();

ありがとう。

4

1 に答える 1

5

を設定autocommit = falseすると、トランザクションをコミットするタイミングを選択できます。それが最善の場合、原子性、トランザクション分離などに依存するため、判断できるのはあなただけです。

これが、autoCommitプロパティを「エンドユーザー」に公開することの全体的なポイントであるため、変更が相互にどのように関連するか、または依存するかを制御できます。

あなたができないこと(私はかなり確信しています-私がJDBCで直接プログラムしてからしばらく経ちました)は、間に他のコマンドなしで2つのコミットコマンドを発行することです。

于 2012-10-01T15:21:34.957 に答える