[Minor] Added update count checking when executing INSERT, UPDATE, DEL
This commit is contained in:
parent
902a153043
commit
911a92ad5e
|
@ -107,7 +107,12 @@ public class PostgreSqlOrderDao extends PostgresqlDao<Order> implements OrderDao
|
||||||
SQLXML sqlxml = createSqlXml(order, preparedStatement);
|
SQLXML sqlxml = createSqlXml(order, preparedStatement);
|
||||||
preparedStatement.setSQLXML(6, sqlxml);
|
preparedStatement.setSQLXML(6, sqlxml);
|
||||||
try {
|
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 {
|
} finally {
|
||||||
sqlxml.free();
|
sqlxml.free();
|
||||||
}
|
}
|
||||||
|
@ -133,7 +138,12 @@ public class PostgreSqlOrderDao extends PostgresqlDao<Order> implements OrderDao
|
||||||
SQLXML sqlxml = createSqlXml(order, preparedStatement);
|
SQLXML sqlxml = createSqlXml(order, preparedStatement);
|
||||||
preparedStatement.setSQLXML(5, sqlxml);
|
preparedStatement.setSQLXML(5, sqlxml);
|
||||||
try {
|
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 {
|
} finally {
|
||||||
sqlxml.free();
|
sqlxml.free();
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,7 +99,12 @@ public class PostgreSqlResourceDao extends PostgresqlDao<Resource> implements Re
|
||||||
SQLXML sqlxml = createSqlXml(res, preparedStatement);
|
SQLXML sqlxml = createSqlXml(res, preparedStatement);
|
||||||
preparedStatement.setSQLXML(4, sqlxml);
|
preparedStatement.setSQLXML(4, sqlxml);
|
||||||
try {
|
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 {
|
} finally {
|
||||||
sqlxml.free();
|
sqlxml.free();
|
||||||
}
|
}
|
||||||
|
@ -122,7 +127,12 @@ public class PostgreSqlResourceDao extends PostgresqlDao<Resource> implements Re
|
||||||
SQLXML sqlxml = createSqlXml(resource, preparedStatement);
|
SQLXML sqlxml = createSqlXml(resource, preparedStatement);
|
||||||
preparedStatement.setSQLXML(3, sqlxml);
|
preparedStatement.setSQLXML(3, sqlxml);
|
||||||
try {
|
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 {
|
} finally {
|
||||||
sqlxml.free();
|
sqlxml.free();
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,22 +43,19 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void commit(TransactionResult txResult) throws Exception {
|
protected void commit(TransactionResult txResult) throws Exception {
|
||||||
try {
|
|
||||||
if (this.orderDao != null)
|
// first perform DAOs
|
||||||
this.orderDao.commit(txResult);
|
if (this.orderDao != null)
|
||||||
if (this.resourceDao != null)
|
this.orderDao.commit(txResult);
|
||||||
this.resourceDao.commit(txResult);
|
if (this.resourceDao != null)
|
||||||
if (this.connection != null)
|
this.resourceDao.commit(txResult);
|
||||||
this.connection.commit();
|
|
||||||
} finally {
|
// then commit the SQL connection
|
||||||
if (this.connection != null) {
|
if (this.connection != null)
|
||||||
try {
|
this.connection.commit();
|
||||||
this.connection.close();
|
|
||||||
} catch (Exception e) {
|
// and close the connection, but not catching, as otherwise we can't rollback in exception case
|
||||||
logger.error("Failed to close connection due to " + e.getMessage(), e); //$NON-NLS-1$
|
this.connection.close();
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -289,7 +289,12 @@ public abstract class PostgresqlDao<T extends StrolchElement> implements Strolch
|
||||||
try (PreparedStatement preparedStatement = this.tx.getConnection().prepareStatement(sql)) {
|
try (PreparedStatement preparedStatement = this.tx.getConnection().prepareStatement(sql)) {
|
||||||
|
|
||||||
preparedStatement.setString(1, element.getId());
|
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) {
|
} catch (SQLException e) {
|
||||||
throw new StrolchPersistenceException(MessageFormat.format("Failed to update Order {0} due to {2}",
|
throw new StrolchPersistenceException(MessageFormat.format("Failed to update Order {0} due to {2}",
|
||||||
|
|
Loading…
Reference in New Issue