ConnPool.hpp 2.97 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
public:

39
40
41
  /**
   * Constructor.
   *
42
43
44
45
   * @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.
46
   */
47
  ConnPool(std::unique_ptr<ConnFactory> connFactory, const uint64_t nbConns);
48

Steven Murray's avatar
Steven Murray committed
49
50
51
  /**
   * Destructor.
   */
52
  ~ConnPool() throw();
Steven Murray's avatar
Steven Murray committed
53

54
55
56
57
58
59
60
61
62
  /**
   * 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.
   */
63
  PooledConn getConn();
64

Steven Murray's avatar
Steven Murray committed
65
66
67
  /**
   * Returns the specified database connection to the pool.
   *
68
   * @param conn The connection to be returned to the pool.
Steven Murray's avatar
Steven Murray committed
69
   */
70
  void returnConn(Conn *const conn);
Steven Murray's avatar
Steven Murray committed
71
72
73
74
75
76

private:

  /**
   * The database connection factory for the pool.
   */
77
  std::unique_ptr<ConnFactory> m_connFactory;
Steven Murray's avatar
Steven Murray committed
78
79
80
81

  /**
   * The number of database connections within the pool.
   */
82
  uint64_t m_nbConns;
Steven Murray's avatar
Steven Murray committed
83
84
85
86

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

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

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

  /**
   * Creates the database connection factory for the pool based on the specified
   * login details.
   *
104
   * @param login The database login details to be used to create new
Steven Murray's avatar
Steven Murray committed
105
106
   * connections.
   */
107
  void createConnFactory(const Login &login);
Steven Murray's avatar
Steven Murray committed
108
109
110
111

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

116
}; // class ConnPool
Steven Murray's avatar
Steven Murray committed
117
118
119

} // namespace rdbms
} // namespace cta