mirror of
https://github.com/pocoproject/poco.git
synced 2025-02-20 22:31:23 +01:00
SessionPool sessions feature/property setters and getters
SessionPoolContainer
This commit is contained in:
parent
087550a844
commit
9a024d4516
@ -477,6 +477,9 @@
|
||||
<File
|
||||
RelativePath=".\include\Poco\Data\SessionPool.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\Poco\Data\SessionPoolContainer.h">
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
@ -490,6 +493,9 @@
|
||||
<File
|
||||
RelativePath=".\src\SessionPool.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\SessionPoolContainer.cpp">
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
<Filter
|
||||
|
@ -653,6 +653,10 @@
|
||||
RelativePath=".\include\Poco\Data\SessionPool.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\Poco\Data\SessionPoolContainer.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
@ -669,6 +673,10 @@
|
||||
RelativePath=".\src\SessionPool.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\SessionPoolContainer.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
<Filter
|
||||
|
@ -658,6 +658,10 @@
|
||||
RelativePath=".\include\Poco\Data\SessionPool.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\Poco\Data\SessionPoolContainer.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
@ -674,6 +678,10 @@
|
||||
RelativePath=".\src\SessionPool.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\SessionPoolContainer.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
<Filter
|
||||
|
@ -62,7 +62,7 @@ class ODBC_API SessionImpl: public Poco::Data::AbstractSessionImpl<SessionImpl>
|
||||
/// Implements SessionImpl interface
|
||||
{
|
||||
public:
|
||||
SessionImpl(const std::string& fileName,
|
||||
SessionImpl(const std::string& connect,
|
||||
Poco::Any maxFieldSize = std::size_t(1024),
|
||||
bool enforceCapability=false,
|
||||
bool autoBind = true,
|
||||
|
@ -75,7 +75,7 @@ public:
|
||||
|
||||
int idle() const;
|
||||
/// Returns the number of seconds the session has not been used.
|
||||
|
||||
|
||||
private:
|
||||
SessionPool& _owner;
|
||||
Poco::AutoPtr<SessionImpl> _pImpl;
|
||||
|
@ -44,6 +44,8 @@
|
||||
#include "Poco/Data/PooledSessionHolder.h"
|
||||
#include "Poco/Data/PooledSessionImpl.h"
|
||||
#include "Poco/Data/Session.h"
|
||||
#include "Poco/HashMap.h"
|
||||
#include "Poco/Any.h"
|
||||
#include "Poco/Timer.h"
|
||||
#include "Poco/Mutex.h"
|
||||
#include <list>
|
||||
@ -53,7 +55,7 @@ namespace Poco {
|
||||
namespace Data {
|
||||
|
||||
|
||||
class Data_API SessionPool
|
||||
class Data_API SessionPool: public RefCountedObject
|
||||
/// This class implements session pooling for POCO Data.
|
||||
///
|
||||
/// Creating a connection to a database is often a time consuming
|
||||
@ -131,10 +133,27 @@ public:
|
||||
int available() const;
|
||||
/// Returns the number of available (idle + remaining capacity) sessions.
|
||||
|
||||
const std::string name() const;
|
||||
/// Returns the name for this pool.
|
||||
|
||||
void setFeature(const std::string& name, bool state);
|
||||
/// Sets feature for all the sessions.
|
||||
|
||||
bool getFeature(const std::string& name);
|
||||
/// Returns the requested feature.
|
||||
|
||||
void setProperty(const std::string& name, const Poco::Any& value);
|
||||
/// Sets property for all sessions.
|
||||
|
||||
Poco::Any getProperty(const std::string& name);
|
||||
/// Returns the requested property.
|
||||
|
||||
protected:
|
||||
typedef Poco::AutoPtr<PooledSessionHolder> PooledSessionHolderPtr;
|
||||
typedef Poco::AutoPtr<PooledSessionImpl> PooledSessionImplPtr;
|
||||
typedef std::list<PooledSessionHolderPtr> SessionList;
|
||||
typedef Poco::AutoPtr<PooledSessionHolder> PooledSessionHolderPtr;
|
||||
typedef Poco::AutoPtr<PooledSessionImpl> PooledSessionImplPtr;
|
||||
typedef std::list<PooledSessionHolderPtr> SessionList;
|
||||
typedef Poco::HashMap<std::string, bool> FeatureMap;
|
||||
typedef Poco::HashMap<std::string, Poco::Any> PropertyMap;
|
||||
|
||||
void purgeDeadSessions();
|
||||
int deadImpl(SessionList& rSessions);
|
||||
@ -154,12 +173,20 @@ private:
|
||||
SessionList _idleSessions;
|
||||
SessionList _activeSessions;
|
||||
Poco::Timer _janitorTimer;
|
||||
FeatureMap _featureMap;
|
||||
PropertyMap _propertyMap;
|
||||
mutable Poco::FastMutex _mutex;
|
||||
|
||||
friend class PooledSessionImpl;
|
||||
};
|
||||
|
||||
|
||||
inline const std::string SessionPool::name() const
|
||||
{
|
||||
return format("%s://%s", _sessionKey, _connectionString);
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Data
|
||||
|
||||
|
||||
|
108
Data/include/Poco/Data/SessionPoolContainer.h
Normal file
108
Data/include/Poco/Data/SessionPoolContainer.h
Normal file
@ -0,0 +1,108 @@
|
||||
//
|
||||
// SessionPoolContainer.h
|
||||
//
|
||||
// $Id: //poco/Main/Data/include/Poco/Data/SessionPoolContainer.h#4 $
|
||||
//
|
||||
// Library: Data
|
||||
// Package: SessionPoolContainering
|
||||
// Module: SessionPoolContainer
|
||||
//
|
||||
// Definition of the SessionPoolContainer class.
|
||||
//
|
||||
// Copyright (c) 2006, 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 Data_SessionPoolContainer_INCLUDED
|
||||
#define Data_SessionPoolContainer_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Data/Data.h"
|
||||
#include "Poco/Data/Session.h"
|
||||
#include "Poco/Data/SessionPool.h"
|
||||
#include "Poco/HashMap.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Data {
|
||||
|
||||
|
||||
class Data_API SessionPoolContainer
|
||||
/// This class implements container of session pools.
|
||||
{
|
||||
public:
|
||||
SessionPoolContainer();
|
||||
/// Creates the SessionPoolContainer for sessions with the given session parameters.
|
||||
|
||||
~SessionPoolContainer();
|
||||
/// Destroys the SessionPoolContainer.
|
||||
|
||||
void add(SessionPool* pPool);
|
||||
/// Adds existing session pool to the container.
|
||||
|
||||
Session add(const std::string& sessionKey,
|
||||
const std::string& connectionString,
|
||||
int minSessions = 1,
|
||||
int maxSessions = 32,
|
||||
int idleTime = 60);
|
||||
/// Adds a new session pool to the container and returns a Session from
|
||||
/// newly created pool.
|
||||
|
||||
Session get(const std::string& name);
|
||||
/// Returns a Session.
|
||||
|
||||
void remove(const std::string& name);
|
||||
/// Removes a SessionPool.
|
||||
|
||||
int count() const;
|
||||
/// Returns the number of session pols in the container.
|
||||
|
||||
private:
|
||||
typedef HashMap<std::string, AutoPtr<SessionPool> > SessionPoolMap;
|
||||
|
||||
SessionPoolContainer(const SessionPoolContainer&);
|
||||
SessionPoolContainer& operator = (const SessionPoolContainer&);
|
||||
|
||||
SessionPoolMap _sessionPools;
|
||||
};
|
||||
|
||||
|
||||
inline void SessionPoolContainer::remove(const std::string& name)
|
||||
{
|
||||
_sessionPools.erase(name);
|
||||
}
|
||||
|
||||
|
||||
inline int SessionPoolContainer::count() const
|
||||
{
|
||||
return static_cast<int>(_sessionPools.size());
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Data
|
||||
|
||||
|
||||
#endif // Data_SessionPoolContainer_INCLUDED
|
@ -83,6 +83,15 @@ Session SessionPool::get()
|
||||
}
|
||||
PooledSessionHolderPtr pHolder(_idleSessions.front());
|
||||
PooledSessionImplPtr pPSI(new PooledSessionImpl(pHolder));
|
||||
|
||||
FeatureMap::Iterator fmIt = _featureMap.begin();
|
||||
FeatureMap::Iterator fmEnd = _featureMap.end();
|
||||
for (; fmIt != fmEnd; ++fmIt) pPSI->setFeature(fmIt->first, fmIt->second);
|
||||
|
||||
PropertyMap::Iterator pmIt = _propertyMap.begin();
|
||||
PropertyMap::Iterator pmEnd = _propertyMap.end();
|
||||
for (; pmIt != pmEnd; ++pmIt) pPSI->setProperty(pmIt->first, pmIt->second);
|
||||
|
||||
_activeSessions.push_front(pHolder);
|
||||
_idleSessions.pop_front();
|
||||
return Session(pPSI);
|
||||
@ -158,6 +167,50 @@ int SessionPool::available() const
|
||||
}
|
||||
|
||||
|
||||
void SessionPool::setFeature(const std::string& name, bool state)
|
||||
{
|
||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||
|
||||
if (_nSessions > 0)
|
||||
throw InvalidAccessException("Features can not be set after the first session was created.");
|
||||
|
||||
_featureMap.insert(FeatureMap::ValueType(name, state));
|
||||
}
|
||||
|
||||
|
||||
bool SessionPool::getFeature(const std::string& name)
|
||||
{
|
||||
FeatureMap::ConstIterator it = _featureMap.find(name);
|
||||
|
||||
if (_featureMap.end() == it)
|
||||
throw NotFoundException("Feature not found:" + name);
|
||||
|
||||
return it->second;
|
||||
}
|
||||
|
||||
|
||||
void SessionPool::setProperty(const std::string& name, const Poco::Any& value)
|
||||
{
|
||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||
|
||||
if (_nSessions > 0)
|
||||
throw InvalidAccessException("Properties can not be set after first session was created.");
|
||||
|
||||
_propertyMap.insert(PropertyMap::ValueType(name, value));
|
||||
}
|
||||
|
||||
|
||||
Poco::Any SessionPool::getProperty(const std::string& name)
|
||||
{
|
||||
PropertyMap::ConstIterator it = _propertyMap.find(name);
|
||||
|
||||
if (_propertyMap.end() == it)
|
||||
throw NotFoundException("Property not found:" + name);
|
||||
|
||||
return it->second;
|
||||
}
|
||||
|
||||
|
||||
void SessionPool::putBack(PooledSessionHolderPtr pHolder)
|
||||
{
|
||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||
|
98
Data/src/SessionPoolContainer.cpp
Normal file
98
Data/src/SessionPoolContainer.cpp
Normal file
@ -0,0 +1,98 @@
|
||||
//
|
||||
// SessionPoolContainer.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Data/src/SessionPoolContainer.cpp#3 $
|
||||
//
|
||||
// Library: Data
|
||||
// Package: SessionPoolContainering
|
||||
// Module: SessionPoolContainer
|
||||
//
|
||||
// Copyright (c) 2006, 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.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Data/SessionPoolContainer.h"
|
||||
#include "Poco/Data/SessionFactory.h"
|
||||
#include "Poco/Data/DataException.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Data {
|
||||
|
||||
|
||||
SessionPoolContainer::SessionPoolContainer()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
SessionPoolContainer::~SessionPoolContainer()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void SessionPoolContainer::add(SessionPool* pPool)
|
||||
{
|
||||
poco_check_ptr (pPool);
|
||||
|
||||
if (_sessionPools.find(pPool->name()) != _sessionPools.end())
|
||||
throw InvalidAccessException("Session pool already exists: " + pPool->name());
|
||||
|
||||
_sessionPools.insert(SessionPoolMap::ValueType(pPool->name(), pPool));
|
||||
}
|
||||
|
||||
|
||||
Session SessionPoolContainer::add(const std::string& sessionKey,
|
||||
const std::string& connectionString,
|
||||
int minSessions,
|
||||
int maxSessions,
|
||||
int idleTime)
|
||||
{
|
||||
AutoPtr<SessionPool> pSP =
|
||||
new SessionPool(sessionKey, connectionString, minSessions, maxSessions, idleTime);
|
||||
|
||||
std::string name = pSP->name();
|
||||
|
||||
if (_sessionPools.find(name) != _sessionPools.end())
|
||||
throw InvalidAccessException("Session pool already exists: " + name);
|
||||
|
||||
std::pair<SessionPoolMap::Iterator, bool> ins =
|
||||
_sessionPools.insert(SessionPoolMap::ValueType(name, pSP));
|
||||
|
||||
return ins.first->second->get();
|
||||
}
|
||||
|
||||
|
||||
Session SessionPoolContainer::get(const std::string& name)
|
||||
{
|
||||
SessionPoolMap::Iterator it = _sessionPools.find(name);
|
||||
if (_sessionPools.end() == it) throw NotFoundException(name);
|
||||
return it->second->get();
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Data
|
@ -40,7 +40,6 @@ namespace Test {
|
||||
|
||||
|
||||
SessionImpl::SessionImpl(const std::string& init):
|
||||
_init(init),
|
||||
_f(false),
|
||||
_connected(true)
|
||||
{
|
||||
|
@ -90,7 +90,6 @@ public:
|
||||
Poco::Any getP(const std::string& name);
|
||||
|
||||
private:
|
||||
std::string _init;
|
||||
bool _f;
|
||||
Poco::Any _p;
|
||||
bool _connected;
|
||||
|
@ -34,14 +34,21 @@
|
||||
#include "CppUnit/TestCaller.h"
|
||||
#include "CppUnit/TestSuite.h"
|
||||
#include "Poco/Data/SessionPool.h"
|
||||
#include "Poco/Data/SessionPoolContainer.h"
|
||||
#include "Poco/Thread.h"
|
||||
#include "Poco/AutoPtr.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Connector.h"
|
||||
|
||||
|
||||
using namespace Poco::Data::Keywords;
|
||||
using Poco::Thread;
|
||||
using Poco::AutoPtr;
|
||||
using Poco::NotFoundException;
|
||||
using Poco::InvalidAccessException;
|
||||
using Poco::Data::Session;
|
||||
using Poco::Data::SessionPool;
|
||||
using Poco::Data::SessionPoolContainer;
|
||||
using Poco::Data::SessionPoolExhaustedException;
|
||||
using Poco::Data::SessionUnavailableException;
|
||||
|
||||
@ -62,6 +69,16 @@ void SessionPoolTest::testSessionPool()
|
||||
{
|
||||
SessionPool pool("test", "cs", 1, 4, 5);
|
||||
|
||||
pool.setFeature("f1", true);
|
||||
assert (pool.getFeature("f1"));
|
||||
try { pool.getFeature("g1"); fail ("must fail"); }
|
||||
catch ( Poco::NotFoundException& ) { }
|
||||
|
||||
pool.setProperty("p1", 1);
|
||||
assert (1 == Poco::AnyCast<int>(pool.getProperty("p1")));
|
||||
try { pool.getProperty("r1"); fail ("must fail"); }
|
||||
catch ( Poco::NotFoundException& ) { }
|
||||
|
||||
assert (pool.capacity() == 4);
|
||||
assert (pool.allocated() == 0);
|
||||
assert (pool.idle() == 0);
|
||||
@ -70,6 +87,12 @@ void SessionPoolTest::testSessionPool()
|
||||
assert (pool.allocated() == pool.used() + pool.idle());
|
||||
Session s1(pool.get());
|
||||
|
||||
try { pool.setFeature("f1", true); fail ("must fail"); }
|
||||
catch ( Poco::InvalidAccessException& ) { }
|
||||
|
||||
try { pool.setProperty("p1", 1); fail ("must fail"); }
|
||||
catch ( Poco::InvalidAccessException& ) { }
|
||||
|
||||
assert (pool.capacity() == 4);
|
||||
assert (pool.allocated() == 1);
|
||||
assert (pool.idle() == 0);
|
||||
@ -187,6 +210,28 @@ void SessionPoolTest::testSessionPool()
|
||||
}
|
||||
|
||||
|
||||
void SessionPoolTest::testSessionPoolContainer()
|
||||
{
|
||||
SessionPoolContainer spc;
|
||||
AutoPtr<SessionPool> pPool = new SessionPool("test", "cs");
|
||||
spc.add(pPool);
|
||||
assert (1 == spc.count());
|
||||
try { spc.add(pPool); fail ("must fail"); }
|
||||
catch (InvalidAccessException&) { }
|
||||
spc.remove(pPool->name());
|
||||
assert (0 == spc.count());
|
||||
try { spc.get("test"); fail ("must fail"); }
|
||||
catch (NotFoundException&) { }
|
||||
|
||||
spc.add("test", "cs");
|
||||
assert (1 == spc.count());
|
||||
spc.remove("test://cs");
|
||||
assert (0 == spc.count());
|
||||
try { spc.get("test"); fail ("must fail"); }
|
||||
catch (NotFoundException&) { }
|
||||
}
|
||||
|
||||
|
||||
void SessionPoolTest::setUp()
|
||||
{
|
||||
}
|
||||
@ -202,6 +247,7 @@ CppUnit::Test* SessionPoolTest::suite()
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SessionPoolTest");
|
||||
|
||||
CppUnit_addTest(pSuite, SessionPoolTest, testSessionPool);
|
||||
CppUnit_addTest(pSuite, SessionPoolTest, testSessionPoolContainer);
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
|
@ -47,6 +47,7 @@ public:
|
||||
~SessionPoolTest();
|
||||
|
||||
void testSessionPool();
|
||||
void testSessionPoolContainer();
|
||||
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
Loading…
x
Reference in New Issue
Block a user