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