ConnPool.hpp 3.12 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
#include "common/threading/CondVar.hpp"
#include "common/threading/Mutex.hpp"
23
#include "rdbms/ConnAndStmts.hpp"
24
#include "rdbms/Conn.hpp"
25
#include "rdbms/wrapper/ConnWrapper.hpp"
26
#include "rdbms/wrapper/ConnFactory.hpp"
Steven Murray's avatar
Steven Murray committed
27

28
#include <list>
Steven Murray's avatar
Steven Murray committed
29
30
31
32
33
#include <memory>

namespace cta {
namespace rdbms {

34
35
class Login;

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

42
43
44
  /**
   * Constructor.
   *
45
46
   * @param login The database login details to be used to create new
   * connections.
47
48
   * @param maxNbConns The maximum number of database connections within the
   * pool.
49
   */
50
  ConnPool(const Login &login, const uint64_t maxNbConns);
51
52
53
54
55
56
57
58
59
60

  /**
   * 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.
   */
61
  Conn getConn();
62

63
64
private:

65
  friend Conn;
66

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 open, then this method sets the
   * autocommit mode of the connection to AUTOCOMMIT_ON because this is the
   * default value of a newly created connection.
   *
75
76
77
78
   * 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
79
   *
80
   * @param connAndStmts The connection to be commited and returned to the pool.
Steven Murray's avatar
Steven Murray committed
81
   */
82
  void returnConn(std::unique_ptr<ConnAndStmts> connAndStmts);
Steven Murray's avatar
Steven Murray committed
83
84

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

  /**
Steven Murray's avatar
Steven Murray committed
90
   * The maximum number of database connections within the pool.
Steven Murray's avatar
Steven Murray committed
91
   */
92
  uint64_t m_maxNbConns;
Steven Murray's avatar
Steven Murray committed
93

Steven Murray's avatar
Steven Murray committed
94
95
96
97
98
  /**
   * The number of database connections currently on loan.
   */
  uint64_t m_nbConnsOnLoan;

Steven Murray's avatar
Steven Murray committed
99
100
101
  /**
   * Mutex used to serialize access to the database connections within the pool.
   */
102
  threading::Mutex m_connsAndStmtsMutex;
Steven Murray's avatar
Steven Murray committed
103
104
105
106
107

  /**
   * Condition variable used by threads returning connections to the pool to
   * notify threads waiting for connections.
   */
108
  threading::CondVar m_connsAndStmtsCv;
Steven Murray's avatar
Steven Murray committed
109
110
111
112

  /**
   * The database connections within the pool.
   */
113
  std::list<std::unique_ptr<ConnAndStmts> > m_connsAndStmts;
114
}; // class ConnPool
Steven Murray's avatar
Steven Murray committed
115
116
117

} // namespace rdbms
} // namespace cta