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