"DRP" "SQL"-pessimistic lock-prevents dirty data when multiple users are simultaneously operating

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.