0
我正在使用Oracle DB和Hibernate的JavaEE/JPA管理事務,並且需要實現某種嵌套事務。據我所知,這種東西不是開箱即用的,但我應該可以使用保存點來達到這個目的。如何使用Hibernate Session.doWork(...)進行保存點/嵌套事務?
正如https://stackoverflow.com/a/7626387/173689建議我嘗試了以下內容:
@Transactional(value = TxType.REQUIRES_NEW)
public boolean doImport(Import importer, Row row) throws ImportRowFailedException {
// stripped code ...
// We need to try different possibilities from which one may succeed...
// ...former failures must be rolled back!
for (Possibility poss : importer.getPossibilities()) {
if (this.tryPossibility(poss, row)) break;
}
// stripped code ...
}
public boolean tryPossibility(Possibility possibility, Row row) {
try {
Session session = this.em.unwrap(Session.class);
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
Savepoint before = connection.setSavepoint();
if (!possibility.importRow(row)) {
connection.rollback(before);
throw new ImportRowFailedException();
}
}
});
}
catch (ImportRowFailedException ex) {
return false;
}
return true;
}
在connection.rollback(before)
我得到以下異常:
Caused by: java.sql.SQLException: IJ031040: Connection is not associated with a managed connection: [email protected]
at org.jboss.jca.adapters.jdbc.WrappedConnection.lock(WrappedConnection.java:164)
at org.jboss.jca.adapters.jdbc.WrappedConnection.rollback(WrappedConnection.java:883)
我該怎麼處理呢?