Question

i've been thinking about implementing a object pool for jcouchdb objects for jersey. Now i am asking myself what would be the best way to deliver a jcouchdb instance to the resource endpoints.

I expect the pool to have a method for requesting an jcouchdb object and for releasing it so that it can be reused.

My first idea was to implement a InjectableProvider as a singleton an use a annotation in the resource endpoint to "grab" it. The InjectableProvider then returns an jcouchdb object from the object pool and marks it as busy. How can i release the jcouchdb object after I've used it? And i would request a jcouchdb object for every resource endpoint instance even if i never need it?! (don't know when the annotated objects get instantiated)

Another idea i was thinking about was to attach the object pool to the servlet context (with set attribute).

Any other ideas?

I am basically a bit confused when i comes to shared resources and jersey. Hopefully someone can clear things up for me.

Thanks

Was it helpful?

Solution

If you do exactly as you just said, your code would look like this:

public class MyResource{
     @GET
     @RequestMapping("/bleh")
     public Response getValue(@Context JCouchDBObject object){
        //manipulate object
     }
}

@Provider
public class MyProvider extends InjectableProvider<Context, Parameter>{
   public Injectable<JCouchDBObject> getInjectable(ComponentContext context, Context hp, Parameter param) {
        //GetObject and return
   }
}

I've never worked with JCouchDB, but unless each object is linked to the DB connection pool - there is nothing to manually release - all of this will be handled for you.

But: This is not what the InjectableProvider was designed for. Typically, the InjectableProvider will be used to create and resolve some sort of request object (such as the JCouchDBObject's ID, etc). Then you should use a service to collect the JCouchDBObject and handling any manually release there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top