Skip to content

Commit 3defcf1

Browse files
committed
Refactoring connection_pool.h
1 parent b8e1049 commit 3defcf1

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed
Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,68 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
// INFO|TODO - Note that is file is Windows specific right now. Making it arch agnostic will be
5-
// taken up in future.
4+
// INFO|TODO - Note that is file is Windows specific right now. Making it
5+
// arch agnostic will be taken up in future.
6+
7+
#ifndef MSSQL_PYTHON_CONNECTION_POOL_H_
8+
#define MSSQL_PYTHON_CONNECTION_POOL_H_
69

710
#pragma once
11+
#include <chrono>
812
#include <deque>
9-
#include <unordered_map>
1013
#include <memory>
1114
#include <mutex>
1215
#include <string>
13-
#include <chrono>
14-
#include "connection.h"
16+
#include <unordered_map>
17+
#include "connection/connection.h"
1518

16-
// Manages a fixed-size pool of reusable database connections for a single connection string
19+
// Manages a fixed-size pool of reusable database connections for a
20+
// single connection string
1721
class ConnectionPool {
18-
public:
22+
public:
1923
ConnectionPool(size_t max_size, int idle_timeout_secs);
2024

2125
// Acquires a connection from the pool or creates a new one if under limit
22-
std::shared_ptr<Connection> acquire(const std::wstring& connStr, const py::dict& attrs_before = py::dict());
26+
std::shared_ptr<Connection> acquire(
27+
const std::wstring& connStr,
28+
const py::dict& attrs_before = py::dict());
2329

2430
// Returns a connection to the pool for reuse
2531
void release(std::shared_ptr<Connection> conn);
2632

2733
// Closes all connections in the pool, releasing resources
2834
void close();
2935

30-
private:
31-
size_t _max_size; // Maximum number of connections allowed
32-
int _idle_timeout_secs; // Idle time before connections are considered stale
36+
private:
37+
size_t _max_size; // Maximum number of connections allowed
38+
int _idle_timeout_secs; // Idle time before connections are stale
3339
size_t _current_size = 0;
3440
std::deque<std::shared_ptr<Connection>> _pool; // Available connections
35-
std::mutex _mutex; // Mutex for thread-safe access
41+
std::mutex _mutex; // Mutex for thread-safe access
3642
};
3743

3844
// Singleton manager that handles multiple pools keyed by connection string
3945
class ConnectionPoolManager {
40-
public:
46+
public:
4147
// Returns the singleton instance of the manager
4248
static ConnectionPoolManager& getInstance();
4349

4450
void configure(int max_size, int idle_timeout);
4551

4652
// Gets a connection from the appropriate pool (creates one if none exists)
47-
std::shared_ptr<Connection> acquireConnection(const std::wstring& conn_str, const py::dict& attrs_before = py::dict());
53+
std::shared_ptr<Connection> acquireConnection(
54+
const std::wstring& conn_str,
55+
const py::dict& attrs_before = py::dict());
4856

4957
// Returns a connection to its original pool
50-
void returnConnection(const std::wstring& conn_str, std::shared_ptr<Connection> conn);
58+
void returnConnection(const std::wstring& conn_str,
59+
std::shared_ptr<Connection> conn);
5160

5261
// Closes all pools and their connections
5362
void closePools();
5463

55-
private:
56-
ConnectionPoolManager() = default;
64+
private:
65+
ConnectionPoolManager() = default;
5766
~ConnectionPoolManager() = default;
5867

5968
// Map from connection string to connection pool
@@ -63,8 +72,10 @@ class ConnectionPoolManager {
6372
std::mutex _manager_mutex;
6473
size_t _default_max_size = 10;
6574
int _default_idle_secs = 300;
66-
75+
6776
// Prevent copying
6877
ConnectionPoolManager(const ConnectionPoolManager&) = delete;
6978
ConnectionPoolManager& operator=(const ConnectionPoolManager&) = delete;
7079
};
80+
81+
#endif // MSSQL_PYTHON_CONNECTION_POOL_H_

0 commit comments

Comments
 (0)