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