0
我使用oracle作爲我的數據庫服務器。我有一個簡單的SQL表,它存儲每個成員的代碼。我想從表中刪除代碼,但也得到它的價值。選擇並刪除事務中的記錄sql
SQL> describe member_code_store;
Name Null? Type
----------------------------------------- -------- ----------------------------
MEMBER NOT NULL NUMBER(38)
CODE NOT NULL VARCHAR2(30)
所以,我想在一個事務中
PreparedStatement pstmt = null;
ResultSet rs = null;
String query =
"SELECT code FROM member_code_store where coupon=? AND rownum=1";
Connection connection = DBConnection.getConnection();
pstmt = connection.prepareStatement(query);
pstmt.setString(1, String.valueOf(3));
rs = pstmt.executeQuery();
rs.next();
String code = rs.getString(1);
刪除代碼現在
String query =
"delete from member_code_store where coupon =? AND code=?;";
Connection connection = DBConnection.getConnection();
pstmt = connection.prepareStatement(query);
pstmt.setString(1, String.valueOf(3));
pstmt.setString(2, code);
rs = pstmt.executeUpdate();
問題與上面的代碼下面的查詢運行是多個工人移除代碼將得到相同的代碼。我如何附加事務,以便鎖定記錄而不是鎖定整個表。
或者我應該使用效率更高的程序還是程序包?
Oracle和SQL服務器使用不同的鎖定機制。對於oracle,請參閱 - https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:927629362932 – OldProgrammer