1 | do {↵ | | 1 | do {↵
|
2 | // The loop ensures atomicity of the↵ | | |
|
3 | // select + update even for no transaction↵ | | |
|
4 | // or read committed isolation level↵ | | |
|
|
5 | //sql = query;↵ | | 2 |
↵
|
6 | SQL_STATEMENT_LOGGER.logStatement( sql, FormatStyle.BASIC );↵ | | 3 | SQL_STATEMENT_LOGGER.logStatement( selectQuery, FormatStyle.BASIC );
↵
|
7 | PreparedStatement qps = conn.prepareStatement(query);↵ | | 4 | PreparedStatement selectPS = conn.prepareStatement( selectQuery );↵
|
8 | PreparedStatement ips = null;↵ | | |
|
9 | try {↵ | | |
|
10 | //qps.setString(1, key);↵ | | |
|
| | | 5 | try {
↵
|
11 | ResultSet rs = qps.executeQuery();↵ | | 6 | ResultSet selectRS = selectPS.executeQuery();
↵
|
12 | boolean isInitialized = rs.next();↵ | | 7 | ↵
|
13 | if ( !isInitialized ) {↵ | | 8 | if ( !selectRS.next() ) {
↵
|
14 | result = 0;↵ | | 9 | ↵
|
15 | ips = conn.prepareStatement(insert);↵ | | |
|
16 | //ips.setString(1, key↵ | | 10 | String err = "could not read a hi value - you need to populate the table: " + tableName;↵
|
17 | );↵ | | 11 | log.error( err );
↵
|
18 | ips.setInt(1, result);↵ | | 12 | ↵
|
19 | ips.execute();↵ | | 13 | throw new IdentifierGenerationException( err );
↵
|
20 | }↵ | | 14 | }
↵
|
21 | else {↵ | | 15 | ↵
|
22 | result = rs.getInt(1);↵ | | 16 | result = selectRS.getLong( 1 );
↵
|
23 | }↵ | | 17 | ↵
|
24 | rs.close();↵ | | 18 | selectRS.close();
↵
|
25 | }↵ | | 19 | }
↵
|
26 | catch (SQLException sqle) {↵ | | 20 | catch ( SQLException sqle ) {
↵
|
27 | log.error("could not read or init a hi value", sqle);↵ | | 21 | log.error( "could not read a hi value", sqle );
↵
|
28 | throw sqle;↵ | | 22 | throw sqle;
↵
|
29 | }↵ | | 23 | }
↵
|
30 | finally {↵ | | 24 | finally {
↵
|
31 | if (ips != null) {↵ | | 25 | ↵
|
32 | ips.close();↵ | | 26 | selectPS.close();
↵
|
33 | }↵ | | 27 | ↵
|
34 | qps.close();↵ | | |
|
35 | }↵ | | |
|
|
36 | //sql = update;↵ | | 28 | }↵
|
|
| | | 29 | SQL_STATEMENT_LOGGER.logStatement( updateQuery, FormatStyle.BASIC );
↵
|
37 | PreparedStatement ups = conn.prepareStatement(update);↵ | | 30 | PreparedStatement updatePS = conn.prepareStatement( updateQuery );
↵
|
38 | try {↵ | | 31 | try {
↵
|
39 | ups.setInt( 1, result + 1 );↵ | | 32 | int increment = applyIncrementSizeToSourceValues ? incrementSize : 1;
↵
|
40 | ups.setInt( 2, result );↵ | | 33 | updatePS.setLong( 1, result + increment );
↵
|
41 | //ups.setString( 3, key );↵ | | 34 | updatePS.setLong( 2, result );
↵
|
42 | rows = ups.executeUpdate();↵ | | 35 | rows = updatePS.executeUpdate();
↵
|
43 | }↵ | | 36 | }
↵
|
44 | catch (SQLException sqle) {↵ | | 37 | catch ( SQLException sqle ) {
↵
|
45 | log.error("could not update hi value in: " + tableName, sqle);↵ | | 38 | log.error( "could not updateQuery hi value in: " + tableName, sqle );
↵
|
46 | throw sqle;↵ | | 39 | throw sqle;
↵
|
47 | }↵ | | 40 | }
↵
|
48 | finally {↵ | | 41 | finally {
↵
|
49 | ups.close();↵ | | 42 | updatePS.close();
↵
|
50 | }↵ | | 43 | }
↵
|
51 | }↵ | | 44 | }↵
|
52 | while (rows==0); | | 45 | while ( rows == 0 );
|