I guess I found an easy (but maybe crappy) way of doing it :
Code:
public class MyFlushEventListener extends DefaultFlushEventListener {
private static final long serialVersionUID = -6356392955121838101L;
private static final Log LOG = LogFactory.getLog(MyFlushEventListener.class);
// will load it from a config file
private static final int MAX_RETRY = 3;
/**
* @see org.hibernate.event.def.DefaultFlushEventListener#onFlush(org.hibernate.event.FlushEvent)
*/
public void onFlush(FlushEvent event) throws HibernateException {
int tryNo = 1;
boolean doneCorrectly = false;
while (tryNo <= MAX_RETRY && !doneCorrectly) {
try {
super.onFlush(event);
doneCorrectly = true;
tryNo++;
} catch (Exception e) {
StringBuffer buf = new StringBuffer("Can't flush the Hibernate session.");
buf.append(MAX_RETRY - tryNo);
buf.append(" try(s) to go.");
LOG.warn(buf.toString(), e);
}
}
if (!doneCorrectly) {
StringBuffer buf = new StringBuffer("Couldn't flush the Hibernate session after ");
buf.append(MAX_RETRY);
buf.append(" try(s). Check WARN in the log.");
String message = buf.toString();
LOG.error(message);
throw new HibernateException(message);
}
}
}
Does anyone have a advice about that code?
edit: thinking of it, there is still the Transaction opening problem. If I try to open a transaction and the DB is off line, I will be thrown away...