[JAVA-715] Web support for MongoDB Created: 14/Dec/12  Updated: 15/Dec/12  Resolved: 15/Dec/12

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Angelo Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File MongoProvider.java     Text File MongoServletContextListener.java    

 Description   

It doesn't exist "official" support for using MongoDB in Web Apps. I attach my Java code with my idea to help support for Web. To use it, declare the Servlet Context Listener in the web.xml like this :

----------------------------------------------
<listener>
<listener-class>com.mongodb.web.MongoServletContextListener
</listener-class>
</listener>

<context-param>
<param-name>mongoURI</param-name>
<param-value>mongodb://localhost:27017</param-value>
</context-param>
----------------------------------------------

This listener :

  • on the contextInitialized : initialize the static variable Mongo of the MongoProvider with the "mongoURI" param-value.
  • on the contextDestroyed : close the connection of the static variable Mongo

Once this listener is started, you can use MongoProvider.getMongo() in any code (Servlet, Filter, Services, etc) to use Mongo.



 Comments   
Comment by Angelo [ 15/Dec/12 ]

Thank's Scott for this information. I could replace my MongoProvider with your Holder.

My goal is to use MongoDB in a WebApp with JEE feature with "official" classes. I thought that ServletContextListener was the best mean to initialize MongoDB and that Driver could host those classes.

But if I have understood you prefer not to have dependencies to JEE. Perhaps we could create a new project "JEE support for MongoDB" for Web and JAX-RS.

Comment by Scott Hernandez (Inactive) [ 15/Dec/12 ]

If your goal is just to use a singleton Mongo then you can use the Mongo.Holder class: http://api.mongodb.org/java/current/com/mongodb/Mongo.Holder.html

When the class-loader gets destroyed then all held instanced will be cleaned up.

Comment by Angelo [ 15/Dec/12 ]

OK I understand. But I think it should be very cool if it exists an "official" javax.* support for MongoDB. I mean :

  • Web support like this issue.
  • JAX-RS support that I have done (I will explain you if you are interested).

Perhaps the "official" javax.* support for MongoDB should be created in other project than the driver?

Comment by Jeffrey Yemin [ 15/Dec/12 ]

Lots of users run mongo in a non-J2EE environment.

Also, lots of users create Mongo instances in different ways, like via an IOC container like Guice or Spring, so the code here would not be generically useful enough to warrant inclusion.

Comment by Angelo [ 15/Dec/12 ]

Thank a lot Jeff for your answer.

What is the problem with a dependency on non-JAVA SE packages? I tell me that you must just add dependency servlet-api to your pom just to "compile" the driver.

For OSGi, you must just import package "javax.servlet" as "optionnal".

I had intention to create a new issue for JAX-RS support for MongoDB, but if you don't want add some dependencies, I will not create it. I'm writing articles in my blog to explain that.

Comment by Jeffrey Yemin [ 15/Dec/12 ]

Thank you for this contribution, and let us know if there is anything else you'd like to see in the driver.

Comment by Jeffrey Yemin [ 15/Dec/12 ]

I don't think we will add this to the driver, as it will create a dependency on non-JAVA SE packages.

Generated at Thu Feb 08 08:52:54 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.