25#ifndef FBCPP_ATTACHMENT_POOL_H
26#define FBCPP_ATTACHMENT_POOL_H
28#include "Attachment.h"
32#include <condition_variable>
60 return attachmentOptions;
70 attachmentOptions = value;
113 return acquireTimeout;
121 acquireTimeout = value;
165 return validateOnAcquire;
173 validateOnAcquire = value;
183 return sessionResetOnRelease;
192 sessionResetOnRelease = value;
198 std::size_t minSize = 0u;
199 std::size_t maxSize = 10u;
200 std::chrono::milliseconds acquireTimeout{30000};
201 std::optional<std::chrono::milliseconds> idleTimeout;
202 std::optional<std::chrono::milliseconds> maxLifetime;
203 bool validateOnAcquire =
true;
204 bool sessionResetOnRelease =
false;
214 struct AttachmentPoolEntry final
216 std::unique_ptr<Attachment> attachment;
217 std::chrono::steady_clock::time_point createdAt;
218 std::chrono::steady_clock::time_point lastReturnedAt;
223 class AttachmentPool;
297 return entry !=
nullptr;
306 return *entry->attachment;
326 AttachmentPool* pool;
327 impl::AttachmentPoolEntry* entry;
372 const std::string&
getUri() const noexcept
419 std::optional<PooledAttachment> acquireImpl(
bool throwOnFailure);
420 void evictLocked(std::unique_lock<std::mutex>& lock);
421 void release(impl::AttachmentPoolEntry& entry)
noexcept;
427 std::vector<std::unique_ptr<impl::AttachmentPoolEntry>> entries;
428 std::deque<impl::AttachmentPoolEntry*> available;
429 std::size_t inUse = 0u;
430 std::size_t pending = 0u;
433 std::condition_variable availableCondition;
Represents options used when creating an Attachment object.
Represents options used when creating an AttachmentPool object.
bool getSessionResetOnRelease() const
Returns whether a connection has its session reset (via ALTER SESSION RESET) before being returned to...
const std::optional< std::chrono::milliseconds > & getIdleTimeout() const
Returns the maximum time a connection may stay idle in the pool before being closed.
AttachmentPoolOptions & setValidateOnAcquire(bool value)
Sets whether a connection is validated (via ping) before being handed out by acquire().
AttachmentPoolOptions & setIdleTimeout(std::chrono::milliseconds value)
Sets the maximum time a connection may stay idle in the pool before being closed.
std::size_t getMaxSize() const
Returns the maximum number of connections the pool may create.
bool getValidateOnAcquire() const
Returns whether a connection is validated (via ping) before being handed out by acquire().
AttachmentPoolOptions & setSessionResetOnRelease(bool value)
Sets whether a connection has its session reset (via ALTER SESSION RESET) before being returned to th...
const std::optional< std::chrono::milliseconds > & getMaxLifetime() const
Returns the maximum lifetime of a connection, regardless of usage.
std::chrono::milliseconds getAcquireTimeout() const
Returns how long acquire() waits for a connection to become available before throwing.
AttachmentPoolOptions & setAttachmentOptions(const AttachmentOptions &value)
Sets the options used to create each pooled Attachment.
AttachmentPoolOptions & setMaxLifetime(std::chrono::milliseconds value)
Sets the maximum lifetime of a connection, regardless of usage.
AttachmentPoolOptions & setMinSize(std::size_t value)
Sets the minimum number of connections kept warm by the pool.
const AttachmentOptions & getAttachmentOptions() const
Returns the options used to create each pooled Attachment.
AttachmentPoolOptions & setMaxSize(std::size_t value)
Sets the maximum number of connections the pool may create.
std::size_t getMinSize() const
Returns the minimum number of connections kept warm by the pool.
AttachmentPoolOptions & setAcquireTimeout(std::chrono::milliseconds value)
Sets how long acquire() waits for a connection to become available before throwing.
Represents a thread-safe pool of Attachment objects connected to the same database.
std::size_t inUseCount()
Returns the number of connections currently leased out by the pool.
PooledAttachment acquire()
Acquires a connection from the pool, creating one if needed and allowed by getMaxSize().
const AttachmentPoolOptions & getOptions() const noexcept
Returns the options used to create this AttachmentPool object.
const std::string & getUri() const noexcept
Returns the database URI this pool connects to.
void close()
Closes every available connection and marks the pool as closed.
Client & getClient() noexcept
Returns the Client object reference used to create this AttachmentPool object.
std::size_t availableCount()
Returns the number of connections currently available (not leased) in the pool.
~AttachmentPool() noexcept
Closes every idle connection in the pool.
std::size_t size()
Returns the total number of connections currently created by the pool (in use plus available).
std::optional< PooledAttachment > tryAcquire()
Attempts to acquire a connection from the pool without throwing.
Represents a connection to a Firebird database.
Represents a Firebird client library instance.
RAII lease for an Attachment borrowed from an AttachmentPool.
bool isValid() const noexcept
Returns whether this object currently holds a leased Attachment.
~PooledAttachment() noexcept
Returns the Attachment to the pool, unless it was already released.
void release()
Returns the leased Attachment to the pool immediately.
PooledAttachment & operator=(PooledAttachment &&o) noexcept
Move assignment.
Attachment & get() noexcept
Returns the leased Attachment.
PooledAttachment(PooledAttachment &&o) noexcept
Move constructor.