Skip to content

Commit 95a9332

Browse files
committed
[jooby 1.x - hibernate]: classcast exception when connection can't be acquire fix #1473
1 parent c559d20 commit 95a9332

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

modules/jooby-hbm/src/main/java/org/jooby/internal/hbm/OpenSessionInView.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,25 @@ public class OpenSessionInView implements Filter {
215215
@Override
216216
public void handle(final Request req, final Response rsp, final Chain chain) throws Throwable {
217217
RootUnitOfWork uow = (RootUnitOfWork) req.require(UnitOfWork.class);
218-
// start transaction
219-
uow.begin();
220218

221-
rsp.after(after(uow));
219+
try {
220+
// start transaction
221+
uow.begin();
222+
223+
rsp.after(after(uow));
222224

223-
rsp.complete(complete(uow));
225+
rsp.complete(complete(uow));
224226

225-
// move next
226-
chain.next(req, rsp);
227+
// move next
228+
chain.next(req, rsp);
229+
} catch (Throwable x) {
230+
try {
231+
uow.setRollbackOnly().end();
232+
} catch (Throwable suppressed) {
233+
x.addSuppressed(suppressed);
234+
}
235+
throw x;
236+
}
227237
}
228238

229239
private static Route.Complete complete(final RootUnitOfWork uow) {

modules/jooby-hbm/src/main/java/org/jooby/internal/hbm/RootUnitOfWork.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,15 @@ public UnitOfWork commit() {
243243
return this;
244244
}
245245
if (!readOnly) {
246-
log.debug("flusing session: {}", oid(session));
246+
log.debug("flushing session: {}", oid(session));
247247
session.flush();
248248
} else {
249-
log.debug("flusing ignored on read-only session: {}", oid(session));
249+
log.debug("flushing ignored on read-only session: {}", oid(session));
250250
}
251251
active(session, trx -> {
252-
log.debug("commiting transaction: {}(trx@{})", oid(session), oid(trx));
252+
log.debug("committing transaction: {}(trx@{})", oid(session), oid(trx));
253253
trx.commit();
254-
}, trx -> log.warn("unable to commit inactive transaction: {}(trx@{})", oid(session), oid(trx)));
254+
}, trx -> log.debug("unable to commit inactive transaction: {}(trx@{})", oid(session), oid(trx)));
255255
return this;
256256
}
257257

@@ -277,7 +277,7 @@ public UnitOfWork rollback() {
277277
active(session, trx -> {
278278
log.debug("rollback transaction: {}(trx@{})", oid(session), oid(trx));
279279
trx.rollback();
280-
}, trx -> log.warn("unable to rollback inactive transaction: {}(trx@{})", oid(session), oid(trx)));
280+
}, trx -> log.debug("unable to rollback inactive transaction: {}(trx@{})", oid(session), oid(trx)));
281281
return this;
282282
}
283283

@@ -303,16 +303,19 @@ public void end() {
303303
commit();
304304
}
305305
} finally {
306-
if (readOnly) {
307-
setConnectionReadOnly(false);
308-
}
306+
try {
307+
if (readOnly) {
308+
setConnectionReadOnly(false);
309+
}
309310

310-
String sessionId = oid(session);
311-
log.debug("closing session: {}", sessionId);
312-
Try.run(session::close)
313-
.onFailure(x -> log.error("session.close() resulted in exception: {}", sessionId, x))
314-
.onSuccess(() -> log.debug("session closed: {}", sessionId));
315-
unbind(session.getSessionFactory());
311+
String sessionId = oid(session);
312+
log.debug("closing session: {}", sessionId);
313+
Try.run(session::close)
314+
.onFailure(x -> log.error("session.close() resulted in exception: {}", sessionId, x))
315+
.onSuccess(() -> log.debug("session closed: {}", sessionId));
316+
} finally {
317+
unbind(session.getSessionFactory());
318+
}
316319
}
317320
}
318321

0 commit comments

Comments
 (0)