gpt4 book ai didi

java - mongodb scala 驱动程序 casbah 是否自动管理连接池

转载 作者:行者123 更新时间:2023-11-30 02:53:29 24 4
gpt4 key购买 nike

我正在使用 mongo cashbah Scala 驱动程序,我想在我的代码中使用连接池,但我不确定我的代码是否使用连接池,我也读到我们只需要创建一次 MongoClient 实例并再次重用它,所以我不确定我的代码是否重用它或每次都创建一个新实例,请指导我这是我的代码

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()
}

我对上述代码有一些疑问

  1. 此代码是否使用连接池

  2. 此代码是否重用 mongoClient 实例或其创建新实例每次实例

  3. 我是否需要在每次查询后关闭连接,如果不需要,则何时关闭我应该关闭连接

请指导我

更新

我对代码进行了以下更改

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()
}

此更新的代码是重用 mongo 连接还是每次都创建一个新实例,请指导我

最佳答案

请参阅此 question 。因此,每当您创建 MongoConnection 时,实际上都会在后台创建连接池。

关于您的特定代码:每次您想要获取记录时,您都会创建MongoConnection。将其分配给val,将其移至上层并一直使用它。当应用程序停止时关闭它。

关于java - mongodb scala 驱动程序 casbah 是否自动管理连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37939319/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com