I am using mongo cashbah Scala driver i want to use connection pooling in my code but i am not sure my code is using connection pooling or not also i have read that we need to create MongoClient instance only once and reuse it again so i am not sure my code is reusing it or creating a new instance every time please guide me here is my code
object MongoFactory {
val log = LoggerFactory.getLogger(this.getClass)
val config = ConfigFactory.load()
var client:MongoClient=null
private var SERVER:ServerAddress = {
val hostName=config.getString("db.hostname")
val port=config.getString("db.port").toInt
new ServerAddress(hostName,port)
}
private var DATABASE:String = config.getString("db.dbname")
def createConnection: MongoClient = {
log.info("server "+SERVER + "DATABASE" +DATABASE)
client=MongoClient(SERVER)
client
}
def getConnection : MongoClient = {
log.debug("In method getConnection")
if(client==null)
{
log.debug("mongoclient instance is null")
client=createConnection
log.debug("mongoclient is {}",client)
log.debug("Leaving method getConnection with returned value {}",client)
client
}
else
{
log.debug("Leaving method getConnection with returned value {}",client)
client
}
}
def getCollection(conn: MongoClient,collectionName:String): MongoCollection = {
conn(DATABASE)(collectionName)
}
def closeConnection(conn: MongoClient) {
conn.close
}
class Abc
{
def readAll()
{
var connection=MongoFactory.getConnection
var collection=MongoFactory.getCollection(connection, "User")
val cursor=collection.find()
while(cursor.hasNext)
{
// here fetching the data from database
}
MongoFactory.closeConnection(connection)
}
def readById()={
var connection=MongoFactory.getConnection
var collection=MongoFactory.getCollection(connection, "User")
val cursor=collection.find(q.get)
while(cursor.hasNext)
{
// here fetching the data from database
}
MongoFactory.closeConnection(connection)
}
}
object test extends App {
MongoFactory.getConnection
val abc=new Abc()
abc.readAll()
abc.readById()
}
I have some questions regarding the above code
does this code is using connection pooling
does this code reuses the mongoClient instance or its creating new instance every time
do i need to close connection after every query and if not when should i close the connection
please guide me
UPDATE
i have made following changes to the code
object MongoFactory {
val log = LoggerFactory.getLogger(this.getClass)
val config = ConfigFactory.load()
var client:MongoClient=null
private var SERVER:ServerAddress = {
val hostName=config.getString("db.hostname")
val port=config.getString("db.port").toInt
new ServerAddress(hostName,port)
}
private var DATABASE:String = config.getString("db.dbname")
val connectionMongo = MongoConnection(SERVER)
val collectionMongo = connectionMongo(DATABASE)("artGroup")
}
class Abc
{
def readAll()
{
val cursor=collectionMongo.find()
while(cursor.hasNext)
{
// here fetching the data from database
}
}
def readById()={
val cursor=collectionMongo.find(q.get)
while(cursor.hasNext)
{
// here fetching the data from database
}
}
}
object test extends App {
val abc=new Abc()
abc.readAll()
abc.readById()
}
does this updated code is reusing the mongo connection or its creating a new instance every time please guide me