简体   繁体   English

您如何将数据库对象从对象池注入并处理到资源中?

[英]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.

相关问题 您如何在NoSql数据库中对客户&gt;订单&gt; ordertem&gt;产品进行建模? - How would you model customer > order > ordertem > product in NoSql database? 在 NoSQL 中,您如何处理对常见依赖数据的大量更新? - In NoSQL, how do you handle massive updates to common dependant data? 如何使用CouchRest Ruby Gem从CouchDB视图中检索对象 - How to retrieve an object from a CouchDB view using the CouchRest Ruby Gem 如何使用同一个CouchDB实例中的许多应用程序来处理`_users`数据库中的用户? - How to handle users in `_users` database with many applications in the same CouchDB instance? 您将如何使用文档存储(例如CouchDB,Redis,MongoDB,Riak等)构建博客 - How would you architect a blog using a document store (such as CouchDB, Redis, MongoDB, Riak, etc) PouchDB从对象数组中发出对象 - PouchDB emit object from an array of objects 从Cloudant中的持久json对象中删除属性 - Remove a property from a persisted json object in cloudant 从 Scala 中的 CouchDB 检索简单的 object - Simple object retrieval from CouchDB in Scala 如何使用Rails中具有一些常见属性/属性的其他模型创建模型的对象? - How can I create a model's object from a different model with some common properties/attributes in Rails? 基于文档的数据库和CouchDB如何处理ID引用? - How does a document-based database and CouchDB in particular handle ID references?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM