#pragma once #include "config.h" #if USE_LIBPQXX #include #include #include #include "Connection.h" namespace postgres { using Pool = BorrowedObjectPool; using PoolPtr = std::shared_ptr; class ConnectionHolder { public: ConnectionHolder(PoolPtr pool_, ConnectionPtr connection_, bool auto_close_) : pool(pool_) , connection(std::move(connection_)) , auto_close(auto_close_) {} ConnectionHolder(const ConnectionHolder & other) = delete; void setBroken() { is_broken = true; } ~ConnectionHolder() { if (auto_close) { connection.reset(); } else if (is_broken) { try { connection->getRef().reset(); } catch (...) { connection.reset(); } } pool->returnObject(std::move(connection)); } pqxx::connection & get() { return connection->getRef(); } void update() { connection->updateConnection(); } private: PoolPtr pool; ConnectionPtr connection; bool auto_close; bool is_broken = false; }; using ConnectionHolderPtr = std::unique_ptr; } #endif