mongodb scala driver casbah 是否自动管理连接池
Does mongodb scala driver casbah manage connection pool automatically
我正在使用 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()
}
我对上面的代码有一些疑问
这段代码是否使用了连接池
此代码是否重用 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")
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
,移到上层,一直使用。当应用程序停止时关闭它。
我正在使用 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()
}
我对上面的代码有一些疑问
这段代码是否使用了连接池
此代码是否重用 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")
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
,移到上层,一直使用。当应用程序停止时关闭它。