Bonjour à tous,

première fois que j'utilise un Batch via un PreparedStatement pour mettre à jour ou insérer des lignes dans un base oracle.

Il y a quelque chose que je ne comprends pas, le tableau d'entier, retourné lorsque j'exécute le executeBatch, contient uniquement des valeurs -2 alors que les lignes sont mises à jour. Il devrait normalement me retourner la valeur 1.

En fait je dois effectuer une vérification sur le nombre de lignes mises à jour et lever des exception en fonction de ce paramètre.

Voici ma méthode utilisée:

Code java : Sélectionner tout - Visualiser dans une fenêtre à part
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    public static boolean activateSecondStageProposals(Connection connection,
                                                      Vector<String> proposals,
                                                      int callID,
                                                      int callType)
        throws adminDBManagerException
    {
        PreparedStatement selectStatement = null;
        PreparedStatement updateStatement = null;
        ResultSet rs = null;
        int step = 0;
        String stepStr = "";
        boolean success = true;
 
        try
        {
            if (callType == 2) // Two stage call
            {
                //Lock all the accounts
                updateStatement =
                        connection.prepareStatement("UPDATE EPSS_REGISTRATION SET STATUS = 3 WHERE CALL_ID = ?");
                updateStatement.setInt(1, callID);
                updateStatement.executeUpdate();
 
                //Set accepted proposals to unsubmitted
                step = 1;
                updateStatement =
                        connection.prepareStatement("UPDATE EPSS_PROPOSAL SET IS_SUBMITTED = 0 WHERE CALL_ID = ? AND PROPOSAL_NUMBER = ? AND IS_SUBMITTED = 4");
                for (int i = 0; i < proposals.size(); i++)
                {
                    updateStatement.setInt(1, callID);
                    updateStatement.setInt(2,
                                           Integer.parseInt((proposals.get(i)).trim()));
                    updateStatement.addBatch();
                }
                int[] count = updateStatement.executeBatch();
                stepStr = "\"Set IS_SUBMITTED = 0\":";
                for(int i = 0; i < count.length; i++)
                {
                    if(count[i] == 0)
                    {
                        connection.rollback();
                        success = false;
                        throw new adminDBManagerException(stepStr + " No row updated for proposal number: " +
                                            Integer.parseInt((proposals.get(i)).trim()));
                    }
                    else if(count[i] > 1)
                    {
                        connection.rollback();
                        success = false;
                        throw new adminDBManagerException(stepStr +
                                                          " More than one row updated for proposal number: " +
                                                          Integer.parseInt((proposals.get(i)).trim()));
                    }                    
                }
                // Reopen accepted proposals
                step = 2;
                updateStatement =
                        connection.prepareStatement("UPDATE EPSS_REGISTRATION SET STATUS = 2 WHERE STATUS = 3 AND PROPOSAL_ID = ( SELECT PROPOSAL_ID FROM EPSS_PROPOSAL WHERE CALL_ID = ? AND PROPOSAL_NUMBER = ?)");
                for (int i = 0; i < proposals.size(); i++)
                {
                    updateStatement.setInt(1, callID);
                    updateStatement.setInt(2,
                                           Integer.parseInt((proposals.get(i)).trim()));
                    updateStatement.addBatch();
                }
                count = updateStatement.executeBatch();
                stepStr = "\"Set STATUS = 2\":";
                for (int i = 0; i < count.length; i++)
                {
                    if (count[i] == 0)
                    {
                        connection.rollback();
                        success = false;
                        throw new adminDBManagerException(stepStr +
                                                          " No row updated for proposal number: " +
                                                          Integer.parseInt((proposals.get(i)).trim()));
                    }
                    else if (count[i] > 1)
                    {
                        connection.rollback();
                        success = false;
                        throw new adminDBManagerException(stepStr +
                                                          " More than one row updated for proposal number: " +
                                                          Integer.parseInt((proposals.get(i)).trim()));
                    }
                }
                //Set call to second stage
                updateStatement =
                        connection.prepareStatement("UPDATE EPSS_CALL SET STAGE_TYPE = 2 WHERE CALL_ID = ?");
                updateStatement.executeUpdate();
            }
            else //Continuous two stages
            {
                // Reopen accepted proposals
                updateStatement =
                        connection.prepareStatement("UPDATE EPSS_PROPOSAL SET IS_SUBMITTED = 0 WHERE CALL_ID = ? AND PROPOSAL_NUMBER = ? AND IS_SUBMITTED = 4");
                for (int i = 0; i < proposals.size(); i++)
                {
                    updateStatement.setInt(1, callID);
                    updateStatement.setInt(2,
                                           Integer.parseInt((proposals.get(i)).trim()));
                    updateStatement.addBatch();
                }
                int[] count = updateStatement.executeBatch();
                System.out.println("Number of Rows in count: "+count.length);
                stepStr = "\"Set IS_SUBMITTED = 0\":";
                for (int i = 0; i < count.length; i++)
                {
                    System.out.println("Counter value in batch: "+count[i]);
                    if (count[i] == 0)
                    {
                        connection.rollback();
                        success = false;
                        throw new adminDBManagerException(stepStr +
                                                          " No row updated for proposal number: " +
                                                          Integer.parseInt((proposals.get(i)).trim()));
                    }
                    else if (count[i] > 1)
                    {
                        connection.rollback();
                        success = false;
                        throw new adminDBManagerException(stepStr +
                                                          " More than one row updated for proposal number: " +
                                                          Integer.parseInt((proposals.get(i)).trim()));
                    }
                }
                //add accepted proposals in table EPSS_SECOND_STAGE_APPROVED
                updateStatement =
                        connection.prepareStatement("INSERT INTO EPSS_SECOND_STAGE_APPROVED (PROPOSAL_NUMBER) VALUES(?)");
                for (int i = 0; i < proposals.size(); i++)
                {
                    selectStatement =
                            connection.prepareStatement("SELECT COUNT(*) FROM EPSS_SECOND_STAGE_APPROVED WHERE PROPOSAL_NUMBER = ?");
                    selectStatement.setInt(1,
                                           Integer.parseInt((proposals.get(i)).trim()));
                    rs = selectStatement.executeQuery();
                    rs.next();
                    if (rs.getInt(1) == 0)
                    {
                        updateStatement.setInt(1,
                                               Integer.parseInt((proposals.get(i)).trim()));
                        updateStatement.addBatch();
                    }
 
                }
                int[] batch = updateStatement.executeBatch();
            }
 
        }
        catch(BatchUpdateException bue)
        {
            bue.printStackTrace();
            try
            {
                connection.rollback();
                success = false;
            }
            catch (Exception xe)
            {
            }
            int counts[] = bue.getUpdateCounts();
        }
        catch (Exception e)
        {
            try
            {
                connection.rollback();
                success = false;
            }
            catch (Exception xe)
            {
            }
            System.out.println("Exception in activateSecondStageProposals");
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new adminDBManagerException(e);
        }
        finally
        {
            try
            {
                rs.close();
                updateStatement.close();
                selectStatement.close();
            }
            catch (Exception e)
            {
            }
        }
 
        return success;
 
    }

Si rien ne cloche dans mon traitement batch, je vais devoir trouver une autre solution pour effecvtuer mes traitements et vérifications (d'abord un select pour être sur que la ligne existe et ensuite un update sinon exception levée)

If select count(*) --> retourne 1 alors "update" sinon "exception levée".

Merci pour votre aide.