Skip to main content

MongoDBClient for Java Application

Singleton MongoDBClient for Java Application

Reason to recommend not to close MongoDB driver connection:-

The MongoDB Java driver provides a MongoClient class for the connection of your Java application with the MongoDB database.

           The mongodb java client maintains (example below) a connection pool, which is relatively expensive to set up [even in faster network it takes 100 of milliseconds, apart from that each driver connection would run in separate thread and it will consume memory], so one should reuse the MongoClient instance across the lifetime.
            A Connection Pool is a cache of database connections maintained by the driver so that connections can be reused when new connections to the database are required. To reduce the number of connection pools created by your application, it is recommend to call MongoClient.connect once.

So it becomes obvious to create one instance of the MongoClient and to reuse it in every request of your application and not to close it.

MongoDBServices wrapper class:-

  1. I have used singleton design pattern in java by using enum.
  2. I kept separate properties file which has parameters to connect with MongoDB server.
  3. Declaration and Initialization of MongoClient.
  4. Use method for database operation.

Singleton pattern in Java by using enum


Parameters to connect with MongoDB server

Parameters to connect with MongoDB server
Properties file holding details for connection.

Declaration and Initialization of MongoClient

Declaration and Initialization of MongoClient
Declaration and Initialization of MongoClient

Use method for database operation

Database operation using singleton method
Database operation using singleton method


Resources:-

http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java

http://stackoverflow.com/questions/26914320/closing-mongodb-java-connection

Comments

Popular posts from this blog

How to Open Browser in Incognito / InPrivate mode using Selenium / WebDriver

How to Open Chrome Browser in Incognito mode using Selenium / WebDriver ?          We will use Chrome Drivers Capabilities & ChromeOptions to open Chrome browser in incognito mode. To be precise, we have to use argument --incognito  for ChromeOption as shown in below example- How to Open Firefox Browser in Incognito / Private mode using Selenium / WebDriver ?     We will use Firefox Profile   to open Firefox in private mode. To be precise, we will set browser.private.browsing.autostart in firefox profile preference. How to Open Internet Explorer (IE) Browser in InPrivate mode using Selenium / WebDriver ?       We will use IE Driver Capabilities to open IE in InPrivate mode. To be precise we will use FORCE_CREATE_PROCESS capability along with IE_SWITCHES to which parameter would be -private This is all for now. Cheers!!

History Of Selenium WebDriver

I have assumed that you have already read my past blog entry, Introduction to Selenium WebDriver and here on the quest to find out technical answer of question - what selenium is? Before getting to the answer, I want you to go though the below info-graphics of history of Selenium WebDriver.  History of Selenium Now that you are aware of history, Can you answer what Selenium is? Selenium is set of tools which includes :- Selenium IDE Selenium RC WebDriver Selenium Grid So meaning of Selenium depends upon the context, c iting the man himself - Simon Stewart here - Unfortunately, the Selenium project uses a lot of jargon. Selenium Core is the heart of the original Selenium implementation, and is a set of Javascript scripts that control the browser. This is sometimes referred to as "Selenium" and sometimes as "Core". Selenium RC was the name given to the language bindings for Selenium Core, and is commonly, and confusingly, referred to as just "Selenium"