0

I'm writing my first Apache Wink / JAX-RS web service, and I want to connect to MongoDB. Is the best way to do this to create a new connection each time a request is received and close it at the end of the request, or is there some way to have a persistent connection that I can access from my service?

The latter seems a much more sensible approach to me, but some Googling has failed to turn up much information on the subject and how I could achieve that.

Any pointers in the right direction would be appreciated!

James Baker
  • 1,143
  • 17
  • 39
  • MongoClient is thread safe, uses connection pooling, and is meant to be reused, as mentioned in [the javadoc](http://api.mongodb.org/java/3.0/com/mongodb/MongoClient.html?_ga=1.261567117.1896281156.1441811615) – Paul Samsotha Sep 09 '15 at 15:15
  • I get that, but how do I use it from within a Wink application? – James Baker Sep 10 '15 at 07:31
  • You might want to create a service wrapper around some data access operations. If you have access to some DI framework, just inject the service into your resource classes. Or if you aren't using any DI, maybe create a singleton or if you don't like the idea of ta singleton, you can probably make something work with a `ContextResolver` as mentioned [here](http://stackoverflow.com/q/32478159/2587435) – Paul Samsotha Sep 10 '15 at 07:44

0 Answers0