- remove redundant code and implement the incomplete trx api enhancement for database procedure.
This commit is contained in:
Heng Sin Low 2009-12-07 09:04:30 +00:00
parent 17c6e5db8c
commit 4f57115ea3
2 changed files with 77 additions and 133 deletions

View File

@ -39,7 +39,24 @@ public final class ProcessUtil {
private ProcessUtil() {} private ProcessUtil() {}
/**
* @param processInfo
* @param ProcedureName
* @param trx
* @return boolean
*/
public static boolean startDatabaseProcedure (ProcessInfo processInfo, String ProcedureName, Trx trx) { 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 sql = "{call " + ProcedureName + "(?)}";
String trxName = trx != null ? trx.getTrxName() : null; String trxName = trx != null ? trx.getTrxName() : null;
try try
@ -49,24 +66,27 @@ public final class ProcessUtil {
cstmt.setInt(1, processInfo.getAD_PInstance_ID()); cstmt.setInt(1, processInfo.getAD_PInstance_ID());
cstmt.executeUpdate(); cstmt.executeUpdate();
cstmt.close(); cstmt.close();
if (trx != null && trx.isActive()) if (trx != null && trx.isActive() && managedTrx)
{ {
trx.commit(true); trx.commit(true);
trx.close();
} }
} }
catch (Exception e) catch (Exception e)
{ {
log.log(Level.SEVERE, sql, e); log.log(Level.SEVERE, sql, e);
if (trx != null && trx.isActive()) if (trx != null && trx.isActive() && managedTrx)
{ {
trx.rollback(); trx.rollback();
trx.close();
} }
processInfo.setSummary (Msg.getMsg(Env.getCtx(), "ProcessRunError") + " " + e.getLocalizedMessage()); processInfo.setSummary (Msg.getMsg(Env.getCtx(), "ProcessRunError") + " " + e.getLocalizedMessage());
processInfo.setError (true); processInfo.setError (true);
return false; return false;
} }
finally
{
if (trx != null && managedTrx)
trx.close();
}
return true; return true;
} }
@ -75,7 +95,24 @@ public final class ProcessUtil {
return startJavaProcess(Env.getCtx(), pi, trx); return startJavaProcess(Env.getCtx(), pi, trx);
} }
/**
* @param ctx
* @param pi
* @param trx
* @return boolean
*/
public static boolean startJavaProcess(Properties ctx, ProcessInfo pi, Trx trx) { 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(); String className = pi.getClassName();
if (className == null) { if (className == null) {
MProcess proc = new MProcess(ctx, pi.getAD_Process_ID(), trx.getTrxName()); MProcess proc = new MProcess(ctx, pi.getAD_Process_ID(), trx.getTrxName());
@ -121,11 +158,9 @@ public final class ProcessUtil {
try try
{ {
success = process.startProcess(ctx, pi, trx); success = process.startProcess(ctx, pi, trx);
if (success && trx != null) if (success && trx != null && managedTrx)
{ {
trx.commit(true); trx.commit(true);
trx.close();
trx = null;
} }
} }
catch (Exception e) catch (Exception e)
@ -136,7 +171,7 @@ public final class ProcessUtil {
} }
finally finally
{ {
if (trx != null) if (trx != null && managedTrx)
{ {
trx.rollback(); trx.rollback();
trx.close(); trx.close();
@ -277,69 +312,7 @@ public final class ProcessUtil {
* @return * @return
*/ */
public static boolean startJavaProcessWithoutTrxClose(Properties ctx, ProcessInfo pi, Trx trx) { public static boolean startJavaProcessWithoutTrxClose(Properties ctx, ProcessInfo pi, Trx trx) {
String className = pi.getClassName(); return startJavaProcess(ctx, pi, trx, false);
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;
} }

View File

@ -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 * @return Process Instance
*/ */
public MPInstance processIt (int Record_ID, Trx trx) 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); MPInstance pInstance = new MPInstance (this, Record_ID);
// Lock // Lock
@ -193,7 +204,7 @@ public class MProcess extends X_AD_Process
ProcessInfo processInfo = new ProcessInfo("", this.getAD_Process_ID()); ProcessInfo processInfo = new ProcessInfo("", this.getAD_Process_ID());
processInfo.setAD_PInstance_ID(pInstance.getAD_PInstance_ID()); processInfo.setAD_PInstance_ID(pInstance.getAD_PInstance_ID());
ok = processIt(processInfo, trx); ok = processIt(processInfo, trx, managedTrx);
// Unlock // Unlock
pInstance.setResult(ok ? MPInstance.RESULT_OK : MPInstance.RESULT_ERROR); pInstance.setResult(ok ? MPInstance.RESULT_OK : MPInstance.RESULT_ERROR);
@ -212,6 +223,17 @@ public class MProcess extends X_AD_Process
* @return true if OK * @return true if OK
*/ */
public boolean processIt (ProcessInfo pi, Trx trx) 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) if (pi.getAD_PInstance_ID() == 0)
{ {
@ -228,7 +250,7 @@ public class MProcess extends X_AD_Process
if (Classname != null && Classname.length() > 0) if (Classname != null && Classname.length() > 0)
{ {
pi.setClassName(Classname); pi.setClassName(Classname);
ok = startClass(pi, trx); ok = startClass(pi, trx, managedTrx);
} }
else else
{ {
@ -236,7 +258,7 @@ public class MProcess extends X_AD_Process
String ProcedureName = getProcedureName(); String ProcedureName = getProcedureName();
if (ProcedureName != null && ProcedureName.length() > 0) if (ProcedureName != null && ProcedureName.length() > 0)
{ {
ok = startProcess (ProcedureName, pi, trx); ok = startProcess (ProcedureName, pi, trx, managedTrx);
} }
else else
{ {
@ -263,16 +285,17 @@ public class MProcess extends X_AD_Process
* Start Database Process * Start Database Process
* @param ProcedureName PL/SQL procedure name * @param ProcedureName PL/SQL procedure name
* @param pInstance process instance * @param pInstance process instance
* @param managedTrx false if trx is managed by caller
* see ProcessCtl.startProcess * see ProcessCtl.startProcess
* @return true if success * @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(); int AD_PInstance_ID = processInfo.getAD_PInstance_ID();
// execute on this thread/connection // execute on this thread/connection
log.info(ProcedureName + "(" + AD_PInstance_ID + ")"); log.info(ProcedureName + "(" + AD_PInstance_ID + ")");
return ProcessUtil.startDatabaseProcedure(processInfo, ProcedureName, trx); return ProcessUtil.startDatabaseProcedure(processInfo, ProcedureName, trx, managedTrx);
} // startProcess } // startProcess
@ -286,14 +309,15 @@ public class MProcess extends X_AD_Process
* @param Classname name of the class to call * @param Classname name of the class to call
* @param pi process info * @param pi process info
* @param trx transaction * @param trx transaction
* @param managedTrx false if trx is managed by caller
* @return true if success * @return true if success
* see ProcessCtl.startClass * see ProcessCtl.startClass
*/ */
private boolean startClass (ProcessInfo pi, Trx trx) private boolean startClass (ProcessInfo pi, Trx trx, boolean managedTrx)
{ {
log.info(pi.getClassName()); log.info(pi.getClassName());
return ProcessUtil.startJavaProcess(getCtx(), pi, trx); return ProcessUtil.startJavaProcess(getCtx(), pi, trx, managedTrx);
} // startClass } // startClass
@ -431,60 +455,7 @@ public class MProcess extends X_AD_Process
*/ */
public boolean processItWithoutTrxClose (ProcessInfo pi, Trx trx) public boolean processItWithoutTrxClose (ProcessInfo pi, Trx trx)
{ {
if (pi.getAD_PInstance_ID() == 0) return processIt(pi, trx, false);
{
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;
} // processItWithoutTrxClose } // 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 } // MProcess