From c6e42032743a6749e916606d2024d407d83382ac Mon Sep 17 00:00:00 2001 From: Heng Sin Low Date: Wed, 13 Feb 2008 04:22:10 +0000 Subject: [PATCH] [ 1854603 ] Prepare method is being called twice - trx management fix. --- base/src/org/compiere/wf/MWFActivity.java | 41 +++++++++++++---------- base/src/org/compiere/wf/MWFProcess.java | 22 +++++++++--- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/base/src/org/compiere/wf/MWFActivity.java b/base/src/org/compiere/wf/MWFActivity.java index 501b5f597e..791b85131d 100644 --- a/base/src/org/compiere/wf/MWFActivity.java +++ b/base/src/org/compiere/wf/MWFActivity.java @@ -278,8 +278,8 @@ public class MWFActivity extends X_AD_WF_Activity implements Runnable // Inform Process if (m_process == null) m_process = new MWFProcess (getCtx(), getAD_WF_Process_ID(), - m_trx == null ? null : m_trx.getTrxName()); - m_process.checkActivities(m_trx == null ? null : m_trx.getTrxName(), m_po); + this.get_TrxName()); + m_process.checkActivities(this.get_TrxName(), m_po); } else { @@ -735,18 +735,21 @@ public class MWFActivity extends X_AD_WF_Activity implements Runnable { log.info ("Node=" + getNode()); m_newValue = null; - if (!m_state.isValidAction(StateEngine.ACTION_Start)) - { - setTextMsg("State=" + getWFState() + " - cannot start"); - setWFState(StateEngine.STATE_Terminated); - return; - } - // - setWFState(StateEngine.STATE_Running); - m_trx = Trx.get(Trx.createTrxName("WF"), true); + + String trxName = Trx.createTrxName("WF"); + m_trx = Trx.get(trxName, true); // try { + if (!m_state.isValidAction(StateEngine.ACTION_Start)) + { + setTextMsg("State=" + getWFState() + " - cannot start"); + setWFState(StateEngine.STATE_Terminated); + return; + } + // + setWFState(StateEngine.STATE_Running); + if (getNode().get_ID() == 0) { setTextMsg("Node not found - AD_WF_Node_ID=" + getAD_WF_Node_ID()); @@ -766,9 +769,7 @@ public class MWFActivity extends X_AD_WF_Activity implements Runnable // teo_sarca [ 1708835 ] // Reason: if the commit fails the document should be put in Invalid state try { - m_trx.commit(true); - m_trx.close(); - m_trx = null; + m_trx.commit(true); } catch (Exception e) { // If we have a DocStatus, change it to Invalid, and throw the exception to the next level if (m_docStatus != null) @@ -787,8 +788,7 @@ public class MWFActivity extends X_AD_WF_Activity implements Runnable log.log(Level.WARNING, "" + getNode(), e); /**** Trx Rollback ****/ m_trx.rollback(); - m_trx.close(); - m_trx = null; + // if (e.getCause() != null) log.log(Level.WARNING, "Cause", e.getCause()); @@ -807,7 +807,14 @@ public class MWFActivity extends X_AD_WF_Activity implements Runnable m_po.save(); } } - m_trx = null; + finally + { + if (m_trx != null) + { + m_trx.close(); + } + m_trx = null; + } } // run diff --git a/base/src/org/compiere/wf/MWFProcess.java b/base/src/org/compiere/wf/MWFProcess.java index 5602e5077b..eeeb967c6b 100644 --- a/base/src/org/compiere/wf/MWFProcess.java +++ b/base/src/org/compiere/wf/MWFProcess.java @@ -133,6 +133,17 @@ public class MWFProcess extends X_AD_WF_Process * @return array of activities */ public MWFActivity[] getActivities (boolean requery, boolean onlyActive) + { + return getActivities(requery, onlyActive, get_TrxName()); + } + + /** + * Get active Activities of Process + * @param requery if true requery + * @param onlyActive only active activities + * @return array of activities + */ + public MWFActivity[] getActivities (boolean requery, boolean onlyActive, String trxName) { if (!requery && m_activities != null) return m_activities; @@ -145,11 +156,11 @@ public class MWFProcess extends X_AD_WF_Process sql += " AND Processed='N'"; try { - pstmt = DB.prepareStatement (sql, get_TrxName()); + pstmt = DB.prepareStatement (sql, trxName); pstmt.setInt (1, getAD_WF_Process_ID()); rs = pstmt.executeQuery (); while (rs.next ()) - list.add (new MWFActivity(getCtx(), rs, get_TrxName())); + list.add (new MWFActivity(getCtx(), rs, trxName)); } catch (Exception e) { @@ -241,7 +252,7 @@ public class MWFProcess extends X_AD_WF_Process if (m_state.isClosed()) return; // - MWFActivity[] activities = getActivities (true, true); // requery active + MWFActivity[] activities = getActivities (true, true, trxName); // requery active String closedState = null; boolean suspended = false; boolean running = false; @@ -253,7 +264,7 @@ public class MWFProcess extends X_AD_WF_Process // Completed - Start Next if (activityState.isCompleted()) { - if (startNext (activity, activities, lastPO)) + if (startNext (activity, activities, lastPO, trxName)) continue; } // @@ -310,7 +321,7 @@ public class MWFProcess extends X_AD_WF_Process * @param activities all activities * @return true if there is a next activity */ - private boolean startNext (MWFActivity last, MWFActivity[] activities, PO lastPO) + private boolean startNext (MWFActivity last, MWFActivity[] activities, PO lastPO, String trxName) { log.fine("Last=" + last); // transitions from the last processed node @@ -340,6 +351,7 @@ public class MWFProcess extends X_AD_WF_Process // Start new Activity... MWFActivity activity = new MWFActivity (this, transitions[i].getAD_WF_Next_ID(), lastPO); + activity.set_TrxName(trxName); activity.run(); // only the first valid if XOR