In this case, you should use a template template, for example:
public abstract class BaseClass
{
public Object executeTrans(Template template)
{
tx.begin();
template.doTransaction();
tx.commit();
}
}
public interface Template
{
public void doTransaction();
}
public childClass extends BaseClass
{
public List<String> getInfoFromDB()
{
executeTrans(
new Template()
{
public void doTransaction()
{
...do get info from DB here.
}
}
);
}
public void saveToDB()
{
executeTrans(
new Template()
{
public void doTransaction()
{
...do save to DB here.
}
}
);
}
}
Speaking of this, I would advise using the Spring JDBC template classes instead of riding on their own - they have been tried and tested and solved the problems you encountered with nested transactions, etc.
source
share