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

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

namespace cta {
namespace rdbms {

35
36
class Login;

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

43
44
  CTA_GENERATE_EXCEPTION_CLASS(MaxNbConnsIsZero);

45
46
47
  /**
   * Constructor.
   *
48
49
   * @param login The database login details to be used to create new
   * connections.
50
51
   * @param maxNbConns The maximum number of database connections within the
   * pool.
52
   * @throw MaxNbConnsIsZero if maxNbConns is set to 0.
53
   */
54
  ConnPool(const Login &login, const uint64_t maxNbConns);
55
56
57
58
59
60
61
62
63
64

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

67
68
private:

69
  friend Conn;
70

Steven Murray's avatar
Steven Murray committed
71
  /**
72
   * If the specified database connection is open, then this method calls
73
74
   * commit on the connection and returns it to the pool.
   *
75
76
77
78
   * 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.
   *
79
80
81
82
   * 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
83
   *
84
   * @param connAndStmts The connection to be commited and returned to the pool.
Steven Murray's avatar
Steven Murray committed
85
   */
86
  void returnConn(std::unique_ptr<ConnAndStmts> connAndStmts);
Steven Murray's avatar
Steven Murray committed
87
88

  /**
89
   * The database connection factory.
Steven Murray's avatar
Steven Murray committed
90
   */
91
  std::unique_ptr<wrapper::ConnFactory> m_connFactory;
Steven Murray's avatar
Steven Murray committed
92
93

  /**
Steven Murray's avatar
Steven Murray committed
94
   * The maximum number of database connections within the pool.
Steven Murray's avatar
Steven Murray committed
95
   */
96
  uint64_t m_maxNbConns;
Steven Murray's avatar
Steven Murray committed
97

Steven Murray's avatar
Steven Murray committed
98
99
100
101
102
  /**
   * The number of database connections currently on loan.
   */
  uint64_t m_nbConnsOnLoan;

Steven Murray's avatar
Steven Murray committed
103
104
105
  /**
   * Mutex used to serialize access to the database connections within the pool.
   */
106
  threading::Mutex m_connsAndStmtsMutex;
Steven Murray's avatar
Steven Murray committed
107
108
109
110
111

  /**
   * Condition variable used by threads returning connections to the pool to
   * notify threads waiting for connections.
   */
112
  threading::CondVar m_connsAndStmtsCv;
Steven Murray's avatar
Steven Murray committed
113
114
115
116

  /**
   * The database connections within the pool.
   */
117
  std::list<std::unique_ptr<ConnAndStmts> > m_connsAndStmts;
118
}; // class ConnPool
Steven Murray's avatar
Steven Murray committed
119
120
121

} // namespace rdbms
} // namespace cta