mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
not a good idea
This commit is contained in:
@@ -16,32 +16,24 @@ public abstract class UnitOfWork<T> {
|
|||||||
protected abstract T runInSession() throws Exception;
|
protected abstract T runInSession() throws Exception;
|
||||||
|
|
||||||
public T run() {
|
public T run() {
|
||||||
// if newSession is false, we already are in a unit of work and roll back/commit will happen in the wrapping unit of work
|
final Session session = sessionFactory.openSession();
|
||||||
boolean newSession = !ManagedSessionContext.hasBind(sessionFactory);
|
if (ManagedSessionContext.hasBind(sessionFactory)) {
|
||||||
|
throw new IllegalStateException("Already in a unit of work!");
|
||||||
final Session session = newSession ? sessionFactory.openSession() : sessionFactory.getCurrentSession();
|
}
|
||||||
T t = null;
|
T t = null;
|
||||||
try {
|
try {
|
||||||
if (newSession) {
|
ManagedSessionContext.bind(session);
|
||||||
ManagedSessionContext.bind(session);
|
session.beginTransaction();
|
||||||
session.beginTransaction();
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
t = runInSession();
|
t = runInSession();
|
||||||
if (newSession) {
|
commitTransaction(session);
|
||||||
commitTransaction(session);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (newSession) {
|
rollbackTransaction(session);
|
||||||
rollbackTransaction(session);
|
|
||||||
}
|
|
||||||
this.<RuntimeException> rethrow(e);
|
this.<RuntimeException> rethrow(e);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (newSession) {
|
session.close();
|
||||||
session.close();
|
ManagedSessionContext.unbind(sessionFactory);
|
||||||
ManagedSessionContext.unbind(sessionFactory);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user