Start and End a Synchronous Business Transaction

The following example shows code that starts a Business Transaction called 'Checkout' whenever the method checkout() is called. The Business Transaction ends when the method returns. Encapsulating the method body in a try or finally block ensures that you end the Business Transaction even if the method itself throws an exception or otherwise terminates without reaching the end.

public String checkout(List<ItemOrders> orders) {
Transaction transaction = null;
try {
transaction = AppdynamicsAgent.startTransaction("Checkout", null, EntryTypes.POJO, false);
/*******************
* Method Body Here
*******************/
} finally {
if (transaction != null) {
transaction.end();
}
}
}

Alternatively, you can use try-with-resources pattern:

public String checkout(List<ItemOrders> orders) {
try (Transaction transaction = AppdynamicsAgent.startTransaction("Checkout", null, EntryTypes.POJO, false)) {
/*******************
* Method Body Here
*******************/
}
}

In this case, the Business Transaction ends when the try block closes.