1 /**2 * Generate a sequence of the table based on the table name3 * @paramTableName4 * @returnreturns the generated sequence5 */6 //Global Method-Locking7 //public static synchronized int generate (String tableName) {8 //local member Method-locking9 //public synchronized int generate (String tableName) {Ten //synchronized (this) { One Public Static intGenerate (String tableName) { A //pessimistic lock for update using database -String sql = "Select value from t_table_id where Table_name=?" For Update "; -Connection conn =NULL; thePreparedStatement pstmt =NULL; -ResultSet rs =NULL; - intValue = 0; - Try { +conn =dbutil.getconnection (); - //Start a transaction + dbutil.begintransaction (conn); APSTMT =conn.preparestatement (SQL); atPstmt.setstring (1, tableName); -rs =pstmt.executequery (); - if(!Rs.next ()) { - Throw Newruntimeexception (); - } -Value = Rs.getint ("Value"); invalue++;//self-added - Modifyvaluefield (conn, tableName, value); to //Commit a transaction + dbutil.committransaction (conn); -}Catch(Exception e) { the e.printstacktrace (); * //rolling back a transaction $ dbutil.rollbacktransaction (conn);Panax Notoginseng Throw Newruntimeexception (); -}finally { the Dbutil.close (RS); + Dbutil.close (pstmt); ADbutil.resetconnection (conn);//Resetting The state of the connection the Dbutil.close (conn); + } - returnvalue; $}
Summary: Constantly improve their programming efficiency!
"DRP" "SQL"-pessimistic lock-prevents dirty data when multiple users are simultaneously operating