Interface KeyedObjectPool<K,V>

Type Parameters:
K - the key type - 键类型
V - the value type - 值类型
All Superinterfaces:
AutoCloseable
All Known Implementing Classes:
GenericKeyedObjectPool

public interface KeyedObjectPool<K,V> extends AutoCloseable
KeyedObjectPool - Keyed Object Pool Interface KeyedObjectPool - 键控对象池接口

Object pool interface that manages objects by key. Each key has its own pool of objects, useful for multi-tenant or multi-datasource scenarios.

按键管理对象的对象池接口。每个键有自己的对象池,适用于多租户或多数据源场景。

Features | 主要功能:

  • Key-based object management - 基于键的对象管理
  • Per-key borrow/return - 每键借用/归还
  • Per-key statistics - 每键统计
  • Multi-tenant support - 多租户支持

Usage Examples | 使用示例:

// Multi-datasource scenario
Connection masterConn = pool.borrowObject("master");
Connection slaveConn = pool.borrowObject("slave");
try {
    // use connections
} finally {
    pool.returnObject("master", masterConn);
    pool.returnObject("slave", slaveConn);
}

// Execute pattern
String result = pool.execute("master", (key, conn) -> {
    return conn.executeQuery("SELECT...");
});

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 Summary

    Modifier and Type
    Method
    Description
    Borrows an object for the given key.
    borrowObject(K key, Duration timeout)
    Borrows an object for the given key with timeout.
    void
    Clears all objects for all keys.
    void
    clear(K key)
    Clears objects for the given key.
    default <R> R
    execute(K key, BiFunction<K,V,R> action)
    Executes an action with a pooled object (auto-return).
    int
    Gets the active count for the given key.
    int
    Gets the idle count for the given key.
    int
    Gets the total number of keys.
    void
    invalidateObject(K key, V obj)
    Invalidates an object for the given key.
    void
    returnObject(K key, V obj)
    Returns an object for the given key.

    Methods inherited from interface AutoCloseable

    close
  • Method Details

    • borrowObject

      V borrowObject(K key) throws OpenPoolException
      Borrows an object for the given key. 为给定的键借用对象。
      Parameters:
      key - the key - 键
      Returns:
      the borrowed object - 借用的对象
      Throws:
      OpenPoolException - if borrowing fails - 如果借用失败
    • borrowObject

      V borrowObject(K key, Duration timeout) throws OpenPoolException
      Borrows an object for the given key with timeout. 带超时为给定的键借用对象。
      Parameters:
      key - the key - 键
      timeout - the maximum wait time - 最大等待时间
      Returns:
      the borrowed object - 借用的对象
      Throws:
      OpenPoolException - if borrowing fails or times out - 如果借用失败或超时
    • returnObject

      void returnObject(K key, V obj)
      Returns an object for the given key. 归还给定键的对象。
      Parameters:
      key - the key - 键
      obj - the object to return - 要归还的对象
    • invalidateObject

      void invalidateObject(K key, V obj)
      Invalidates an object for the given key. 使给定键的对象失效。
      Parameters:
      key - the key - 键
      obj - the object to invalidate - 要失效的对象
    • getNumIdle

      int getNumIdle(K key)
      Gets the idle count for the given key. 获取给定键的空闲数。
      Parameters:
      key - the key - 键
      Returns:
      the idle count - 空闲数
    • getNumActive

      int getNumActive(K key)
      Gets the active count for the given key. 获取给定键的活跃数。
      Parameters:
      key - the key - 键
      Returns:
      the active count - 活跃数
    • clear

      void clear(K key)
      Clears objects for the given key. 清除给定键的对象。
      Parameters:
      key - the key - 键
    • clear

      void clear()
      Clears all objects for all keys. 清除所有键的所有对象。
    • getNumKeys

      int getNumKeys()
      Gets the total number of keys. 获取键的总数。
      Returns:
      the key count - 键数
    • execute

      default <R> R execute(K key, BiFunction<K,V,R> action) throws OpenPoolException
      Executes an action with a pooled object (auto-return). 使用池化对象执行操作(自动归还)。
      Type Parameters:
      R - the result type - 结果类型
      Parameters:
      key - the key - 键
      action - the action (receives key and object) - 操作(接收键和对象)
      Returns:
      the action result - 操作结果
      Throws:
      OpenPoolException - if execution fails - 如果执行失败