Interface PooledObjectFactory<T>

Type Parameters:
T - the type of object being pooled - 池化对象类型
All Known Implementing Classes:
BasePooledObjectFactory, SimplePooledObjectFactory

public interface PooledObjectFactory<T>
PooledObjectFactory - Pooled Object Factory Interface PooledObjectFactory - 池化对象工厂接口

Factory interface for creating and managing pooled objects throughout their lifecycle: creation, activation, validation, passivation, and destruction.

用于在整个生命周期中创建和管理池化对象的工厂接口:创建、激活、验证、钝化和销毁。

Object Lifecycle | 对象生命周期:

makeObject()      -> Create new object
activateObject()  -> Prepare object for use (before borrow)
validateObject()  -> Check if object is valid
passivateObject() -> Reset object state (after return)
destroyObject()   -> Cleanup and release resources

Features | 主要功能:

  • Object creation - 对象创建
  • Object activation before borrow - 借用前激活对象
  • Object validation - 对象验证
  • Object passivation after return - 归还后钝化对象
  • Object destruction - 对象销毁

Usage Examples | 使用示例:

PooledObjectFactory<Connection> factory = new PooledObjectFactory<>() {
    @Override
    public PooledObject<Connection> makeObject() {
        return new DefaultPooledObject<>(DriverManager.getConnection(url));
    }

    @Override
    public boolean validateObject(PooledObject<Connection> obj) {
        return obj.getObject().isValid(1);
    }
};

Security | 安全性:

  • Thread-safe: Implementation dependent - 线程安全: 取决于实现
  • Null-safe: No - 空值安全: 否
Since:
JDK 25, opencode-base-pool V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • makeObject

      PooledObject<T> makeObject() throws OpenPoolException
      Creates a new pooled object. 创建新的池化对象。

      Called when the pool needs to create a new object.

      当池需要创建新对象时调用。

      Returns:
      the new pooled object - 新的池化对象
      Throws:
      OpenPoolException - if creation fails - 如果创建失败
    • destroyObject

      void destroyObject(PooledObject<T> obj) throws OpenPoolException
      Destroys a pooled object. 销毁池化对象。

      Called when the object is being removed from the pool.

      当对象从池中移除时调用。

      Parameters:
      obj - the object to destroy - 要销毁的对象
      Throws:
      OpenPoolException - if destruction fails - 如果销毁失败
    • validateObject

      boolean validateObject(PooledObject<T> obj)
      Validates a pooled object. 验证池化对象。

      Called to check if the object is still valid for use.

      调用以检查对象是否仍可使用。

      Parameters:
      obj - the object to validate - 要验证的对象
      Returns:
      true if valid, false otherwise - 有效返回true,否则返回false
    • activateObject

      void activateObject(PooledObject<T> obj) throws OpenPoolException
      Activates a pooled object. 激活池化对象。

      Called before the object is borrowed from the pool.

      在对象从池中借出前调用。

      Parameters:
      obj - the object to activate - 要激活的对象
      Throws:
      OpenPoolException - if activation fails - 如果激活失败
    • passivateObject

      void passivateObject(PooledObject<T> obj) throws OpenPoolException
      Passivates a pooled object. 钝化池化对象。

      Called after the object is returned to the pool.

      在对象归还到池后调用。

      Parameters:
      obj - the object to passivate - 要钝化的对象
      Throws:
      OpenPoolException - if passivation fails - 如果钝化失败