数据库连接池ConnectionPool

package com.bnls.test.pool

import java.sql.{Connection, ResultSet}
import com.jolbox.bonecp.{BoneCP, BoneCPConfig}
import org.slf4j.LoggerFactory

object ConnectionPool {

val logger = LoggerFactory.getLogger(this.getClass)

private val connectionPool = {
try {
Class.forName("com.mysql.jdbc.Driver")
val config = new BoneCPConfig()
config.setJdbcUrl("jdbc:mysql://10.60.81.185:3311/intfdb")
config.setUsername("intfopr")
config.setPassword("Qhcmysql%1234")
config.setLazyInit(true)

config.setMinConnectionsPerPartition(3)
config.setMaxConnectionsPerPartition(5)
config.setPartitionCount(5)
config.setCloseConnectionWatch(true)
config.setLogStatementsEnabled(false)

Some(new BoneCP(config))
} catch {
case exception: Exception =>
logger.warn("Error in creation of connection pool" + exception.printStackTrace())
None
}
}

def getConnection: Option[Connection] = {
connectionPool match {
case Some(connectionPool) => Some(connectionPool.getConnection)
case None => None
}
}

def closeConnection(connection: Connection): Unit = {
if (!connection.isClosed) {
connection.close()
}
}

}

转载于:https://www.cnblogs.com/heguoxiu/p/10064311.html

你可能感兴趣的:(数据库连接池ConnectionPool)