MySQL++: cpool.h Source File
cpool.h
Go to the documentation of this file.
/***********************************************************************
Copyright (c) 2007-2008 by Educational Technology Resources, Inc. and
(c) 2007 by Jonathan Wakely. Others may also hold copyrights on
code in this file. See the CREDITS.txt file in the top directory
of the distribution for details.
This file is part of MySQL++.
MySQL++ is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
MySQL++ 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with MySQL++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
***********************************************************************/
#if !defined(MYSQLPP_CPOOL_H)
#define MYSQLPP_CPOOL_H
#include "beemutex.h"
#include <list>
#include <assert.h>
#include <time.h>
namespace mysqlpp {
#if !defined(DOXYGEN_IGNORE)
// Make Doxygen ignore this
class MYSQLPP_EXPORT Connection;
#endif
class MYSQLPP_EXPORT ConnectionPool
{
public:
ConnectionPool() { }
virtual ~ConnectionPool() { assert(empty()); }
bool empty() const { return pool_.empty(); }
virtual Connection* exchange(const Connection* pc);
virtual Connection* grab();
virtual void release(const Connection* pc);
void remove(const Connection* pc);
virtual Connection* safe_grab();
void shrink() { clear(false); }
protected:
void clear(bool all = true);
virtual Connection* create() = 0;
virtual void destroy(Connection*) = 0;
virtual unsigned int max_idle_time() = 0;
size_t size() const { return pool_.size(); }
private:
struct ConnectionInfo {
Connection* conn;
time_t last_used;
bool in_use;
ConnectionInfo(Connection* c) :
conn(c),
last_used(time(0)),
in_use(true)
{
}
// Strict weak ordering for ConnectionInfo objects.
//
// This ordering defines all in-use connections to be "less
// than" those not in use. Within each group, connections
// less recently touched are less than those more recent.
bool operator<(const ConnectionInfo& rhs) const
{
const ConnectionInfo& lhs = *this;
return lhs.in_use == rhs.in_use ?
lhs.last_used < rhs.last_used :
lhs.in_use;
}
};
typedef std::list<ConnectionInfo> PoolT;
typedef PoolT::iterator PoolIt;
Connection* find_mru();
void remove(const PoolIt& it);
void remove_old_connections();
PoolT pool_;
BeecryptMutex mutex_;
};
} // end namespace mysqlpp
#endif // !defined(MYSQLPP_CPOOL_H)
mysqlpp::Connection
Manages the connection to the database server.
Definition: connection.h:59
mysqlpp::ConnectionPool
Manages a pool of connections for programs that need more than one Connection object at a time,...
Definition: cpool.h:68
mysqlpp::ConnectionPool::size
size_t size() const
Returns the current size of the internal connection pool.
Definition: cpool.h:210
mysqlpp::ConnectionPool::empty
bool empty() const
Returns true if pool is empty.
Definition: cpool.h:81
beemutex.h
MUTually EXclusive lock class.
mysqlpp::ConnectionPool::ConnectionPool
ConnectionPool()
Create empty pool.
Definition: cpool.h:72
mysqlpp::ConnectionPool::shrink
void shrink()
Remove all unused connections from the pool.
Definition: cpool.h:165
mysqlpp::ConnectionPool::~ConnectionPool
virtual ~ConnectionPool()
Destroy object.
Definition: cpool.h:78