A typical service class method looks like the following:
public void updateSomething(TransactionConfig tc, ...)
{
try
{
TransactionHelper.begin(tc);
....
TransactionHelper.commit(tc);
}
catch (Exception e)
{
TransactionHelper.rollback(tc);
throw e;
}
}
OR
public xxx getSomething(TransactionConfig tc, ...)
{
try
{
TransactionHelper.begin(tc);
....
return xxx;
}
finally
{
TransactionHelper.rollback(tc);
}
}
The object TransactionConfig can be used to control how transaction is to be performed in the service call, e.g.
// Don't start a transaction. getSomething(new TransactionConfig().doNotStartTransaction(), xxx); // Use read-replica. This is recommended for retrieving past data set such as reports. // Do NOT use read-replica for updates. // This is only applicable for MySQL with read-replica setup. getSomething(new TransactionConfig().useReadReplica(), xxx); // Set timeout to 30min (much longer than the default). getSomething(new TransactionConfig().timeout30min(), xxx); // Don't start a transaction AND use read-replica and a 30min timeout. getSomething(new TransactionConfig().doNotStartTransaction().useReadReplica().timeout30min(), xxx); // No specific transaction configuration - apply default. getSomething(new TransactionConfig(), xxx); getSomething(null, xxx); // or simply pass null.