Skip to content

Commit

Permalink
Merge pull request #4049 from adempiere/feature/4048
Browse files Browse the repository at this point in the history
#4048 [Bug Report] Lock transaction when try complete a payment using…
  • Loading branch information
niclopup committed Jan 17, 2023
2 parents 83d8a00 + 6246267 commit 0da7739
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion base/src/org/compiere/model/MFactAcct.java
Expand Up @@ -131,7 +131,7 @@ public MAccount getMAccount()
getC_LocFrom_ID(), getC_LocTo_ID(), getC_SalesRegion_ID(),
getC_Project_ID(), getC_Campaign_ID(), getC_Activity_ID(),
getUser1_ID(), getUser2_ID(), getUser3_ID() , getUser4_ID(),
getUserElement1_ID(), getUserElement2_ID(), null);
getUserElement1_ID(), getUserElement2_ID(), get_TrxName());
if (acct != null && acct.get_ID() == 0)
acct.saveEx();
return acct;
Expand Down
5 changes: 3 additions & 2 deletions base/src/org/compiere/process/DocumentEngine.java
Expand Up @@ -55,6 +55,7 @@
import org.compiere.model.MRole;
import org.compiere.model.MTable;
import org.compiere.model.PO;
import org.compiere.util.AdempiereUserError;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
Expand Down Expand Up @@ -1464,7 +1465,7 @@ public String postImmediate (boolean force)
String error = null;
if (MClient.isClientAccounting()) {
getLogger().info ("Table=" + tableName + ", Record=" + recordId);
MAcctSchema[] ass = MAcctSchema.getClientAcctSchema(ctx, clientId);
MAcctSchema[] ass = MAcctSchema.getClientAcctSchema(ctx, clientId, trxName);
Doc doc = getDoc(ass, tableName, recordId, trxName);
if (doc != null) {
error = doc.postImmediate(force);
Expand Down Expand Up @@ -1545,7 +1546,7 @@ public Doc getDoc (MAcctSchema[] acctSchemas, String tableName, int recordId, St
* @param rs ResultSet
* @param trxName transaction name
* @return Document
* @throws AdempiereUserError
* @throws AdempiereUserError
*/
public Doc getDoc (MAcctSchema[] acctSchemas, String tableName, ResultSet rs, String trxName) {
return Doc.get(acctSchemas, tableName, rs, trxName);
Expand Down
46 changes: 29 additions & 17 deletions base/src/org/compiere/wf/MWorkflow.java
Expand Up @@ -25,8 +25,10 @@
import java.util.Calendar;
import java.util.List;
import java.util.Properties;
import java.util.function.Function;
import java.util.logging.Level;


import org.adempiere.core.domains.models.X_AD_Workflow;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException;
Expand Down Expand Up @@ -707,6 +709,7 @@ public MWFProcess start (ProcessInfo processInfo)
MWFProcess workflowProcess = null;
Trx workflowProcessTransaction = null;
Savepoint savepoint = null;
Function<ProcessInfo,Boolean> isWorkflowEngineTransaction = pi -> pi.getTransactionName() == null;
try {
workflowProcess = new MWFProcess (this, processInfo, null);
// Check if exits activities actives if this way then Other Process Active
Expand All @@ -718,9 +721,11 @@ && isLock(getAD_Table_ID(), processInfo.getRecord_ID())
} else if (MWorkflow.WORKFLOWTYPE_DocumentProcess.equals(getWorkflowType())) {
workflowProcess.lockDocument();
}
if (processInfo.getTransactionName() == null)
if (isWorkflowEngineTransaction.apply(processInfo)) {
//Create new transaction
workflowProcessTransaction = Trx.get(Trx.createTrxName("WFP"), true);
else {
} else {
//Use the transaction from process info
workflowProcessTransaction = Trx.get(processInfo.getTransactionName(), false);
savepoint = workflowProcessTransaction.setSavepoint(null);
}
Expand All @@ -730,33 +735,39 @@ && isLock(getAD_Table_ID(), processInfo.getRecord_ID())
processInfo.setSummary(Msg.getMsg(getCtx(), "Processing"));
workflowProcess.startWork(workflowProcessTransaction);
//Check if transaction is management by the process info or workflow engine
if (workflowProcessTransaction != null) {
if (processInfo.getTransactionName() == null)
workflowProcessTransaction.commit(true);
else
workflowProcessTransaction.releaseSavepoint(savepoint);
if (isWorkflowEngineTransaction.apply(processInfo)) {
//Commit the Workflow Engine Transaction
assert workflowProcessTransaction != null;
workflowProcessTransaction.commit(true);
} else {
//Commit save point using the Process Info Transaction
assert workflowProcessTransaction != null;
workflowProcessTransaction.releaseSavepoint(savepoint);
}
if (MWorkflow.WORKFLOWTYPE_DocumentProcess.equals(getWorkflowType()))
workflowProcess.unlockDocument();
} catch (Exception e) {
if (workflowProcessTransaction != null) {
if (processInfo.getTransactionName() == null)
workflowProcessTransaction.rollback();
else {
try {
workflowProcessTransaction.rollback(savepoint);
} catch (SQLException sqlException) {
throw new AdempiereException(sqlException.getMessage());
}
if (isWorkflowEngineTransaction.apply(processInfo)) {
assert workflowProcessTransaction != null;
workflowProcessTransaction.rollback();
}
else {
try {
assert workflowProcessTransaction != null;
workflowProcessTransaction.rollback(savepoint);
} catch (SQLException sqlException) {
throw new AdempiereException(sqlException.getMessage());
}
}
log.log(Level.SEVERE, e.getLocalizedMessage(), e);
processInfo.setSummary(e.getMessage(), true);
workflowProcess = null;
} finally {
//Check if transaction is management by the process info or workflow engine
if (workflowProcessTransaction != null && processInfo.getTransactionName() == null)
if (isWorkflowEngineTransaction.apply(processInfo)) {
assert workflowProcessTransaction != null;
workflowProcessTransaction.close();
}
}
return workflowProcess;
} // MWFProcess
Expand Down Expand Up @@ -790,6 +801,7 @@ public MWFProcess startWait (ProcessInfo processInfo)
Thread.yield();
StateEngine state = process.getState();
int loops = 0;

while (!state.isClosed() && !state.isSuspended())
{
if (loops > MAXLOOPS)
Expand Down

0 comments on commit 0da7739

Please sign in to comment.