ConnPool.hpp 2.88 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
   * @param nbConns The number of database connections within the pool.
44
   */
45
  ConnPool(ConnFactory &connFactory, const uint64_t nbConns);
46
47
48
49
50
51
52
53
54
55

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

58
59
60
61
62
  /**
   * Closes all the database connections currently in the pool.
   */
  void closeAllConns();

63
64
65
66
private:

  friend PooledConn;

Steven Murray's avatar
Steven Murray committed
67
  /**
68
   * If the specified database connection is open, then this method calls
69
70
   * commit on the connection and returns it to the pool.
   *
71
72
73
74
   * 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
75
   *
76
   * @param conn The connection to be commited and returned to the pool.
Steven Murray's avatar
Steven Murray committed
77
   */
78
  void returnConn(Conn *const conn);
Steven Murray's avatar
Steven Murray committed
79
80

  /**
81
   * The database connection factory.
Steven Murray's avatar
Steven Murray committed
82
   */
83
  ConnFactory &m_connFactory;
Steven Murray's avatar
Steven Murray committed
84
85
86
87

  /**
   * The number of database connections within the pool.
   */
88
  uint64_t m_nbConns;
Steven Murray's avatar
Steven Murray committed
89
90
91
92

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

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

  /**
   * The database connections within the pool.
   */
104
  std::list< std::unique_ptr<Conn> > m_conns;
Steven Murray's avatar
Steven Murray committed
105
106
107
108

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

113
}; // class ConnPool
Steven Murray's avatar
Steven Murray committed
114
115
116

} // namespace rdbms
} // namespace cta