You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ofbiz.apache.org by Jack Liu <ja...@aicent.com> on 2009/04/17 09:41:08 UTC

transaction consistency in ofbiz

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 


RE: transaction consistency in ofbiz

Posted by Jack Liu <ja...@aicent.com>.
Yes,I am sure because I debug step by step.

GenericEntity.set() throws IllegalArgumentException ;

I use OFBiz trunk version

________________________________

From: Scott Gray [mailto:scott.gray@hotwaxmedia.com] 
Sent: 2009年4月17日 16:28
To: user@ofbiz.apache.org
Subject: Re: transaction consistency in ofbiz

 

Are you sure that a transaction rollback is actually invoked?  GenericEntity.setFields() calls GenericEntity.set() which has a statement in it that logs an exception but doesn't actually throw one.

 

Regards

Scott

 

HotWax Media

http://www.hotwaxmedia.com <http://www.hotwaxmedia.com/> 

 

On 17/04/2009, at 7:41 PM, Jack Liu wrote:





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 

 


Re: transaction consistency in ofbiz

Posted by Scott Gray <sc...@hotwaxmedia.com>.
Are you sure that a transaction rollback is actually invoked?   
GenericEntity.setFields() calls GenericEntity.set() which has a  
statement in it that logs an exception but doesn't actually throw one.

Regards
Scott

HotWax Media
http://www.hotwaxmedia.com

On 17/04/2009, at 7:41 PM, Jack Liu wrote:

> 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
>