ConnPool.hpp 2.81 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
#include "Conn.hpp"
#include "ConnFactory.hpp"
#include "PooledConn.hpp"
Steven Murray's avatar
Steven Murray committed
24
25

#include <condition_variable>
26
#include <list>
Steven Murray's avatar
Steven Murray committed
27
28
29
30
31
32
33
34
35
#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
public:

39
40
41
  /**
   * Constructor.
   *
42
   * @param connFactory The database connection factory.
43
44
   * @param maxNbConns The maximum number of database connections within the
   * pool.
45
   */
46
  ConnPool(ConnFactory &connFactory, const uint64_t maxNbConns);
47
48
49
50
51
52
53
54
55
56

  /**
   * 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.
   */
57
  PooledConn getConn();
58

59
60
61
62
private:

  friend PooledConn;

Steven Murray's avatar
Steven Murray committed
63
  /**
64
   * If the specified database connection is open, then this method calls
65
66
   * commit on the connection and returns it to the pool.
   *
67
68
69
70
   * If the specified database connection is closed, then this method closes all
   * connections within the pool.
   *
   * A closed connection is reopened when it is pulled from the pool.
Steven Murray's avatar
Steven Murray committed
71
   *
72
   * @param conn The connection to be commited and returned to the pool.
Steven Murray's avatar
Steven Murray committed
73
   */
74
  void returnConn(Conn *const conn);
Steven Murray's avatar
Steven Murray committed
75
76

  /**
77
   * The database connection factory.
Steven Murray's avatar
Steven Murray committed
78
   */
79
  ConnFactory &m_connFactory;
Steven Murray's avatar
Steven Murray committed
80
81

  /**
82
   * The maximum mnumber of database connections within the pool.
Steven Murray's avatar
Steven Murray committed
83
   */
84
  uint64_t m_maxNbConns;
Steven Murray's avatar
Steven Murray committed
85
86
87
88

  /**
   * Mutex used to serialize access to the database connections within the pool.
   */
89
  std::mutex m_connsMutex;
Steven Murray's avatar
Steven Murray committed
90
91
92
93
94

  /**
   * Condition variable used by threads returning connections to the pool to
   * notify threads waiting for connections.
   */
95
  std::condition_variable m_connsCv;
Steven Murray's avatar
Steven Murray committed
96
97
98
99

  /**
   * The database connections within the pool.
   */
100
  std::list< std::unique_ptr<Conn> > m_conns;
Steven Murray's avatar
Steven Murray committed
101
102
103
104

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

109
}; // class ConnPool
Steven Murray's avatar
Steven Murray committed
110
111
112

} // namespace rdbms
} // namespace cta