From 07ecea14940495a2b74b3c36b633d98a25975f2f Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Mon, 1 May 2023 13:26:16 +0200 Subject: [PATCH] IDEMPIERE-5509 Implement readonly query without using Trx - fix issue with native sequences in PostgreSQL (#1815) --- .../process/EnableNativeSequence.java | 2 +- .../org/adempiere/webui/ValuePreference.java | 3 +++ .../src/org/compiere/db/DB_PostgreSQL.java | 19 ++++++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/org.adempiere.base/src/org/eevolution/process/EnableNativeSequence.java b/org.adempiere.base/src/org/eevolution/process/EnableNativeSequence.java index b54d713f86..ac5bfd2b8e 100644 --- a/org.adempiere.base/src/org/eevolution/process/EnableNativeSequence.java +++ b/org.adempiere.base/src/org/eevolution/process/EnableNativeSequence.java @@ -61,7 +61,7 @@ public class EnableNativeSequence extends SvrProcess boolean SYSTEM_NATIVE_SEQUENCE = MSysConfig.getBooleanValue(MSysConfig.SYSTEM_NATIVE_SEQUENCE,false); if (SYSTEM_NATIVE_SEQUENCE) { - throw new AdempiereException("Native Sequence is Actived"); + throw new AdempiereException("Native Sequence is already set"); } // update the sysconfig key to Y out of trx and reset the cache diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/ValuePreference.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/ValuePreference.java index b0572ad4fb..d6a7cc9d4e 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/ValuePreference.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/ValuePreference.java @@ -20,6 +20,7 @@ import java.util.Properties; import java.util.UUID; import java.util.logging.Level; +import org.adempiere.exceptions.AdempiereException; import org.adempiere.webui.adwindow.ADWindow; import org.adempiere.webui.adwindow.AbstractADWindowContent; import org.adempiere.webui.apps.AEnv; @@ -642,6 +643,8 @@ public class ValuePreference extends Window implements EventListener int Client_ID = cbClient.isChecked() ? m_AD_Client_ID : 0; int Org_ID = cbOrg.isChecked() ? m_AD_Org_ID : 0; int AD_Preference_ID = DB.getNextID(m_ctx, "AD_Preference", null); + if (AD_Preference_ID < 0) + throw new AdempiereException("Cannot obtain sequence for AD_Preference"); // StringBuilder sql = new StringBuilder ("INSERT INTO AD_Preference (" + "AD_Preference_ID, AD_Preference_UU, AD_Client_ID, AD_Org_ID, IsActive, Created,CreatedBy,Updated,UpdatedBy," diff --git a/org.compiere.db.postgresql.provider/src/org/compiere/db/DB_PostgreSQL.java b/org.compiere.db.postgresql.provider/src/org/compiere/db/DB_PostgreSQL.java index eb0999152c..30a6c27a0a 100755 --- a/org.compiere.db.postgresql.provider/src/org/compiere/db/DB_PostgreSQL.java +++ b/org.compiere.db.postgresql.provider/src/org/compiere/db/DB_PostgreSQL.java @@ -871,7 +871,24 @@ public class DB_PostgreSQL implements AdempiereDatabase } public int getNextID(String name, String trxName) { - int m_sequence_id = DB.getSQLValueEx(trxName, "SELECT nextval('"+name.toLowerCase()+"')"); + Trx trx = null; + int m_sequence_id = -1; + try { + // avoid cannot execute nextval() in a read-only transaction + if (trxName == null) { + trxName = Trx.createTrxName("getNextval"); + trx = Trx.get(trxName, true); + } + m_sequence_id = DB.getSQLValueEx(trxName, "SELECT nextval('"+name.toLowerCase()+"')"); + } catch (Exception e) { + if (trx != null) { + trx.rollback(); + } + } finally { + if (trx != null) { + trx.close(); + } + } return m_sequence_id; }