Class PoolLease<T>

java.lang.Object
cloud.opencode.base.pool.PoolLease<T>
Type Parameters:
T - the type of object being leased - 租约对象类型
All Implemented Interfaces:
AutoCloseable

public final class PoolLease<T> extends Object implements AutoCloseable
PoolLease - AutoCloseable Lease for Borrowed Pool Objects PoolLease - 借用池对象的自动关闭租约

An AutoCloseable wrapper around a borrowed pool object that enables the try-with-resources pattern. When the lease is closed, the object is automatically returned to the pool (or destroyed if invalidated).

借用池对象的 AutoCloseable 包装器,支持 try-with-resources 模式。 当租约关闭时,对象会自动归还到池中(如果已失效则销毁)。

Features | 主要功能:

  • Try-with-resources support - 支持 try-with-resources
  • Automatic return on close - 关闭时自动归还
  • Invalidation support - 支持失效标记
  • Idempotent close - 幂等关闭
  • Thread-safe - 线程安全

Usage Examples | 使用示例:

try (PoolLease<Connection> lease = pool.borrowLease()) {
    Connection conn = lease.get();
    conn.executeQuery("SELECT ...");
} // automatically returned to pool

// With invalidation
try (PoolLease<Connection> lease = pool.borrowLease()) {
    Connection conn = lease.get();
    try {
        conn.executeQuery("SELECT ...");
    } catch (SQLException e) {
        lease.invalidate(); // destroy instead of return
        throw e;
    }
}

Security | 安全性:

  • Thread-safe: Yes (AtomicBoolean for idempotent close) - 线程安全: 是 (AtomicBoolean 保证幂等关闭)
  • Null-safe: No (object and pool must not be null) - 空值安全: 否 (对象和池不能为空)
Since:
JDK 25, opencode-base-pool V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the lease and returns (or invalidates) the object.
    get()
    Gets the borrowed object.
    void
    Marks the object as invalid so that it will be destroyed instead of returned to the pool when the lease is closed.
    boolean
    Checks whether the lease has been closed.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • get

      public T get()
      Gets the borrowed object. 获取借用的对象。
      Returns:
      the borrowed object - 借用的对象
      Throws:
      IllegalStateException - if the lease is already closed - 如果租约已关闭
    • invalidate

      public void invalidate()
      Marks the object as invalid so that it will be destroyed instead of returned to the pool when the lease is closed. 将对象标记为无效,使其在租约关闭时被销毁而非归还到池中。

      This method can be called multiple times safely.

      此方法可以安全地多次调用。

    • close

      public void close()
      Closes the lease and returns (or invalidates) the object. 关闭租约并归还(或失效)对象。

      If invalidate() was called, the object is destroyed via ObjectPool.invalidateObject(Object). Otherwise, the object is returned via ObjectPool.returnObject(Object).

      如果调用了 invalidate(),对象将通过 ObjectPool.invalidateObject(Object) 销毁。否则,对象将通过 ObjectPool.returnObject(Object) 归还。

      This method is idempotent: only the first invocation has effect.

      此方法是幂等的:只有第一次调用有效果。

      Specified by:
      close in interface AutoCloseable
    • isClosed

      public boolean isClosed()
      Checks whether the lease has been closed. 检查租约是否已关闭。
      Returns:
      true if the lease is closed - 如果租约已关闭返回 true