I'm using the technique where you open a transaction....and then close it with an app filter. I'm using the example code from the Caveat emptor app.
Code:
public String makePersistent(Metric metric) throws InfrastructureException
{
try {
HibernateUtil.getSession().saveOrUpdate(metric);
HibernateUtil.getSession().flush();
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
} catch (Exception e) {
throw new InfrastructureException(e);
}
return metric.getMetricId().toString();
}
The problem I ran into (before I added the flush statement) was that database errors seemed to be ignore. If a user tried to add a duplicate record....the makePersistent method does not catch the error.
I solved this by adding the flush()...but I'm wondering if I've correctly solved the problem.
In other words...until you close the transaction (which I'm doing with an app filter) you don't get your error. Since you don't close the transaction in the "makePersistent" -- you don't catch the error until much later.
By adding "flush" I then get my error trapped they way I expect, but I'm wondering if I'm breaking something else -- something that I don't understand and will haunt me later.
Thanks,
Lee