poco/MongoDB/include/Poco/MongoDB/PoolableConnectionFactory.h
2013-03-11 22:50:08 -05:00

126 lines
3.1 KiB
C++

//
// PoolableConnectionFactory.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: PoolableConnectionFactory
//
// Definition of the PoolableConnectionFactory class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef MongoDB_PoolableConnectionFactory_INCLUDED
#define MongoDB_PoolableConnectionFactory_INCLUDED
#include "Poco/MongoDB/Connection.h"
#include "Poco/ObjectPool.h"
namespace Poco {
template<>
class PoolableObjectFactory<MongoDB::Connection, MongoDB::Connection::Ptr>
/// PoolableObjectFactory specialisation for Connection. New connections
/// are created with the given address.
{
public:
PoolableObjectFactory(Net::SocketAddress& address)
: _address(address)
{
}
PoolableObjectFactory(const std::string& address)
: _address(address)
{
}
MongoDB::Connection::Ptr createObject()
{
return new MongoDB::Connection(_address);
}
bool validateObject(MongoDB::Connection::Ptr pObject)
{
return true;
}
void activateObject(MongoDB::Connection::Ptr pObject)
{
}
void deactivateObject(MongoDB::Connection::Ptr pObject)
{
}
void destroyObject(MongoDB::Connection::Ptr pObject)
{
}
private:
Net::SocketAddress _address;
};
namespace MongoDB {
class PooledConnection
/// Helper class for borrowing and returning a connection automatically from a pool.
{
public:
PooledConnection(Poco::ObjectPool<Connection, Connection::Ptr>& pool) : _pool(pool)
{
_connection = _pool.borrowObject();
}
virtual ~PooledConnection()
{
_pool.returnObject(_connection);
}
operator Connection::Ptr ()
{
return _connection;
}
private:
Poco::ObjectPool<Connection, Connection::Ptr>& _pool;
Connection::Ptr _connection;
};
} // namespace MongoDB
} // namespace Poco
#endif //MongoDB_PoolableConnectionFactory_INCLUDED