[Minor] Added update count checking when executing INSERT, UPDATE, DEL

This commit is contained in:
Robert von Burg 2014-02-25 21:19:10 +01:00
parent 902a153043
commit 911a92ad5e
4 changed files with 43 additions and 21 deletions

View File

@ -107,7 +107,12 @@ public class PostgreSqlOrderDao extends PostgresqlDao<Order> implements OrderDao
SQLXML sqlxml = createSqlXml(order, preparedStatement);
preparedStatement.setSQLXML(6, sqlxml);
try {
preparedStatement.execute();
int modCount = preparedStatement.executeUpdate();
if (modCount != 1) {
String msg = "Expected to save 1 element with id {0} but SQL statement modified {1} elements!";
msg = MessageFormat.format(msg, order.getId(), modCount);
throw new StrolchPersistenceException(msg);
}
} finally {
sqlxml.free();
}
@ -133,7 +138,12 @@ public class PostgreSqlOrderDao extends PostgresqlDao<Order> implements OrderDao
SQLXML sqlxml = createSqlXml(order, preparedStatement);
preparedStatement.setSQLXML(5, sqlxml);
try {
preparedStatement.execute();
int modCount = preparedStatement.executeUpdate();
if (modCount != 1) {
String msg = "Expected to update 1 element with id {0} but SQL statement modified {1} elements!";
msg = MessageFormat.format(msg, order.getId(), modCount);
throw new StrolchPersistenceException(msg);
}
} finally {
sqlxml.free();
}

View File

@ -99,7 +99,12 @@ public class PostgreSqlResourceDao extends PostgresqlDao<Resource> implements Re
SQLXML sqlxml = createSqlXml(res, preparedStatement);
preparedStatement.setSQLXML(4, sqlxml);
try {
preparedStatement.execute();
int modCount = preparedStatement.executeUpdate();
if (modCount != 1) {
String msg = "Expected to save 1 element with id {0} but SQL statement modified {1} elements!";
msg = MessageFormat.format(msg, res.getId(), modCount);
throw new StrolchPersistenceException(msg);
}
} finally {
sqlxml.free();
}
@ -122,7 +127,12 @@ public class PostgreSqlResourceDao extends PostgresqlDao<Resource> implements Re
SQLXML sqlxml = createSqlXml(resource, preparedStatement);
preparedStatement.setSQLXML(3, sqlxml);
try {
preparedStatement.execute();
int modCount = preparedStatement.executeUpdate();
if (modCount != 1) {
String msg = "Expected to update 1 element with id {0} but SQL statement modified {1} elements!";
msg = MessageFormat.format(msg, resource.getId(), modCount);
throw new StrolchPersistenceException(msg);
}
} finally {
sqlxml.free();
}

View File

@ -43,22 +43,19 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction {
@Override
protected void commit(TransactionResult txResult) throws Exception {
try {
if (this.orderDao != null)
this.orderDao.commit(txResult);
if (this.resourceDao != null)
this.resourceDao.commit(txResult);
if (this.connection != null)
this.connection.commit();
} finally {
if (this.connection != null) {
try {
this.connection.close();
} catch (Exception e) {
logger.error("Failed to close connection due to " + e.getMessage(), e); //$NON-NLS-1$
}
}
}
// first perform DAOs
if (this.orderDao != null)
this.orderDao.commit(txResult);
if (this.resourceDao != null)
this.resourceDao.commit(txResult);
// then commit the SQL connection
if (this.connection != null)
this.connection.commit();
// and close the connection, but not catching, as otherwise we can't rollback in exception case
this.connection.close();
}
@Override

View File

@ -289,7 +289,12 @@ public abstract class PostgresqlDao<T extends StrolchElement> implements Strolch
try (PreparedStatement preparedStatement = this.tx.getConnection().prepareStatement(sql)) {
preparedStatement.setString(1, element.getId());
preparedStatement.execute();
int modCount = preparedStatement.executeUpdate();
if (modCount != 1) {
String msg = "Expected to delete 1 element with id {0} but SQL statement modified {1} elements!";
msg = MessageFormat.format(msg, element.getId(), modCount);
throw new StrolchPersistenceException(msg);
}
} catch (SQLException e) {
throw new StrolchPersistenceException(MessageFormat.format("Failed to update Order {0} due to {2}",