Hiya. I'm having some trouble rolling back a transaction - it fails to work as expected. In my code, I'm reading in a bunch of data and creating a set of persistent objects. Sometimes the data is malformed, and the import utility tosses an IllegalStateException. I catch the exception and roll back the transaction, but look in the database and see that the partial data exists therein! Here's my code sample:
Code:
Session session = objectContext.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// delete any studies that already exist that have the same name
String name = studyElement.attributeValue("name");
List studies = session.find("select from Study as study where study.name = ?", name, Hibernate.STRING);
if (!studies.isEmpty()) {
for (int i = 0; i < studies.size(); i++) {
Study study = (Study)studies.get(i);
log.debug("deleting study named: " + study.getName());
session.delete(study);
}
}
tx.commit();
tx = null;
tx = session.beginTransaction();
Study study = new Study();
// set study properties
session.save(study);
// create study children and save them, associate with study, etc., etc.
tx.commit();
} catch (Exception e) {
log.error("Error importing data", e);
if (tx != null) {
log.debug("Rolling back...");
tx.rollback();
log.debug("done rolling back: "+tx.wasRolledBack());
}
throw e;
} finally {
session.close();
}
Any clues as to what I'm doing wrong? fwiw, I'm running this against a mysql database, let's see, 3.23.53 on a win32 box.