IDEMPIERE-497 2Pack SQL Statement Handler improvements for Postgresql. Handle scenario where savepoint become invalid because there's commit or rollback clause in the sql statement.

This commit is contained in:
Heng Sin Low 2013-08-22 16:49:37 +08:00
parent 25f2528b55
commit be0f3bfdc5
2 changed files with 28 additions and 25 deletions

View File

@ -2644,10 +2644,10 @@ public abstract class PO
else else
{ {
if (m_trxName == null) if (m_trxName == null)
log.log(Level.WARNING, "#" + no log.log(Level.WARNING, "Update return " + no + " instead of 1"
+ " - " + p_info.getTableName() + "." + where); + " - " + p_info.getTableName() + "." + where);
else else
log.log(Level.WARNING, "#" + no log.log(Level.WARNING, "Update return " + no + " instead of 1"
+ " - [" + m_trxName + "] - " + p_info.getTableName() + "." + where); + " - [" + m_trxName + "] - " + p_info.getTableName() + "." + where);
} }
return ok; return ok;

View File

@ -49,15 +49,13 @@ public class SQLStatementElementHandler extends AbstractElementHandler {
sql = sql.substring(0, sql.length() - 1); sql = sql.substring(0, sql.length() - 1);
Savepoint savepoint = null; Savepoint savepoint = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
X_AD_Package_Imp_Detail impDetail = null;
try { try {
// NOTE Postgres needs to commit DDL statements // NOTE Postgres needs to commit DDL statements
// add a SQL command just with COMMIT if you want to simulate the Oracle behavior (commit on DDL) // add a SQL command just with COMMIT if you want to simulate the Oracle behavior (commit on DDL)
// Use savepoint here so that SQL exception would not rollback the whole process // Use savepoint here so that SQL exception would not rollback the whole process
if (DB.isPostgreSQL())
{
Trx trx = Trx.get(getTrxName(ctx), true); Trx trx = Trx.get(getTrxName(ctx), true);
savepoint = trx.setSavepoint(null); savepoint = trx.setSavepoint(null);
}
pstmt = DB.prepareStatement(sql, getTrxName(ctx)); pstmt = DB.prepareStatement(sql, getTrxName(ctx));
if (DBType.equals("ALL")) { if (DBType.equals("ALL")) {
@ -87,26 +85,22 @@ public class SQLStatementElementHandler extends AbstractElementHandler {
stmt = null; stmt = null;
} }
} }
X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, "",
0);
logImportDetail (ctx, impDetail, 1, "SQLStatement",1,"Execute");
} catch (Exception e) { } catch (Exception e) {
if (DB.isPostgreSQL()) { // rollback immediately on exception to avoid a wrong SQL stop the whole process
// rollback immediately postgres on exception to avoid a wrong SQL stop the whole process
if (savepoint != null) if (savepoint != null)
{ {
Trx trx = Trx.get(getTrxName(ctx), false); Trx trx = Trx.get(getTrxName(ctx), false);
try { try {
trx.rollback(savepoint); if (trx.getConnection() != null)
} catch (SQLException e1) {} trx.getConnection().rollback(savepoint);
} catch (SQLException e1) {
//a rollback or commit have happens making the savepoint becomes invalid.
//rollback trx to continue
trx.rollback();
}
savepoint = null; savepoint = null;
} }
}
log.log(Level.SEVERE,"SQLSatement", e); log.log(Level.SEVERE,"SQLSatement", e);
X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, "",
0);
logImportDetail (ctx, impDetail, 0, "SQLStatement",1,"Execute");
} finally { } finally {
DB.close(pstmt); DB.close(pstmt);
pstmt = null; pstmt = null;
@ -114,10 +108,19 @@ public class SQLStatementElementHandler extends AbstractElementHandler {
Trx trx = Trx.get(getTrxName(ctx), false); Trx trx = Trx.get(getTrxName(ctx), false);
try { try {
trx.releaseSavepoint(savepoint); trx.releaseSavepoint(savepoint);
} catch (SQLException e) {} } catch (SQLException e) {
if (DB.isPostgreSQL()) {
//a commit or rollback have happens that make the savepoint invalid.
//need to call rollback to continue
trx.commit();
} }
} }
} }
}
impDetail = createImportDetail(ctx, element.qName, "",
0);
logImportDetail (ctx, impDetail, 1, "SQLStatement",1,"Execute");
}
public void endElement(PIPOContext ctx, Element element) throws SAXException { public void endElement(PIPOContext ctx, Element element) throws SAXException {
} }