[英]How would you inject and handle a database object from an object pool into a resource?
i've been thinking about implementing a object pool for jcouchdb objects for jersey. 我一直在考虑为jersey的jcouchdb对象实现对象池。 Now i am asking myself what would be the best way to deliver a jcouchdb instance to the resource endpoints. 现在,我在问自己,将jcouchdb实例传递到资源端点的最佳方法是什么。
I expect the pool to have a method for requesting an jcouchdb object and for releasing it so that it can be reused. 我希望该池具有一种请求jcouchdb对象并释放它的方法,以便可以重用它。
My first idea was to implement a InjectableProvider as a singleton an use a annotation in the resource endpoint to "grab" it. 我的第一个想法是将InjectableProvider实现为单例,并在资源端点中使用批注“捕获”它。 The InjectableProvider then returns an jcouchdb object from the object pool and marks it as busy. 然后,InjectableProvider从对象池返回jcouchdb对象,并将其标记为忙。 How can i release the jcouchdb object after I've used it? 使用完后如何释放jcouchdb对象? And i would request a jcouchdb object for every resource endpoint instance even if i never need it?! 我会为每个资源端点实例请求一个jcouchdb对象,即使我永远不需要它? (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). 我正在考虑的另一个想法是将对象池附加到servlet上下文(具有set属性)。
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 谢谢
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. 我从来没有使用过JCouchDB,但是除非每个对象都链接到数据库连接池-没有要手动释放的内容-所有这些都将为您处理。
But: This is not what the InjectableProvider was designed for. 但是:这不是InjectableProvider设计的目的。 Typically, the InjectableProvider will be used to create and resolve some sort of request object (such as the JCouchDBObject's ID, etc). 通常,InjectableProvider将用于创建和解析某种请求对象(例如JCouchDBObject的ID等)。 Then you should use a service to collect the JCouchDBObject and handling any manually release there. 然后,您应该使用服务来收集JCouchDBObject并在那里处理任何手动发布。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.