diff --git a/base/src/org/adempiere/util/ProcessUtil.java b/base/src/org/adempiere/util/ProcessUtil.java index 51687f7918..3ce3c9038e 100644 --- a/base/src/org/adempiere/util/ProcessUtil.java +++ b/base/src/org/adempiere/util/ProcessUtil.java @@ -39,7 +39,24 @@ public final class ProcessUtil { private ProcessUtil() {} + /** + * @param processInfo + * @param ProcedureName + * @param trx + * @return boolean + */ public static boolean startDatabaseProcedure (ProcessInfo processInfo, String ProcedureName, Trx trx) { + return startDatabaseProcedure(processInfo, ProcedureName, trx, true); + } + + /** + * @param processInfo + * @param ProcedureName + * @param trx + * @param managedTrx false if trx is managed by caller + * @return boolean + */ + public static boolean startDatabaseProcedure (ProcessInfo processInfo, String ProcedureName, Trx trx, boolean managedTrx) { String sql = "{call " + ProcedureName + "(?)}"; String trxName = trx != null ? trx.getTrxName() : null; try @@ -49,24 +66,27 @@ public final class ProcessUtil { cstmt.setInt(1, processInfo.getAD_PInstance_ID()); cstmt.executeUpdate(); cstmt.close(); - if (trx != null && trx.isActive()) + if (trx != null && trx.isActive() && managedTrx) { trx.commit(true); - trx.close(); } } catch (Exception e) { log.log(Level.SEVERE, sql, e); - if (trx != null && trx.isActive()) + if (trx != null && trx.isActive() && managedTrx) { trx.rollback(); - trx.close(); } processInfo.setSummary (Msg.getMsg(Env.getCtx(), "ProcessRunError") + " " + e.getLocalizedMessage()); processInfo.setError (true); return false; } + finally + { + if (trx != null && managedTrx) + trx.close(); + } return true; } @@ -75,7 +95,24 @@ public final class ProcessUtil { return startJavaProcess(Env.getCtx(), pi, trx); } + /** + * @param ctx + * @param pi + * @param trx + * @return boolean + */ public static boolean startJavaProcess(Properties ctx, ProcessInfo pi, Trx trx) { + return startJavaProcess(ctx, pi, trx, true); + } + + /** + * @param ctx + * @param pi + * @param trx + * @param managedTrx false if trx is managed by caller + * @return boolean + */ + public static boolean startJavaProcess(Properties ctx, ProcessInfo pi, Trx trx, boolean managedTrx) { String className = pi.getClassName(); if (className == null) { MProcess proc = new MProcess(ctx, pi.getAD_Process_ID(), trx.getTrxName()); @@ -121,11 +158,9 @@ public final class ProcessUtil { try { success = process.startProcess(ctx, pi, trx); - if (success && trx != null) + if (success && trx != null && managedTrx) { trx.commit(true); - trx.close(); - trx = null; } } catch (Exception e) @@ -136,7 +171,7 @@ public final class ProcessUtil { } finally { - if (trx != null) + if (trx != null && managedTrx) { trx.rollback(); trx.close(); @@ -277,69 +312,7 @@ public final class ProcessUtil { * @return */ public static boolean startJavaProcessWithoutTrxClose(Properties ctx, ProcessInfo pi, Trx trx) { - String className = pi.getClassName(); - if (className == null) { - MProcess proc = new MProcess(ctx, pi.getAD_Process_ID(), trx.getTrxName()); - if (proc.getJasperReport() != null) - className = JASPER_STARTER_CLASS; - } - //Get Class - Class processClass = null; - try - { - processClass = Class.forName (className); - } - catch (ClassNotFoundException ex) - { - log.log(Level.WARNING, className, ex); - pi.setSummary ("ClassNotFound", true); - return false; - } - - //Get Process - ProcessCall process = null; - try - { - process = (ProcessCall)processClass.newInstance(); - } - catch (Exception ex) - { - log.log(Level.WARNING, "Instance for " + className, ex); - pi.setSummary ("InstanceError", true); - return false; - } - - if (processClass == null) { - pi.setSummary("No Instance for " + pi.getClassName(), true); - return false; - } - - try - { - boolean success = process.startProcess(ctx, pi, trx); - if (trx != null) - { - if(success){ -// - } else { - trx.rollback(); - trx.close(); - return false; - } - } - } - catch (Exception e) - { - if (trx != null) - { - trx.rollback(); - trx.close(); - } - pi.setSummary("ProcessError", true); - log.log(Level.SEVERE, pi.getClassName(), e); - return false; - } - return true; + return startJavaProcess(ctx, pi, trx, false); } diff --git a/base/src/org/compiere/model/MProcess.java b/base/src/org/compiere/model/MProcess.java index b25b0448b9..122c53e22b 100644 --- a/base/src/org/compiere/model/MProcess.java +++ b/base/src/org/compiere/model/MProcess.java @@ -42,7 +42,7 @@ public class MProcess extends X_AD_Process /** * */ - private static final long serialVersionUID = 2404724380401712390L; + private static final long serialVersionUID = 6665942554198058466L; /** @@ -183,6 +183,17 @@ public class MProcess extends X_AD_Process * @return Process Instance */ public MPInstance processIt (int Record_ID, Trx trx) + { + return processIt(Record_ID, trx, true); + } + + /************************************************************************** + * Process w/o parameter + * @param Record_ID record + * @param trx transaction + * @return Process Instance + */ + public MPInstance processIt (int Record_ID, Trx trx, boolean managedTrx) { MPInstance pInstance = new MPInstance (this, Record_ID); // Lock @@ -193,7 +204,7 @@ public class MProcess extends X_AD_Process ProcessInfo processInfo = new ProcessInfo("", this.getAD_Process_ID()); processInfo.setAD_PInstance_ID(pInstance.getAD_PInstance_ID()); - ok = processIt(processInfo, trx); + ok = processIt(processInfo, trx, managedTrx); // Unlock pInstance.setResult(ok ? MPInstance.RESULT_OK : MPInstance.RESULT_ERROR); @@ -212,6 +223,17 @@ public class MProcess extends X_AD_Process * @return true if OK */ public boolean processIt (ProcessInfo pi, Trx trx) + { + return processIt(pi, trx, true); + } + + /** + * Process It (sync) + * @param pi Process Info + * @param trx transaction + * @return true if OK + */ + public boolean processIt (ProcessInfo pi, Trx trx, boolean managedTrx) { if (pi.getAD_PInstance_ID() == 0) { @@ -228,7 +250,7 @@ public class MProcess extends X_AD_Process if (Classname != null && Classname.length() > 0) { pi.setClassName(Classname); - ok = startClass(pi, trx); + ok = startClass(pi, trx, managedTrx); } else { @@ -236,7 +258,7 @@ public class MProcess extends X_AD_Process String ProcedureName = getProcedureName(); if (ProcedureName != null && ProcedureName.length() > 0) { - ok = startProcess (ProcedureName, pi, trx); + ok = startProcess (ProcedureName, pi, trx, managedTrx); } else { @@ -263,16 +285,17 @@ public class MProcess extends X_AD_Process * Start Database Process * @param ProcedureName PL/SQL procedure name * @param pInstance process instance + * @param managedTrx false if trx is managed by caller * see ProcessCtl.startProcess * @return true if success */ - private boolean startProcess (String ProcedureName, ProcessInfo processInfo, Trx trx) + private boolean startProcess (String ProcedureName, ProcessInfo processInfo, Trx trx, boolean managedTrx) { int AD_PInstance_ID = processInfo.getAD_PInstance_ID(); // execute on this thread/connection log.info(ProcedureName + "(" + AD_PInstance_ID + ")"); - return ProcessUtil.startDatabaseProcedure(processInfo, ProcedureName, trx); + return ProcessUtil.startDatabaseProcedure(processInfo, ProcedureName, trx, managedTrx); } // startProcess @@ -286,14 +309,15 @@ public class MProcess extends X_AD_Process * @param Classname name of the class to call * @param pi process info * @param trx transaction + * @param managedTrx false if trx is managed by caller * @return true if success * see ProcessCtl.startClass */ - private boolean startClass (ProcessInfo pi, Trx trx) + private boolean startClass (ProcessInfo pi, Trx trx, boolean managedTrx) { log.info(pi.getClassName()); - return ProcessUtil.startJavaProcess(getCtx(), pi, trx); + return ProcessUtil.startJavaProcess(getCtx(), pi, trx, managedTrx); } // startClass @@ -431,60 +455,7 @@ public class MProcess extends X_AD_Process */ public boolean processItWithoutTrxClose (ProcessInfo pi, Trx trx) { - if (pi.getAD_PInstance_ID() == 0) - { - MPInstance pInstance = new MPInstance (this, pi.getRecord_ID()); - // Lock - pInstance.setIsProcessing(true); - pInstance.save(); - } - - boolean ok = false; - - // Java Class - String Classname = getClassname(); - if (Classname != null && Classname.length() > 0) - { - pi.setClassName(Classname); - ok = startClassWithoutTrxClose(pi, trx); - } - else - { - // PL/SQL Procedure - String ProcedureName = getProcedureName(); - if (ProcedureName != null && ProcedureName.length() > 0) - { - ok = startProcess (ProcedureName, pi, trx); - } - else - { - String msg = "No Classname or ProcedureName for " + getName(); - pi.setSummary(msg, ok); - log.warning(msg); - } - } - - return ok; + return processIt(pi, trx, false); } // processItWithoutTrxClose - /** - * Start Java Class (sync) without closing the given transaction. - * instanciate the class implementing the interface ProcessCall. - * The class can be a Server/Client class (when in Package - * org adempiere.process or org.compiere.model) or a client only class - * (e.g. in org.compiere.report) - * - * @param Classname name of the class to call - * @param pi process info - * @param trx transaction - * @return true if success - * see ProcessCtl.startClass - */ - private boolean startClassWithoutTrxClose (ProcessInfo pi, Trx trx) - { - log.info(pi.getClassName()); - return ProcessUtil.startJavaProcessWithoutTrxClose(getCtx(), pi, trx); - } // startClassWithoutTrxClose - - } // MProcess