Monday 21 July 2014

Solution for InvalidVersionException (Order Version Error in ATG)

While updating the order we might get atg.commerce.order.InvalidVersionException: This order (oXXXXXXX) is out of date.
To fix this problem you must always make sure to update an order within a transaction using the same version. 
In general, the design pattern for updating an order is as follows:
1.      Acquire lock-manager write lock on profile id from the /atg/commerce/order/LocalLockManager
2.      Begin Transaction
3.      Synchronize on the Order object.
4.      Modify Order
5.      Call ((OrderImpl) pOrder).updateVersion();
6.      Call OrderManager.updateOrder()
7.      Release Order synchronization
8.      End Transaction

9.      Release lock-manager write lock on profile id from the /atg/commerce/order/LocalLockManager


Sample Code:

TransactionManager tm = getOrderManager().getTransactionManager();
TransactionDemarcation td = new TransactionDemarcation ();
boolean rollback = false;
try {
                td.begin(mTransactionManager, TransactionDemarcation.REQUIRES_NEW);
                SivarCustomOrderImpl sivarCustomOrder=(SivarCustomOrderImpl) getSivarOrderManager().loadOrder(orderId);
                synchronized (sivarCustomOrder) {
                                sivarCustomOrder.setOpportunityStatus(SivarCartConstants.OPP_STATUS_PENDING);
((SivarCustomOrderImpl) sivarCustomOrder).updateVersion();
                                getSivarOrderManager().updateOrder(sivarCustomOrder);
                }
} catch (TransactionDemarcationException re) {
                rollback=true;
                if (isLoggingError()) {
                                logError(re.getMessage());
                }
}  finally {
                try {
                                td.end(rollback);
                } catch (TransactionDemarcationException e) {
                                if (isLoggingError()) {
                                                logError("TransactionDemarcationException while transaction end: " + e.getMessage(), e);
                                }
                }
}

Note: It's a sample code for your reference.