Exception Handling / Resource Management in JAX-RS Jersey

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(); // doesn't hurt after commit
  session.release(); // or whatever
}

Now with Jersey I have this resource:

@Path("/")
class MyResource {
  @Path("{child}") public Child getChild(...) {
    // how do I manage my session here ???
    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 ?

+3
2

REST - . getChild, , . , , :

@Path("/{childId}")
class ChildResource {  

    @GET
    public Child getChild(@PathParam("childId") String childId) {    
        //Really, move this into a data access object
        Session session = getSession();
        try {  
            doWork();  
            session.commit();
        } finally {  
            session.rollback(); 
            // doesn't hurt after commit  
            session.release(); 
            // or whatever
        }
        return child;  
    }
}
0

Spring. . .

* , ,

0

Source: https://habr.com/ru/post/1709782/


All Articles