ConnPool.hpp 2.48 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

Steven Murray's avatar
Steven Murray committed
58
59
60
  /**
   * Returns the specified database connection to the pool.
   *
61
   * @param conn The connection to be returned to the pool.
Steven Murray's avatar
Steven Murray committed
62
   */
63
  void returnConn(Conn *const conn);
Steven Murray's avatar
Steven Murray committed
64
65
66
67

private:

  /**
68
   * The database connection factory.
Steven Murray's avatar
Steven Murray committed
69
   */
70
  ConnFactory &m_connFactory;
Steven Murray's avatar
Steven Murray committed
71
72
73
74

  /**
   * The number of database connections within the pool.
   */
75
  uint64_t m_nbConns;
Steven Murray's avatar
Steven Murray committed
76
77
78
79

  /**
   * Mutex used to serialize access to the database connections within the pool.
   */
80
  std::mutex m_connsMutex;
Steven Murray's avatar
Steven Murray committed
81
82
83
84
85

  /**
   * Condition variable used by threads returning connections to the pool to
   * notify threads waiting for connections.
   */
86
  std::condition_variable m_connsCv;
Steven Murray's avatar
Steven Murray committed
87
88
89
90

  /**
   * The database connections within the pool.
   */
91
  std::list< std::unique_ptr<Conn> > m_conns;
Steven Murray's avatar
Steven Murray committed
92
93
94
95

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

100
}; // class ConnPool
Steven Murray's avatar
Steven Murray committed
101
102
103

} // namespace rdbms
} // namespace cta