1
81041
2019-06-20 ab3c4acf83f54f8449ca8664c4a2bb79bd30f297
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.fgkj.db;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
 
import com.fgkj.actions.ActionUtil;
import com.fgkj.dao.DAOHelper;
 
public class Upload {
    //文件上传用到
        public static int getBattTestRecordCountNew(int bg_id, String table,Connection mysql_con)
        {
            String[] sql_strs = new String[4];
            sql_strs[0] = "SELECT test_record_count_ex FROM " + table 
                                + " WHERE BattGroupId=" + bg_id  + " FOR UPDATE";
            sql_strs[1] = "UPDATE " + table + " SET test_record_count_ex=test_record_count+1"
                                + " WHERE BattGroupId=" + bg_id;
            sql_strs[2] = "SELECT MAX(test_record_count_ex) FROM " + table 
                                + " WHERE BattGroupId=" + bg_id;
            sql_strs[3] = "INSERT INTO " + table + " (BattGroupId,test_record_count, test_record_count_ex) "
                                + " VALUES (" + bg_id + "," + 1 + "," + 1 + ")";
            return getIdNewByLock(sql_strs, 1 ,mysql_con);
        }
        
        //文件上传用到
        public static int getIdNewByLock(String[] sql_strs, int id_init,Connection mysql_con) 
        {
            
            if(sql_strs.length < 4) {
                return 0;
            }
            int count_id = 0;
            boolean res_exe = true;
            String sql_str0 = sql_strs[0];
            String sql_str1 = sql_strs[1];
            String sql_str2 = sql_strs[2];
            String sql_str3 = sql_strs[3];
            PreparedStatement ps=null;
            ResultSet res=null;
            int judge=1;
            
            try {
                mysql_con.setAutoCommit(false);
                ps= mysql_con.prepareStatement(sql_str0);
                res=ps.executeQuery();
                //ResultSet res = sqlMysqlQuery(sql_str0);
                if(res.next()) {
                    ps=mysql_con.prepareStatement(sql_str1);
                    judge=ps.executeUpdate();
                    if(0 == judge) {
                        res_exe = false;
                    }
                    ps= mysql_con.prepareStatement(sql_str2);
                    res=ps.executeQuery();
                    //res = sqlMysqlQuery(sql_str2);
                    if(res.next()) {
                        count_id = res.getInt(1);
                    }
                } else {
                    count_id = id_init;
                    ps=mysql_con.prepareStatement(sql_str3);
                    judge=ps.executeUpdate();
                    if(0 == judge) {
                        res_exe = false;
                    }
                }                
//                try {
//                    System.out.println("开始休眠30分钟");
//                    Thread.sleep(20*1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }        
//                System.out.println("休眠结束");
                if(true == res_exe) {
                    mysql_con.commit();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                res_exe = false;
            }  finally {
                if(false == res_exe) {
                    try {
                        mysql_con.rollback();
                        mysql_con.setAutoCommit(true);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }    
            return count_id;
        }
 
}