Hi, All
In OFBiz, there is a class named TransactionUtil which helps with some
common transaction tasks
I want to know how to use it, so I wrote some code below
public static Map processCustomerApprove(DispatchContext dctx, Map
context) {
Map result = ServiceUtil.returnSuccess();
GenericDelegator delegator = dctx.getDelegator();
boolean beganTransaction = false;
try {
beganTransaction = TransactionUtil.begin();
Workflow workflow =
WorkFlowFactory.getWorkFlow(UserUtil
.getUserName());
Long wfid = (Long) context.get("wfid");
int actionNum = Integer.parseInt((String)
context.get("action"));
Debug.logInfo("workflow=" + wfid +
",actionnumber=" + actionNum,
module);
String opinion = (String)
context.get("opinion");
String conclusion = (String)
context.get("conclusion");
Debug.logInfo("Conclusion=" + conclusion +
"\nopinion=" + opinion,
module);
Long id = new
Long(delegator.getNextSeqId("Opinion"));
Debug.logInfo("id = " + id, module);
GenericValue opinionValue =
delegator.makeValue("Opinion", UtilMisc
.toMap("id", id));
opinionValue.set("wfid", wfid);
opinionValue.set("approver",
UserUtil.getUserName());
opinionValue.set("conclusion", conclusion);
opinionValue.set("opinion", opinion);
delegator.create(opinionValue);
Map inputs = new HashMap();
inputs.put("conclusion", conclusion);
workflow.doAction(wfid, actionNum, inputs);
Debug.logInfo("workflow enters next step",
module);
if ("Agree".equals(conclusion) && actionNum ==
2) {
//
EntityCondition wfidCondition =
EntityCondition.makeCondition(
"wfid",
EntityOperator.EQUALS, wfid);
List customerInfoHistoryList =
delegator.findList(
"CustomerinfoHistory",
wfidCondition, null, UtilMisc
.toList("id DESC"), null, false);
GenericValue value =
EntityUtil.getFirst(customerInfoHistoryList);
//copy customer info from table
customerinfohistory to customerinfo
//value.remove("wfid");
value.remove("id");
Long customerid =
CcbUtils.getNextSeqId(delegator, "customerinfo");
Debug.logInfo("id = " + customerid,
module);
GenericValue customerInfo =
delegator.makeValue("Customerinfo",
UtilMisc.toMap("id",
id));
customerInfo.setFields(value);
delegator.create(customerInfo);
}
} catch (Exception e) {
try {
TransactionUtil.rollback(beganTransaction, "something wrong in the
operation", e);
} catch (GenericTransactionException e1) {
e1.printStackTrace();
}
}finally {
try {
TransactionUtil.commit();
} catch (GenericTransactionException e) {
e.printStackTrace();
}
}
return result;
}
}
When I debug, TransactionUtil.begin() returns false,
And an error happens when running customerInfo.setFields(value) (I know
why),
, so jvm catches the exception, TransactionUtil.rollback is invoked.
Because beganTransaction is false, it doesn't roll back.
When program ends, transaction doesn't work eventually.
Could you tell me how I should do to keep transaction consistent?
Best Regards,
Jack Liu