connectionpool.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef TNTDB_CONNECTIONPOOL_H
30 #define TNTDB_CONNECTIONPOOL_H
31 
32 #include <cxxtools/pool.h>
33 #include <map>
34 #include <string>
35 
36 namespace tntdb
37 {
38  class Connection;
39 
41  {
42  class Connector
43  {
44  std::string url;
45 
46  public:
47  Connector(const std::string& url_)
48  : url(url_)
49  { }
50 
51  Connection* operator() ();
52  };
53 
55  PoolType pool;
56 
57  public:
58  typedef PoolType::Ptr PoolObjectType;
59 
60  public:
61  explicit ConnectionPool(const std::string& url, unsigned maxcount = 0)
62  : pool(maxcount, Connector(url))
63  { }
64 
66 
68  void drop(unsigned keep = 0) { pool.drop(keep); }
69 
70  unsigned getMaximumSize() { return pool.getMaximumSize(); }
71  void setMaximumSize(unsigned m) { pool.setMaximumSize(m); }
72  unsigned getCurrentSize() const { return pool.getCurrentSize(); }
73  };
74 
76  {
78  ConnectionPools& operator=(const ConnectionPools&) { return *this; }
79 
80  public:
82  typedef std::map<std::string, PoolType*> PoolsType;
83 
84  private:
85  PoolsType pools;
86  unsigned maxcount;
87  mutable cxxtools::Mutex mutex;
88 
89  public:
90  explicit ConnectionPools(unsigned maxcount_ = 0)
91  : maxcount(maxcount_)
92  { }
94 
95  Connection connect(const std::string& url);
96 
98  void drop(unsigned keep = 0);
99 
103  void drop(const std::string& url, unsigned keep = 0);
104 
105  unsigned getMaximumSize()
106  { return maxcount; }
107 
108  void setMaximumSize(unsigned m);
109  unsigned getCurrentSize(const std::string& url) const;
110  };
111 }
112 
113 #endif // TNTDB_CONNECTIONPOOL_H
114