1.this is how my dao class look like where the transaction code keep repeat for every method. possible to put these snippet code in super class so that i do no need to repeat below code all the time? any elaborate how to do this?
2.if there is a need to put the snippet in super.class. should the super.class be static?
for (int i = 0; i < NUM_RETRIES; i++) {
pm.currentTransaction().begin();
<all my code will be here>
try {
pm.currentTransaction().commit();
break;
} catch (JDOCanRetryException ex) {
if (i == (NUM_RETRIES - 1)) {
throw ex;
}
}
}
Addressing only the “extract to superclass” issue, you could:
In your base class put:
In your derived class, redefine doTransaction method
Please adjust the return types and parameters accordingly.
Note that there’s nothing static (static methods cant be overridden), its just an implementation of the template method pattern.