ConnPool.hpp 3.21 KB
Newer Older
Steven Murray's avatar
Steven Murray committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * The CERN Tape Archive (CTA) project
 * Copyright (C) 2015  CERN
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

21
22
23
24
#include "Conn.hpp"
#include "ConnFactory.hpp"
#include "Login.hpp"
#include "PooledConn.hpp"
Steven Murray's avatar
Steven Murray committed
25
26
27
28
29
30
31
32
33
34
35

#include <condition_variable>
#include <memory>
#include <mutex>

namespace cta {
namespace rdbms {

/**
 * A pool of database connections.
 */
36
class ConnPool {
Steven Murray's avatar
Steven Murray committed
37
38
39
40
41
public:

  /**
   * Constructor.
   *
42
   * @param login The database login details to be used to create new
Steven Murray's avatar
Steven Murray committed
43
   * connections.
44
   * @param nbConns The number of database connections within the pool.
Steven Murray's avatar
Steven Murray committed
45
   */
46
  ConnPool(const Login &login, const uint64_t nbConns);
Steven Murray's avatar
Steven Murray committed
47

48
49
50
  /**
   * Constructor.
   *
51
52
53
54
   * @param connFactory Factory to be used to create new database connections.
   * Please note that the ConnPool will take ownership of the factory and
   * therefore the destructor of ConnPool will delete the factory.
   * @param nbConns The number of database connections within the pool.
55
   */
56
  ConnPool(ConnFactory *const connFactory, const uint64_t nbConns);
57

Steven Murray's avatar
Steven Murray committed
58
59
60
  /**
   * Destructor.
   */
61
  ~ConnPool() throw();
Steven Murray's avatar
Steven Murray committed
62

63
64
65
66
67
68
69
70
71
  /**
   * Takes a connection from the pool.
   *
   * Please note that this method will block if the pool is empty.  In such a
   * situation this method will unblock when a connection is returned to the
   * pool.
   *
   * @return A connection from the pool.
   */
72
  PooledConn getConn();
73

Steven Murray's avatar
Steven Murray committed
74
75
76
  /**
   * Returns the specified database connection to the pool.
   *
77
   * @param conn The connection to be returned to the pool.
Steven Murray's avatar
Steven Murray committed
78
   */
79
  void returnConn(Conn *const conn);
Steven Murray's avatar
Steven Murray committed
80
81
82
83
84
85

private:

  /**
   * The database connection factory for the pool.
   */
86
  std::unique_ptr<ConnFactory> m_connFactory;
Steven Murray's avatar
Steven Murray committed
87
88
89
90

  /**
   * The number of database connections within the pool.
   */
91
  uint64_t m_nbConns;
Steven Murray's avatar
Steven Murray committed
92
93
94
95

  /**
   * Mutex used to serialize access to the database connections within the pool.
   */
96
  std::mutex m_connsMutex;
Steven Murray's avatar
Steven Murray committed
97
98
99
100
101

  /**
   * Condition variable used by threads returning connections to the pool to
   * notify threads waiting for connections.
   */
102
  std::condition_variable m_connsCv;
Steven Murray's avatar
Steven Murray committed
103
104
105
106

  /**
   * The database connections within the pool.
   */
107
  std::list<Conn *> m_conns;
Steven Murray's avatar
Steven Murray committed
108
109
110
111
112

  /**
   * Creates the database connection factory for the pool based on the specified
   * login details.
   *
113
   * @param login The database login details to be used to create new
Steven Murray's avatar
Steven Murray committed
114
115
   * connections.
   */
116
  void createConnFactory(const Login &login);
Steven Murray's avatar
Steven Murray committed
117
118
119
120

  /**
   * Creates the specified number of database connections with the pool.
   *
121
   * @param nbConns The number of database connections to be created.
Steven Murray's avatar
Steven Murray committed
122
   */
123
  void createConns(const uint64_t nbConns);
Steven Murray's avatar
Steven Murray committed
124

125
}; // class ConnPool
Steven Murray's avatar
Steven Murray committed
126
127
128

} // namespace rdbms
} // namespace cta