Class BaseKeyedPooledObjectFactory<K,V>

java.lang.Object
cloud.opencode.base.pool.factory.BaseKeyedPooledObjectFactory<K,V>
Type Parameters:
K - the key type - 键类型
V - the value type - 值类型
All Implemented Interfaces:
KeyedPooledObjectFactory<K,V>

public abstract class BaseKeyedPooledObjectFactory<K,V> extends Object implements KeyedPooledObjectFactory<K,V>
BaseKeyedPooledObjectFactory - Base Keyed Pooled Object Factory BaseKeyedPooledObjectFactory - 基础键控池化对象工厂

Abstract base class for KeyedPooledObjectFactory implementations. Subclasses only need to implement the create(key) method.

KeyedPooledObjectFactory实现的抽象基类。子类只需实现create(key)方法。

Features | 主要功能:

  • Simplified keyed factory implementation - 简化的键控工厂实现
  • Default no-op lifecycle methods - 默认空操作的生命周期方法
  • Key-aware object creation - 键感知的对象创建
  • Override only what you need - 只覆盖需要的方法

Usage Examples | 使用示例:

KeyedPooledObjectFactory<String, Connection> factory =
    new BaseKeyedPooledObjectFactory<>() {
        @Override
        protected Connection create(String dsName) throws OpenPoolException {
            try {
                return getDataSource(dsName).getConnection();
            } catch (SQLException e) {
                throw new OpenPoolException("Failed to create connection", e);
            }
        }

        @Override
        public void destroyObject(String key, PooledObject<Connection> obj) {
            try {
                obj.getObject().close();
            } catch (SQLException e) {
                // log error
            }
        }
    };

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:
  • Constructor Details

    • BaseKeyedPooledObjectFactory

      public BaseKeyedPooledObjectFactory()
  • Method Details

    • create

      protected abstract V create(K key) throws OpenPoolException
      Creates the actual object instance for the given key. 为给定的键创建实际的对象实例。

      Subclasses must implement this method to create the object.

      子类必须实现此方法来创建对象。

      Parameters:
      key - the key - 键
      Returns:
      the new object - 新对象
      Throws:
      OpenPoolException - if creation fails - 如果创建失败
    • wrap

      protected PooledObject<V> wrap(V obj)
      Wraps an object in a PooledObject wrapper. 将对象包装在PooledObject包装器中。
      Parameters:
      obj - the object to wrap - 要包装的对象
      Returns:
      the wrapped object - 包装后的对象
    • makeObject

      public PooledObject<V> makeObject(K key) throws OpenPoolException
      Description copied from interface: KeyedPooledObjectFactory
      Creates a new pooled object for the given key. 为给定的键创建新的池化对象。
      Specified by:
      makeObject in interface KeyedPooledObjectFactory<K,V>
      Parameters:
      key - the key - 键
      Returns:
      the new pooled object - 新的池化对象
      Throws:
      OpenPoolException - if creation fails - 如果创建失败
    • destroyObject

      public void destroyObject(K key, PooledObject<V> obj) throws OpenPoolException
      Description copied from interface: KeyedPooledObjectFactory
      Destroys a pooled object for the given key. 销毁给定键的池化对象。
      Specified by:
      destroyObject in interface KeyedPooledObjectFactory<K,V>
      Parameters:
      key - the key - 键
      obj - the object to destroy - 要销毁的对象
      Throws:
      OpenPoolException - if destruction fails - 如果销毁失败
    • validateObject

      public boolean validateObject(K key, PooledObject<V> obj)
      Description copied from interface: KeyedPooledObjectFactory
      Validates a pooled object for the given key. 验证给定键的池化对象。
      Specified by:
      validateObject in interface KeyedPooledObjectFactory<K,V>
      Parameters:
      key - the key - 键
      obj - the object to validate - 要验证的对象
      Returns:
      true if valid - 有效返回true
    • activateObject

      public void activateObject(K key, PooledObject<V> obj) throws OpenPoolException
      Description copied from interface: KeyedPooledObjectFactory
      Activates a pooled object for the given key. 激活给定键的池化对象。
      Specified by:
      activateObject in interface KeyedPooledObjectFactory<K,V>
      Parameters:
      key - the key - 键
      obj - the object to activate - 要激活的对象
      Throws:
      OpenPoolException - if activation fails - 如果激活失败
    • passivateObject

      public void passivateObject(K key, PooledObject<V> obj) throws OpenPoolException
      Description copied from interface: KeyedPooledObjectFactory
      Passivates a pooled object for the given key. 钝化给定键的池化对象。
      Specified by:
      passivateObject in interface KeyedPooledObjectFactory<K,V>
      Parameters:
      key - the key - 键
      obj - the object to passivate - 要钝化的对象
      Throws:
      OpenPoolException - if passivation fails - 如果钝化失败