diff --git a/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java b/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java index dcd863465d1b..10f035528992 100644 --- a/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java +++ b/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java @@ -1625,6 +1625,7 @@ public T persist(final T entity) { final TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; String sql = null; + boolean committed = false; try { txn.start(); for (final Pair pair : _insertSqls) { @@ -1673,6 +1674,7 @@ public T persist(final T entity) { insertElementCollection(entity, _idAttributes.get(_table)[0], id, ecAttributes); } txn.commit(); + committed = true; } catch (final SQLException e) { logger.error("DB Exception on: " + pstmt, e); handleEntityExistsException(e); @@ -1681,6 +1683,10 @@ public T persist(final T entity) { throw new CloudRuntimeException("Problem with getting the ec attribute ", e); } catch (IllegalAccessException e) { throw new CloudRuntimeException("Problem with getting the ec attribute ", e); + } finally { + if (!committed) { + txn.rollback(); + } } return _idField != null ? findByIdIncludingRemoved(id) : null;