diff --git a/org.adempiere.base/src/org/compiere/util/DB.java b/org.adempiere.base/src/org/compiere/util/DB.java index 6457ab8d77..20837117aa 100644 --- a/org.adempiere.base/src/org/compiere/util/DB.java +++ b/org.adempiere.base/src/org/compiere/util/DB.java @@ -1291,6 +1291,10 @@ public final class DB } catch (SQLException e) { + if (trx != null) + { + trx.rollback(); + } throw new DBException(e, sql); } finally @@ -1386,6 +1390,10 @@ public final class DB } catch (SQLException e) { + if (trx != null) + { + trx.rollback(); + } throw new DBException(e, sql); } finally @@ -1481,7 +1489,10 @@ public final class DB } catch (SQLException e) { - //log.log(Level.SEVERE, sql, getSQLException(e)); + if (trx != null) + { + trx.rollback(); + } throw new DBException(e, sql); } finally @@ -1578,6 +1589,10 @@ public final class DB } catch (SQLException e) { + if (trx != null) + { + trx.rollback(); + } throw new DBException(e, sql); } finally @@ -2478,8 +2493,18 @@ public final class DB List retValue = new ArrayList(); PreparedStatement pstmt = null; ResultSet rs = null; + Trx trx = null; + if (trxName == null) + { + trxName = Trx.createTrxName("getSQLValueObjectsEx"); + trx = Trx.get(trxName, true); + } try { + if (trx != null) + { + trx.getConnection().setReadOnly(true); + } pstmt = prepareStatement(sql, trxName); setParameters(pstmt, params); rs = pstmt.executeQuery(); @@ -2498,12 +2523,20 @@ public final class DB } catch (SQLException e) { + if (trx != null) + { + trx.rollback(); + } throw new DBException(e, sql); } finally { close(rs, pstmt); rs = null; pstmt = null; + if (trx != null) + { + trx.close(); + } } return retValue; } @@ -2521,8 +2554,18 @@ public final class DB List> rowsArray = new ArrayList>(); PreparedStatement pstmt = null; ResultSet rs = null; + Trx trx = null; + if (trxName == null) + { + trxName = Trx.createTrxName("getSQLArrayObjectsEx"); + trx = Trx.get(trxName, true); + } try { + if (trx != null) + { + trx.getConnection().setReadOnly(true); + } pstmt = prepareStatement(sql, trxName); setParameters(pstmt, params); rs = pstmt.executeQuery(); @@ -2541,12 +2584,20 @@ public final class DB } catch (SQLException e) { + if (trx != null) + { + trx.rollback(); + } throw new DBException(e, sql); } finally { close(rs, pstmt); rs = null; pstmt = null; + if (trx != null) + { + trx.close(); + } } if (rowsArray.size() == 0) return null; diff --git a/org.idempiere.test/src/org/idempiere/test/base/DBTest.java b/org.idempiere.test/src/org/idempiere/test/base/DBTest.java index 3065c5eabf..9037e08cb3 100644 --- a/org.idempiere.test/src/org/idempiere/test/base/DBTest.java +++ b/org.idempiere.test/src/org/idempiere/test/base/DBTest.java @@ -21,14 +21,18 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.math.BigDecimal; import java.sql.Timestamp; import java.util.ArrayList; +import java.util.List; import org.adempiere.exceptions.DBException; import org.compiere.model.MTable; +import org.compiere.model.X_Test; import org.compiere.util.DB; +import org.compiere.util.Env; import org.compiere.util.KeyNamePair; import org.compiere.util.TimeUtil; import org.compiere.util.ValueNamePair; import org.idempiere.test.AbstractTestCase; +import org.idempiere.test.DictionaryIDs; import org.junit.jupiter.api.Test; /** @@ -38,6 +42,8 @@ import org.junit.jupiter.api.Test; */ public class DBTest extends AbstractTestCase { + private static final int TEST_RECORD_ID = 103; + @Test public void test_getSQLValueEx() throws Exception { @@ -51,18 +57,18 @@ public class DBTest extends AbstractTestCase DB.getSQLValueEx(null, "SELECT 10 FROM INEXISTENT_TABLE"); }); - int t_integer = DB.getSQLValueEx(null, "select t_integer from test where test_id=?", 103); + int t_integer = DB.getSQLValueEx(null, "select t_integer from test where test_id=?", TEST_RECORD_ID); assertThrows(DBException.class, () -> { - DB.getSQLValueEx(null, "update test set t_integer=1 where test_id=?", 103); + DB.getSQLValueEx(null, "update test set t_integer=1 where test_id=?", TEST_RECORD_ID); }); - int t_integer1 = DB.getSQLValueEx(null, "select t_integer from test where test_id=?", 103); + int t_integer1 = DB.getSQLValueEx(null, "select t_integer from test where test_id=?", TEST_RECORD_ID); assertEquals(t_integer, t_integer1, "test.t_integer wrongly updated"); assertThrows(DBException.class, () -> { - DB.getSQLValueEx(getTrxName(), "update test set t_integer=1 where test_id=?;select t_integer from test where test_id=?", 103); + DB.getSQLValueEx(getTrxName(), "update test set t_integer=1 where test_id=?;select t_integer from test where test_id=?", TEST_RECORD_ID); }); rollback(); - t_integer1 = DB.getSQLValueEx(null, "select t_integer from test where test_id=?", 103); + t_integer1 = DB.getSQLValueEx(null, "select t_integer from test where test_id=?", TEST_RECORD_ID); assertEquals(t_integer, t_integer1, "test.t_integer wrongly updated"); } @@ -218,4 +224,54 @@ public class DBTest extends AbstractTestCase assertEquals(arr[5].getName(), "7"); } + @Test + public void test_getSQLValueObjectsEx() + { + StringBuilder sql = new StringBuilder("SELECT ") + .append(X_Test.COLUMNNAME_Test_ID) + .append(", ") + .append(X_Test.COLUMNNAME_Test_UU) + .append(" FROM Test WHERE Test_ID=?"); + List objects = DB.getSQLValueObjectsEx(null, sql.toString(), TEST_RECORD_ID); + assertEquals(2, objects.size()); + X_Test test = new X_Test(Env.getCtx(), TEST_RECORD_ID, getTrxName()); + assertEquals(test.get_ID(), ((Number)objects.get(0)).intValue()); + assertEquals(test.getTest_UU(), objects.get(1)); + + objects = DB.getSQLValueObjectsEx(getTrxName(), sql.toString(), TEST_RECORD_ID); + assertEquals(2, objects.size()); + assertEquals(test.get_ID(), ((Number)objects.get(0)).intValue()); + assertEquals(test.getTest_UU(), objects.get(1)); + } + + @Test + public void test_getSQLArrayObjectsEx() + { + String sql = "SELECT M_Product_ID, Name FROM M_Product WHERE M_Product_ID IN (?, ?, ?)"; + List> rows = DB.getSQLArrayObjectsEx(null, sql, DictionaryIDs.M_Product.AZALEA_BUSH.id, DictionaryIDs.M_Product.MULCH.id, DictionaryIDs.M_Product.FERTILIZER_50.id); + assertEquals(3, rows.size()); + int match = 0; + for(List row : rows) { + assertEquals(2, row.size()); + Number id = (Number)row.get(0); + if (id.intValue() == DictionaryIDs.M_Product.AZALEA_BUSH.id || + id.intValue() == DictionaryIDs.M_Product.MULCH.id || + id.intValue() == DictionaryIDs.M_Product.FERTILIZER_50.id) + match++; + } + assertEquals(3, match); + + rows = DB.getSQLArrayObjectsEx(getTrxName(), sql, DictionaryIDs.M_Product.AZALEA_BUSH.id, DictionaryIDs.M_Product.MULCH.id, DictionaryIDs.M_Product.FERTILIZER_50.id); + assertEquals(3, rows.size()); + match = 0; + for(List row : rows) { + assertEquals(2, row.size()); + Number id = (Number)row.get(0); + if (id.intValue() == DictionaryIDs.M_Product.AZALEA_BUSH.id || + id.intValue() == DictionaryIDs.M_Product.MULCH.id || + id.intValue() == DictionaryIDs.M_Product.FERTILIZER_50.id) + match++; + } + assertEquals(3, match); + } }