I am trying to manage a competing resource (as in: a database session) while programming a RESTful web application in Jersey. I usually write code as follows:
Session session = getSession();
try {
doWork();
session.commit();
} finally {
session.rollback();
session.release();
}
Now with Jersey I have this resource:
@Path("/")
class MyResource {
@Path("{child}") public Child getChild(...) {
return child;
}
}
The problem is that I need to get the session in getChild (), but I cannot be sure that I correctly released it after the work was done, since I already transferred control to the web application.
The child also needs access to the session, so I cannot encapsulate all the work in one way:
class Child {
@Path("{subchild}") public Subchild getSubchild(...) {
return new Subchild(session.get(...));
}
}
-, , . MyResource, , , , , . ExceptionMapper, , ExceptionMapper, , / ..
" " ? , , . FileInputStream ?