zoukankan      html  css  js  c++  java
  • Java 和 数据库两种方式进行加锁

    java方式:

    publicstatic synchronized int generate(StringtableName){  
      Stringsql = "select value from t_table_id where table_name=?";  
      Connectionconn = null;  
      PreparedStatementpstmt = null;  
      ResultSetrs = null;  
      intvalue = 0;  
      try{  
        conn= DbUtil.getConnection();  
        pstmt= conn.prepareStatement(sql);  
        pstmt.setString(1,tableName);  
        rs= pstmt.executeQuery();  
        rs.next();  
    //                        if(!rs.next()){  
    //                                thrownew RuntimeException();  
    //                        }  
      value= rs.getInt("value");  
      value++;  
      modifyValueField(conn,tableName,value);  
    }catch(Exceptione){  
      e.printStackTrace();  
      thrownew RuntimeException();  
    }finally{  
        DbUtil.close(rs);  
        DbUtil.close(pstmt);  
        DbUtil.close(conn);  
    }  
      returnvalue;  
    }  

    数据库的方式:

    //采用悲观锁来实现同步  
    //在sql语句后加 for update就加上了锁,在查询的时候进行加锁,在加锁后不能进行查询。提交时候后其他人才能查询。  
    public static int generate(String tableName){  
            //使用数据库的悲观锁for update  
            String sql = "select value from t_table_id where table_name=? for update";  
            Connection conn = null;  
            PreparedStatement pstmt = null;  
            ResultSet rs = null;  
            int value = 0;  
            try{  
                conn = DbUtil.getConnection();  
                //设置自动提交为false  
                DbUtil.beginTransaction(conn);  
                pstmt = conn.prepareStatement(sql);  
                pstmt.setString(1, tableName);  
                rs = pstmt.executeQuery();  
                rs.next();  
    //          if(!rs.next()){  
    //              throw new RuntimeException();  
    //          }  
                value = rs.getInt("value");  
                value++;  
                modifyValueField(conn,tableName,value);  
                //提交事务  
                DbUtil.commitTransaction(conn);  
            }catch(Exception e){  
                e.printStackTrace();  
                //回滚事务  
                DbUtil.rollbackTranscation(conn);  
                throw new RuntimeException();  
            }finally{  
                DbUtil.close(rs);  
                DbUtil.close(pstmt);  
                DbUtil.resetConnection(conn);  
                DbUtil.close(conn);  
            }  
            return value;  
        }  
  • 相关阅读:
    I NEED A OFFER!
    水题 Codeforces Round #303 (Div. 2) A. Toy Cars
    模拟 HDOJ 5099 Comparison of Android versions
    模拟 HDOJ 5095 Linearization of the kernel functions in SVM
    贪心 HDOJ 5090 Game with Pearls
    Kruskal HDOJ 1863 畅通工程
    Kruskal HDOJ 1233 还是畅通工程
    并查集 HDOJ 1232 畅通工程
    DFS/并查集 Codeforces Round #286 (Div. 2) B
    水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/7044832.html
Copyright © 2011-2022 走看看