coverity 1116564: complicated update of sequences fixed

Signed-off-by: Daan Hoogland <daan@onecht.net>

This closes #564
This commit is contained in:
Daan Hoogland 2015-07-08 00:26:13 +02:00
parent 0cd8c06f7d
commit b0136c56e7

View File

@ -94,15 +94,14 @@ public class SequenceFetcher {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public T call() throws Exception { public T call() throws Exception {
try { StringBuilder sql = new StringBuilder("SELECT ");
PreparedStatement stmt = null; sql.append(_tg.valueColumnName()).append(" FROM ").append(_tg.table());
StringBuilder sql = new StringBuilder("SELECT "); sql.append(" WHERE ").append(_tg.pkColumnName()).append(" = ? FOR UPDATE");
sql.append(_tg.valueColumnName()).append(" FROM ").append(_tg.table());
sql.append(" WHERE ").append(_tg.pkColumnName()).append(" = ? FOR UPDATE");
TransactionLegacy txn = TransactionLegacy.open("Sequence");
PreparedStatement selectStmt = txn.prepareStatement(sql.toString()); try (TransactionLegacy txn = TransactionLegacy.open("Sequence");
PreparedStatement selectStmt = txn.prepareStatement(sql.toString());
) {
if (_key == null) { if (_key == null) {
selectStmt.setString(1, _tg.pkColumnValue()); selectStmt.setString(1, _tg.pkColumnValue());
} else { } else {
@ -113,33 +112,33 @@ public class SequenceFetcher {
sql.append(_tg.table()).append(" SET ").append(_tg.valueColumnName()).append("=").append("?+?"); sql.append(_tg.table()).append(" SET ").append(_tg.valueColumnName()).append("=").append("?+?");
sql.append(" WHERE ").append(_tg.pkColumnName()).append("=?"); sql.append(" WHERE ").append(_tg.pkColumnName()).append("=?");
PreparedStatement updateStmt = txn.prepareStatement(sql.toString()); try (PreparedStatement updateStmt = txn.prepareStatement(sql.toString());) {
if (isRandom) { if (isRandom) {
updateStmt.setInt(2, random.nextInt(10) + 1); updateStmt.setInt(2, random.nextInt(10) + 1);
} else { } else {
updateStmt.setInt(2, _tg.allocationSize()); updateStmt.setInt(2, _tg.allocationSize());
} }
if (_key == null) { if (_key == null) {
updateStmt.setString(3, _tg.pkColumnValue()); updateStmt.setString(3, _tg.pkColumnValue());
} else { } else {
updateStmt.setObject(3, _key); updateStmt.setObject(3, _key);
} }
ResultSet rs = null;
try {
txn.start(); txn.start();
stmt = selectStmt;
rs = stmt.executeQuery();
Object obj = null; Object obj = null;
while (rs.next()) { try (ResultSet rs = selectStmt.executeQuery();) {
if (_clazz.isAssignableFrom(Long.class)) {
obj = rs.getLong(1); while (rs.next()) {
} else if (_clazz.isAssignableFrom(Integer.class)) { if (_clazz.isAssignableFrom(Long.class)) {
obj = rs.getInt(1); obj = rs.getLong(1);
} else { } else if (_clazz.isAssignableFrom(Integer.class)) {
obj = rs.getObject(1); obj = rs.getInt(1);
} else {
obj = rs.getObject(1);
}
} }
} catch (SQLException e) {
s_logger.warn("Caught this exception when running: " + (selectStmt != null ? selectStmt.toString() : ""), e);
} }
if (obj == null) { if (obj == null) {
@ -148,20 +147,14 @@ public class SequenceFetcher {
} }
updateStmt.setObject(1, obj); updateStmt.setObject(1, obj);
stmt = updateStmt; try {
int rows = stmt.executeUpdate(); int rows = updateStmt.executeUpdate();
assert rows == 1 : "Come on....how exactly did we update this many rows " + rows + " for " + updateStmt.toString(); assert rows == 1 : "Come on....how exactly did we update this many rows " + rows + " for " + updateStmt.toString();
txn.commit(); txn.commit();
return (T)obj; return (T)obj;
} catch (SQLException e) { } catch (SQLException e) {
s_logger.warn("Caught this exception when running: " + (stmt != null ? stmt.toString() : ""), e); s_logger.warn("Caught this exception when running: " + (updateStmt != null ? updateStmt.toString() : ""), e);
} finally {
if (rs != null) {
rs.close();
} }
selectStmt.close();
updateStmt.close();
txn.close();
} }
} catch (Exception e) { } catch (Exception e) {
s_logger.warn("Caught this exception when running.", e); s_logger.warn("Caught this exception when running.", e);
@ -169,5 +162,4 @@ public class SequenceFetcher {
return null; return null;
} }
} }
} }