mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-01 09:24:55 +02:00
preserve/restore session features/properties within the pool
This commit is contained in:
parent
0bd881fd5e
commit
810cfd2ff4
@ -115,6 +115,27 @@ public:
|
|||||||
/// already been created, a SessionPoolExhaustedException
|
/// already been created, a SessionPoolExhaustedException
|
||||||
/// is thrown.
|
/// is thrown.
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Session get(const std::string& name, const T& value)
|
||||||
|
/// Returns a Session with requested property set.
|
||||||
|
/// The property can be diferent from the default pool
|
||||||
|
/// value, in which case it is reset back to the pool
|
||||||
|
/// value when the session is reclaimed by the pool.
|
||||||
|
{
|
||||||
|
Session& s = get();
|
||||||
|
_addPropertyMap.insert(AddPropertyMap::value_type(s.impl(),
|
||||||
|
std::make_pair(name, s.getProperty(name))));
|
||||||
|
s.setProperty(name, value);
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
Session get(const std::string& name, bool value);
|
||||||
|
/// Returns a Session with requested feature set.
|
||||||
|
/// The feature can be diferent from the default pool
|
||||||
|
/// value, in which case it is reset back to the pool
|
||||||
|
/// value when the session is reclaimed by the pool.
|
||||||
|
|
||||||
int capacity() const;
|
int capacity() const;
|
||||||
/// Returns the maximum number of sessions the SessionPool will manage.
|
/// Returns the maximum number of sessions the SessionPool will manage.
|
||||||
|
|
||||||
@ -163,10 +184,16 @@ protected:
|
|||||||
|
|
||||||
void purgeDeadSessions();
|
void purgeDeadSessions();
|
||||||
int deadImpl(SessionList& rSessions);
|
int deadImpl(SessionList& rSessions);
|
||||||
|
void applySettings(SessionImpl* pImpl);
|
||||||
void putBack(PooledSessionHolderPtr pHolder);
|
void putBack(PooledSessionHolderPtr pHolder);
|
||||||
void onJanitorTimer(Poco::Timer&);
|
void onJanitorTimer(Poco::Timer&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
typedef std::pair<std::string, Poco::Any> PropertyPair;
|
||||||
|
typedef std::pair<std::string, bool> FeaturePair;
|
||||||
|
typedef std::map<SessionImpl*, PropertyPair> AddPropertyMap;
|
||||||
|
typedef std::map<SessionImpl*, FeaturePair> AddFeatureMap;
|
||||||
|
|
||||||
SessionPool(const SessionPool&);
|
SessionPool(const SessionPool&);
|
||||||
SessionPool& operator = (const SessionPool&);
|
SessionPool& operator = (const SessionPool&);
|
||||||
|
|
||||||
@ -184,6 +211,8 @@ private:
|
|||||||
FeatureMap _featureMap;
|
FeatureMap _featureMap;
|
||||||
PropertyMap _propertyMap;
|
PropertyMap _propertyMap;
|
||||||
bool _shutdown;
|
bool _shutdown;
|
||||||
|
AddPropertyMap _addPropertyMap;
|
||||||
|
AddFeatureMap _addFeatureMap;
|
||||||
mutable
|
mutable
|
||||||
Poco::Mutex _mutex;
|
Poco::Mutex _mutex;
|
||||||
|
|
||||||
|
@ -65,6 +65,17 @@ SessionPool::~SessionPool()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Session SessionPool::get(const std::string& name, bool value)
|
||||||
|
{
|
||||||
|
Session& s = get();
|
||||||
|
_addFeatureMap.insert(AddFeatureMap::value_type(s.impl(),
|
||||||
|
std::make_pair(name, s.getFeature(name))));
|
||||||
|
s.setFeature(name, value);
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Session SessionPool::get()
|
Session SessionPool::get()
|
||||||
{
|
{
|
||||||
Poco::Mutex::ScopedLock lock(_mutex);
|
Poco::Mutex::ScopedLock lock(_mutex);
|
||||||
@ -77,14 +88,7 @@ Session SessionPool::get()
|
|||||||
if (_nSessions < _maxSessions)
|
if (_nSessions < _maxSessions)
|
||||||
{
|
{
|
||||||
Session newSession(SessionFactory::instance().create(_connector, _connectionString));
|
Session newSession(SessionFactory::instance().create(_connector, _connectionString));
|
||||||
|
applySettings(newSession.impl());
|
||||||
FeatureMap::Iterator fmIt = _featureMap.begin();
|
|
||||||
FeatureMap::Iterator fmEnd = _featureMap.end();
|
|
||||||
for (; fmIt != fmEnd; ++fmIt) newSession.setFeature(fmIt->first, fmIt->second);
|
|
||||||
|
|
||||||
PropertyMap::Iterator pmIt = _propertyMap.begin();
|
|
||||||
PropertyMap::Iterator pmEnd = _propertyMap.end();
|
|
||||||
for (; pmIt != pmEnd; ++pmIt) newSession.setProperty(pmIt->first, pmIt->second);
|
|
||||||
|
|
||||||
PooledSessionHolderPtr pHolder(new PooledSessionHolder(*this, newSession.impl()));
|
PooledSessionHolderPtr pHolder(new PooledSessionHolder(*this, newSession.impl()));
|
||||||
_idleSessions.push_front(pHolder);
|
_idleSessions.push_front(pHolder);
|
||||||
@ -218,6 +222,18 @@ Poco::Any SessionPool::getProperty(const std::string& name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SessionPool::applySettings(SessionImpl* pImpl)
|
||||||
|
{
|
||||||
|
FeatureMap::Iterator fmIt = _featureMap.begin();
|
||||||
|
FeatureMap::Iterator fmEnd = _featureMap.end();
|
||||||
|
for (; fmIt != fmEnd; ++fmIt) pImpl->setFeature(fmIt->first, fmIt->second);
|
||||||
|
|
||||||
|
PropertyMap::Iterator pmIt = _propertyMap.begin();
|
||||||
|
PropertyMap::Iterator pmEnd = _propertyMap.end();
|
||||||
|
for (; pmIt != pmEnd; ++pmIt) pImpl->setProperty(pmIt->first, pmIt->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void SessionPool::putBack(PooledSessionHolderPtr pHolder)
|
void SessionPool::putBack(PooledSessionHolderPtr pHolder)
|
||||||
{
|
{
|
||||||
Poco::Mutex::ScopedLock lock(_mutex);
|
Poco::Mutex::ScopedLock lock(_mutex);
|
||||||
@ -228,6 +244,18 @@ void SessionPool::putBack(PooledSessionHolderPtr pHolder)
|
|||||||
{
|
{
|
||||||
if (pHolder->session()->isConnected())
|
if (pHolder->session()->isConnected())
|
||||||
{
|
{
|
||||||
|
// reverse settings applied at acquisition time, if any
|
||||||
|
AddPropertyMap::iterator pIt = _addPropertyMap.find(pHolder->session());
|
||||||
|
if (pIt != _addPropertyMap.end())
|
||||||
|
pHolder->session()->setProperty(pIt->second.first, pIt->second.second);
|
||||||
|
|
||||||
|
AddFeatureMap::iterator fIt = _addFeatureMap.find(pHolder->session());
|
||||||
|
if (fIt != _addFeatureMap.end())
|
||||||
|
pHolder->session()->setFeature(fIt->second.first, fIt->second.second);
|
||||||
|
|
||||||
|
// re-apply the default pool settings
|
||||||
|
applySettings(pHolder->session());
|
||||||
|
|
||||||
pHolder->access();
|
pHolder->access();
|
||||||
_idleSessions.push_front(pHolder);
|
_idleSessions.push_front(pHolder);
|
||||||
}
|
}
|
||||||
|
@ -88,6 +88,9 @@ void SessionPoolTest::testSessionPool()
|
|||||||
assert (pool.allocated() == pool.used() + pool.idle());
|
assert (pool.allocated() == pool.used() + pool.idle());
|
||||||
Session s1(pool.get());
|
Session s1(pool.get());
|
||||||
|
|
||||||
|
assert (s1.getFeature("f1"));
|
||||||
|
assert (1 == Poco::AnyCast<int>(s1.getProperty("p1")));
|
||||||
|
|
||||||
try { pool.setFeature("f1", true); fail ("must fail"); }
|
try { pool.setFeature("f1", true); fail ("must fail"); }
|
||||||
catch ( Poco::InvalidAccessException& ) { }
|
catch ( Poco::InvalidAccessException& ) { }
|
||||||
|
|
||||||
@ -101,7 +104,9 @@ void SessionPoolTest::testSessionPool()
|
|||||||
assert (pool.dead() == 0);
|
assert (pool.dead() == 0);
|
||||||
assert (pool.allocated() == pool.used() + pool.idle());
|
assert (pool.allocated() == pool.used() + pool.idle());
|
||||||
|
|
||||||
Session s2(pool.get());
|
Session s2(pool.get("f1", false));
|
||||||
|
assert (!s2.getFeature("f1"));
|
||||||
|
assert (1 == Poco::AnyCast<int>(s2.getProperty("p1")));
|
||||||
|
|
||||||
assert (pool.capacity() == 4);
|
assert (pool.capacity() == 4);
|
||||||
assert (pool.allocated() == 2);
|
assert (pool.allocated() == 2);
|
||||||
@ -111,7 +116,9 @@ void SessionPoolTest::testSessionPool()
|
|||||||
assert (pool.allocated() == pool.used() + pool.idle());
|
assert (pool.allocated() == pool.used() + pool.idle());
|
||||||
|
|
||||||
{
|
{
|
||||||
Session s3(pool.get());
|
Session s3(pool.get("p1", 2));
|
||||||
|
assert (s3.getFeature("f1"));
|
||||||
|
assert (2 == Poco::AnyCast<int>(s3.getProperty("p1")));
|
||||||
|
|
||||||
assert (pool.capacity() == 4);
|
assert (pool.capacity() == 4);
|
||||||
assert (pool.allocated() == 3);
|
assert (pool.allocated() == 3);
|
||||||
@ -129,6 +136,8 @@ void SessionPoolTest::testSessionPool()
|
|||||||
assert (pool.allocated() == pool.used() + pool.idle());
|
assert (pool.allocated() == pool.used() + pool.idle());
|
||||||
|
|
||||||
Session s4(pool.get());
|
Session s4(pool.get());
|
||||||
|
assert (s4.getFeature("f1"));
|
||||||
|
assert (1 == Poco::AnyCast<int>(s4.getProperty("p1")));
|
||||||
|
|
||||||
assert (pool.capacity() == 4);
|
assert (pool.capacity() == 4);
|
||||||
assert (pool.allocated() == 3);
|
assert (pool.allocated() == 3);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user