Skip to content

Hanzla-Double3/pqxx-dynamic-connection-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

CONNECTION POOL FOR POSTGRESQL USING PQXX (C++)

This is a connection pool implementation for managing PostgreSQL database connections using the pqxx library in C++. It is designed to efficiently manage a pool of database connections, reducing the overhead of repeatedly opening and closing connections. The pool supports prepared statements, connection limits, timeouts, and automatic cleanup of idle connections.

FEATURES:

  • Connection reuse to minimize database connection overhead
  • Prepared statement support during connection initialization
  • Configurable minimum and maximum connection limits
  • Timeout handling for connection acquisition
  • Automatic cleanup of idle connections (30-minute threshold)
  • Thread-safe operations using mutexes and condition variables
  • Connection validation when retrieving from pool
  • Retry mechanism for initial connection attempts

USAGE:

  • Include the connection pool header in your project
  • Link against pqxx and required PostgreSQL libraries
  • Create a ConnectionPool instance with appropriate parameters
  • Use get_connection() to obtain a connection guard
  • Execute database operations through the connection guard
  • The connection is automatically returned to the pool when the guard goes out of scope

CONFIGURATION PARAMETERS:

  • Connection string (format: "dbname=... user=... host=... port=... password=...")
  • Initial connection count
  • Timeout duration for acquiring connections
  • List of prepared statements (name/SQL pairs)
  • Minimum connections (default: 1)
  • Maximum connections (default: 4)
  • Idle disconnect timeout in seconds (default: 60)

EXCEPTIONS:

  • pqxx::broken_connection: Thrown when connection attempts fail during pool initialization or expansion
  • std::runtime_error: Thrown when retrieving a closed connection from the pool

DEPENDENCIES:

  • pqxx library (7.7+ recommended)
  • C++17 or newer
  • PostgreSQL client libraries
  • Thread support (pthreads or equivalent)

EXAMPLE USAGE:

try {
    std::vector<Statement> prep = {{"find_user", "SELECT * FROM users WHERE id = $1"}};
    ConnectionPool pool(conn_str, 2, std::chrono::seconds(5), prep);
    
    auto conn = pool.get_connection();
    pqxx::work txn(*conn);
    auto result = txn.exec_prepared("find_user", 123);
    txn.commit();
}
catch (const std::exception &e) {
    // Handle exceptions
}

NOTES:

  • The pool is thread-safe and can be used in multi-threaded environments
  • Connections are returned to the pool automatically via RAII (ConnectionGuard destructor)
  • Cleanup thread runs every 5 minutes to remove connections idle for more than 30 minutes
  • Failed connection attempts during pool creation will retry 5 times per connection
  • The pool maintains at least the minimum number of connections specified

BUILDING:

Ensure proper linking with pqxx and PostgreSQL libraries. Compile with C++17 support and thread-safe flags.

LIMITATIONS:

  • Currently PostgreSQL-specific due to pqxx dependency
  • Connection validation only occurs when checking out from pool
  • Prepared statements must be redefined if connections are recreated

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages