Pessimistic lock--"DRP"

Source: Internet
Author: User

The left side of the re-input pessimistic lock statement, you can not query out the data.

Pessimistic Lock:

The whole process of data processing, the information on the external changes are conservative attitude. The entire data processing process is in a locked state. The record cannot be modified by the outside world until this transaction is committed. Embodies the exclusivity of the database.

Optimistic Lock:

Implemented based on the version ID. When reading data, the version number is read, modified, and its version number is added 1. When committing, the version data of the submitted data is compared with the version number in the database, which is larger than the version in the database and is submitted.

So the source code of the statement:

/** *  */ PackageCom.bjpowernode.drp.util;Importjava.awt.image.ConvolveOp;Importjava.sql.Connection;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;/*** @ClassName: Idgenerator * @Description: ID Generator *@authorWM * @date January 15, 2016 PM 6:41:06*/ Public classIdgenerator {/*** Generate a sequence of the table based on the table name *@paramTableName *@returnreturns the generated sequence * using synchronized to implement thread synchronization issues*/    //Lock Thread 1//public static synchronized int generate (String tableName) {     Public  intGenerate (String tableName) {//Todo:id Generator//Lock Thread 2        /*synchronized (this) {}*/        //pessimistic lock of the databaseString sql= "Select value from t_table_id where Table_name=? For Update "; Connection Conn=NULL; PreparedStatement pstmt=NULL; ResultSet RS=NULL; intValue=0; Try{conn=dbutil.getconnection (); Pstmt=conn.preparestatement (SQL); Pstmt.setstring (1, TableName); RS=Pstmt.executequery (); if(!Rs.next ()) {                Throw Newruntimeexception (); } Value=rs.getint ("Value"); Value++;        Modifyvaluefield (Conn,tablename,value); } Catch(Exception e) {e.printstacktrace (); Throw Newruntimeexception (); }finally{dbutil.close (RS);            Dbutil.close (PSTMT);        Dbutil.close (conn); }                returnvalue; }        /*** Update the value of the sequence field according to the table name *@paramConn *@paramTableName *@paramValue *@throwsSQLException*/    Private Static  voidModifyvaluefield (Connection conn,string tableName,intValuethrowssqlexception{String SQL= "Update t_table_id set value=?" where Table_name=? "; PreparedStatement pstmt=NULL; Try{pstmt=conn.preparestatement (SQL); Pstmt.setint (1, value); Pstmt.setstring (2, TableName);        Pstmt.executeupdate (); } finally{dbutil.close (pstmt); }    }             Public Static voidMain (string[] args) {//Complete the test        intRetvalue=idgenerator.generate ("T_client");    System.out.println (RetValue); }}

Pessimistic lock--"DRP"

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.