25#ifndef FBCPP_ATTACHMENT_POOL_H
26#define FBCPP_ATTACHMENT_POOL_H
28#include "Attachment.h"
29#include "fb-cpp-export.h"
33#include <condition_variable>
61 return attachmentOptions;
71 attachmentOptions = value;
114 return acquireTimeout;
122 acquireTimeout = value;
166 return validateOnAcquire;
174 validateOnAcquire = value;
184 return sessionResetOnRelease;
193 sessionResetOnRelease = value;
199 std::size_t minSize = 0u;
200 std::size_t maxSize = 10u;
201 std::chrono::milliseconds acquireTimeout{30000};
202 std::optional<std::chrono::milliseconds> idleTimeout;
203 std::optional<std::chrono::milliseconds> maxLifetime;
204 bool validateOnAcquire =
true;
205 bool sessionResetOnRelease =
false;
215 struct AttachmentPoolEntry final
217 std::unique_ptr<Attachment> attachment;
218 std::chrono::steady_clock::time_point createdAt;
219 std::chrono::steady_clock::time_point lastReturnedAt;
224 class AttachmentPool;
298 return entry !=
nullptr;
307 return *entry->attachment;
315 Attachment* operator->() noexcept
327 AttachmentPool* pool;
328 impl::AttachmentPoolEntry* entry;
373 const std::string&
getUri() const noexcept
394 std::size_t availableCount();
399 std::size_t inUseCount();
411 std::optional<PooledAttachment> tryAcquire();
420 std::optional<PooledAttachment> acquireImpl(
bool throwOnFailure);
421 void evictLocked(std::unique_lock<std::mutex>& lock);
422 void release(impl::AttachmentPoolEntry& entry)
noexcept;
428 std::vector<std::unique_ptr<impl::AttachmentPoolEntry>> entries;
429 std::deque<impl::AttachmentPoolEntry*> available;
430 std::size_t inUse = 0u;
431 std::size_t pending = 0u;
434 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.
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.
Client & getClient() noexcept
Returns the Client object reference used to create this AttachmentPool object.
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.