0

pool2我在使用 Apache库的 Scala 中有以下代码。要池化的对象是OlapConnection(一个olap4j类,类似于 SQL 连接)。

问题是当池对象的数量超过最大值时,我无法让池自动关闭连接。

如果我将一个对象返回到pool.returnObject触发passivateObject. 如果我在 中关闭连接passivateObject,我会在每次返回对象时关闭它,这不是我想要的 - 我需要缓存打开的连接。如果我关闭中的连接passivateObject,那么它将永远不会关闭。

如何使这项工作?

斯卡拉代码:

class OlapConnectionUtil (val pool: ObjectPool[OlapConnection]) {
     def connect = {
        pool.borrowObject
     }
     def close(olapConnection: OlapConnection) = {
       pool.returnObject(olapConnection)
     }
}

class OlapConnectionFactory extends BasePooledObjectFactory[OlapConnection] {

  override def create = {           
      val connectionString = "jdbc:mondrian:Jdbc=jdbc:mysql://localhost:3306/foodmart?" + 
            "user=x&password=x;Catalog=FoodMart.xml;JdbcDrivers=com.mysql.jdbc.Driver"           
       val connection = DriverManager.getConnection(connectionString)
       connection.unwrap(classOf[OlapConnection])
    }

    override def wrap(olapConnection: OlapConnection) = 
                                  new DefaultPooledObject(olapConnection)

    override def passivateObject(pooledObject: PooledObject[OlapConnection] ) {
      println("passivateObject WAS CALLED")
      pooledObject.getObject.close
    }    

  }


class Test  {

  val olapConnectionFactory = new OlapConnectionFactory

  def test = { 

     val config = new GenericObjectPoolConfig
     config.setMaxIdle(5)
     config.setMaxTotal(10)

     val util = new OlapConnectionUtil(
                 new GenericObjectPool[OlapConnection](olapConnectionFactory,config))
     val olapConnection = util.connect

     // do stuff with olapConnection

     util.close(olapConnection)
4

1 回答 1

1

如果您打开 JavaDocs,PooledObjectFactory您可能会看到

  1. passivateObject(org.apache.commons.pool2.PooledObject<T>)当每个实例返回池时都会调用它。
  2. destroyObject(org.apache.commons.pool2.PooledObject<T>)在从池中“删除”实例时在每个实例上调用(无论是由于来自 的响应validateObject(org.apache.commons.pool2.PooledObject<T>),还是由于池实现的特定原因。)无法保证被销毁的实例将被视为主动、被动或处于大体一致的状态。

换句话说,您应该将资源取消分配逻辑放入destroyObject. 这是您使用假OlapConnection实现的修改后的测试

import org.apache.commons.pool2._
import org.apache.commons.pool2.impl._

// fake!
case class OlapConnection(val id: Int) {


  def close(): Unit = {
    println(s"Close was called for $this")
  }
}

class OlapConnectionUtil(val pool: ObjectPool[OlapConnection]) {
  def connect = {
    pool.borrowObject
  }

  def close(olapConnection: OlapConnection) = {
    pool.returnObject(olapConnection)
  }
}

class OlapConnectionFactory extends BasePooledObjectFactory[OlapConnection] {
  var cnt = 0

  override def create = {
    cnt += 1
    new OlapConnection(cnt)
  }

  override def wrap(olapConnection: OlapConnection) =
    new DefaultPooledObject(olapConnection)

  override def passivateObject(pooledObject: PooledObject[OlapConnection]) {
    println("-passivateObject was called")
  }

  override def destroyObject(pooledObject: PooledObject[OlapConnection]): Unit = {
    super.destroyObject(pooledObject)
    println("--destroyObject was called")
    pooledObject.getObject.close
  }
}

object Test {

  val olapConnectionFactory = new OlapConnectionFactory

  def test = {

    val config = new GenericObjectPoolConfig
    config.setMaxIdle(5)
    config.setMaxTotal(10)

    val util = new OlapConnectionUtil(new GenericObjectPool[OlapConnection](olapConnectionFactory, config))
    val initConnections = (1 to 10).map(i => util.connect).toList
    val closeCons = initConnections
    Thread.sleep(100)
    println("Start closing")
    closeCons.zipWithIndex.foreach(ci => {
      Thread.sleep(100)
      println(s"Before close ${ci._2 + 1}")
      util.close(ci._1)
      println(s"After close ${ci._2 + 1}")
    })
  }
}

此代码产生的输出是:

关闭前 1
-passivateObject 被调用
关闭 1 后 关闭
2 后
-passivateObject 被调用
关闭后 2
关闭前 3
-passivateObject 被调用
关闭后 3
关闭前 4
-passivateObject 被调用
关闭后 4
关闭前 5
-passivateObject 被调用
关闭后 5
关闭之前 6
-passivateObject 被调用
--destroyObject 被调用
Close 被调用用于 OlapConnection(6)
关闭之后 6 关闭
之前 7
-passivateObject 被调用
--destroyObject 被调用
为 OlapConnection(7) 调用了 Close 7
之后
在 close 8 之前
调用 了
-passivateObject --destroyObject
为 OlapConnection(8) 调用了 Close 8
在 close 8
之前
调用 了
-passivateObject --destroyObject 被
调用了OlapConnection(9)
关闭 9 之后 关闭 10
之前
-passivateObject 被调用
--destroyObject 被调用
关闭 OlapConnection(10)
之后 关闭 10

正如您在此输出中看到的那样,close正在调用此输出,因为OlapConnection(6)正如人们所期望的那样,给定池配置。

于 2018-05-02T20:32:52.357 回答