mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-27 19:10:20 +01:00
SessionPool::name() (maybe this should go into session?)
SessionPool::shutdown() (hanging on destruction for static object - Timer?)
This commit is contained in:
@@ -133,9 +133,13 @@ public:
|
|||||||
int available() const;
|
int available() const;
|
||||||
/// Returns the number of available (idle + remaining capacity) sessions.
|
/// Returns the number of available (idle + remaining capacity) sessions.
|
||||||
|
|
||||||
const std::string name() const;
|
std::string name() const;
|
||||||
/// Returns the name for this pool.
|
/// Returns the name for this pool.
|
||||||
|
|
||||||
|
static std::string name(const std::string sessionKey,
|
||||||
|
const std::string& connectionString);
|
||||||
|
/// Returns the name formatted from supplied arguments as "sessionKey://connectionString".
|
||||||
|
|
||||||
void setFeature(const std::string& name, bool state);
|
void setFeature(const std::string& name, bool state);
|
||||||
/// Sets feature for all the sessions.
|
/// Sets feature for all the sessions.
|
||||||
|
|
||||||
@@ -148,6 +152,8 @@ public:
|
|||||||
Poco::Any getProperty(const std::string& name);
|
Poco::Any getProperty(const std::string& name);
|
||||||
/// Returns the requested property.
|
/// Returns the requested property.
|
||||||
|
|
||||||
|
void shutdown();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
typedef Poco::AutoPtr<PooledSessionHolder> PooledSessionHolderPtr;
|
typedef Poco::AutoPtr<PooledSessionHolder> PooledSessionHolderPtr;
|
||||||
typedef Poco::AutoPtr<PooledSessionImpl> PooledSessionImplPtr;
|
typedef Poco::AutoPtr<PooledSessionImpl> PooledSessionImplPtr;
|
||||||
@@ -164,6 +170,8 @@ private:
|
|||||||
SessionPool(const SessionPool&);
|
SessionPool(const SessionPool&);
|
||||||
SessionPool& operator = (const SessionPool&);
|
SessionPool& operator = (const SessionPool&);
|
||||||
|
|
||||||
|
void closeAll(SessionList& sessionList);
|
||||||
|
|
||||||
std::string _sessionKey;
|
std::string _sessionKey;
|
||||||
std::string _connectionString;
|
std::string _connectionString;
|
||||||
int _minSessions;
|
int _minSessions;
|
||||||
@@ -175,15 +183,23 @@ private:
|
|||||||
Poco::Timer _janitorTimer;
|
Poco::Timer _janitorTimer;
|
||||||
FeatureMap _featureMap;
|
FeatureMap _featureMap;
|
||||||
PropertyMap _propertyMap;
|
PropertyMap _propertyMap;
|
||||||
|
bool _shutdown;
|
||||||
mutable Poco::FastMutex _mutex;
|
mutable Poco::FastMutex _mutex;
|
||||||
|
|
||||||
friend class PooledSessionImpl;
|
friend class PooledSessionImpl;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
inline const std::string SessionPool::name() const
|
inline std::string SessionPool::name(const std::string sessionKey,
|
||||||
|
const std::string& connectionString)
|
||||||
{
|
{
|
||||||
return format("%s://%s", _sessionKey, _connectionString);
|
return format("%s://%s", sessionKey, connectionString);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline std::string SessionPool::name() const
|
||||||
|
{
|
||||||
|
return name(_sessionKey, _connectionString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -80,6 +80,9 @@ public:
|
|||||||
int count() const;
|
int count() const;
|
||||||
/// Returns the number of session pols in the container.
|
/// Returns the number of session pols in the container.
|
||||||
|
|
||||||
|
void shutdown();
|
||||||
|
/// Shuts down all the held pools.
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef HashMap<std::string, AutoPtr<SessionPool> > SessionPoolMap;
|
typedef HashMap<std::string, AutoPtr<SessionPool> > SessionPoolMap;
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,8 @@ SessionPool::SessionPool(const std::string& sessionKey, const std::string& conne
|
|||||||
_maxSessions(maxSessions),
|
_maxSessions(maxSessions),
|
||||||
_idleTime(idleTime),
|
_idleTime(idleTime),
|
||||||
_nSessions(0),
|
_nSessions(0),
|
||||||
_janitorTimer(1000*idleTime, 1000*idleTime/4)
|
_janitorTimer(1000*idleTime, 1000*idleTime/4),
|
||||||
|
_shutdown(false)
|
||||||
{
|
{
|
||||||
Poco::TimerCallback<SessionPool> callback(*this, &SessionPool::onJanitorTimer);
|
Poco::TimerCallback<SessionPool> callback(*this, &SessionPool::onJanitorTimer);
|
||||||
_janitorTimer.start(callback);
|
_janitorTimer.start(callback);
|
||||||
@@ -60,14 +61,15 @@ SessionPool::SessionPool(const std::string& sessionKey, const std::string& conne
|
|||||||
|
|
||||||
SessionPool::~SessionPool()
|
SessionPool::~SessionPool()
|
||||||
{
|
{
|
||||||
_janitorTimer.stop();
|
shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Session SessionPool::get()
|
Session SessionPool::get()
|
||||||
{
|
{
|
||||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||||
|
if (_shutdown) throw InvalidAccessException("Session pull has been shut down.");
|
||||||
|
|
||||||
purgeDeadSessions();
|
purgeDeadSessions();
|
||||||
|
|
||||||
if (_idleSessions.empty())
|
if (_idleSessions.empty())
|
||||||
@@ -75,23 +77,25 @@ Session SessionPool::get()
|
|||||||
if (_nSessions < _maxSessions)
|
if (_nSessions < _maxSessions)
|
||||||
{
|
{
|
||||||
Session newSession(SessionFactory::instance().create(_sessionKey, _connectionString));
|
Session newSession(SessionFactory::instance().create(_sessionKey, _connectionString));
|
||||||
|
|
||||||
|
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);
|
||||||
++_nSessions;
|
++_nSessions;
|
||||||
}
|
}
|
||||||
else throw SessionPoolExhaustedException(_sessionKey, _connectionString);
|
else throw SessionPoolExhaustedException(_sessionKey, _connectionString);
|
||||||
}
|
}
|
||||||
|
|
||||||
PooledSessionHolderPtr pHolder(_idleSessions.front());
|
PooledSessionHolderPtr pHolder(_idleSessions.front());
|
||||||
PooledSessionImplPtr pPSI(new PooledSessionImpl(pHolder));
|
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);
|
_activeSessions.push_front(pHolder);
|
||||||
_idleSessions.pop_front();
|
_idleSessions.pop_front();
|
||||||
return Session(pPSI);
|
return Session(pPSI);
|
||||||
@@ -100,6 +104,9 @@ Session SessionPool::get()
|
|||||||
|
|
||||||
void SessionPool::purgeDeadSessions()
|
void SessionPool::purgeDeadSessions()
|
||||||
{
|
{
|
||||||
|
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||||
|
if (_shutdown) return;
|
||||||
|
|
||||||
SessionList::iterator it = _idleSessions.begin();
|
SessionList::iterator it = _idleSessions.begin();
|
||||||
for (; it != _idleSessions.end(); )
|
for (; it != _idleSessions.end(); )
|
||||||
{
|
{
|
||||||
@@ -122,7 +129,6 @@ int SessionPool::capacity() const
|
|||||||
int SessionPool::used() const
|
int SessionPool::used() const
|
||||||
{
|
{
|
||||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||||
|
|
||||||
return (int) _activeSessions.size();
|
return (int) _activeSessions.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +136,6 @@ int SessionPool::used() const
|
|||||||
int SessionPool::idle() const
|
int SessionPool::idle() const
|
||||||
{
|
{
|
||||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||||
|
|
||||||
return (int) _idleSessions.size();
|
return (int) _idleSessions.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,7 +143,6 @@ int SessionPool::idle() const
|
|||||||
int SessionPool::dead()
|
int SessionPool::dead()
|
||||||
{
|
{
|
||||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
SessionList::iterator it = _activeSessions.begin();
|
SessionList::iterator it = _activeSessions.begin();
|
||||||
@@ -156,13 +160,13 @@ int SessionPool::dead()
|
|||||||
int SessionPool::allocated() const
|
int SessionPool::allocated() const
|
||||||
{
|
{
|
||||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||||
|
|
||||||
return _nSessions;
|
return _nSessions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int SessionPool::available() const
|
int SessionPool::available() const
|
||||||
{
|
{
|
||||||
|
if (_shutdown) return 0;
|
||||||
return _maxSessions - used();
|
return _maxSessions - used();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,6 +174,7 @@ int SessionPool::available() const
|
|||||||
void SessionPool::setFeature(const std::string& name, bool state)
|
void SessionPool::setFeature(const std::string& name, bool state)
|
||||||
{
|
{
|
||||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||||
|
if (_shutdown) throw InvalidAccessException("Session pull has been shut down.");
|
||||||
|
|
||||||
if (_nSessions > 0)
|
if (_nSessions > 0)
|
||||||
throw InvalidAccessException("Features can not be set after the first session was created.");
|
throw InvalidAccessException("Features can not be set after the first session was created.");
|
||||||
@@ -181,6 +186,7 @@ void SessionPool::setFeature(const std::string& name, bool state)
|
|||||||
bool SessionPool::getFeature(const std::string& name)
|
bool SessionPool::getFeature(const std::string& name)
|
||||||
{
|
{
|
||||||
FeatureMap::ConstIterator it = _featureMap.find(name);
|
FeatureMap::ConstIterator it = _featureMap.find(name);
|
||||||
|
if (_shutdown) throw InvalidAccessException("Session pull has been shut down.");
|
||||||
|
|
||||||
if (_featureMap.end() == it)
|
if (_featureMap.end() == it)
|
||||||
throw NotFoundException("Feature not found:" + name);
|
throw NotFoundException("Feature not found:" + name);
|
||||||
@@ -192,6 +198,7 @@ bool SessionPool::getFeature(const std::string& name)
|
|||||||
void SessionPool::setProperty(const std::string& name, const Poco::Any& value)
|
void SessionPool::setProperty(const std::string& name, const Poco::Any& value)
|
||||||
{
|
{
|
||||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||||
|
if (_shutdown) throw InvalidAccessException("Session pull has been shut down.");
|
||||||
|
|
||||||
if (_nSessions > 0)
|
if (_nSessions > 0)
|
||||||
throw InvalidAccessException("Properties can not be set after first session was created.");
|
throw InvalidAccessException("Properties can not be set after first session was created.");
|
||||||
@@ -214,6 +221,7 @@ Poco::Any SessionPool::getProperty(const std::string& name)
|
|||||||
void SessionPool::putBack(PooledSessionHolderPtr pHolder)
|
void SessionPool::putBack(PooledSessionHolderPtr pHolder)
|
||||||
{
|
{
|
||||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||||
|
if (_shutdown) return;
|
||||||
|
|
||||||
SessionList::iterator it = std::find(_activeSessions.begin(), _activeSessions.end(), pHolder);
|
SessionList::iterator it = std::find(_activeSessions.begin(), _activeSessions.end(), pHolder);
|
||||||
if (it != _activeSessions.end())
|
if (it != _activeSessions.end())
|
||||||
@@ -237,19 +245,15 @@ void SessionPool::putBack(PooledSessionHolderPtr pHolder)
|
|||||||
void SessionPool::onJanitorTimer(Poco::Timer&)
|
void SessionPool::onJanitorTimer(Poco::Timer&)
|
||||||
{
|
{
|
||||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||||
|
if (_shutdown) return;
|
||||||
|
|
||||||
SessionList::iterator it = _idleSessions.begin();
|
SessionList::iterator it = _idleSessions.begin();
|
||||||
while (_nSessions > _minSessions && it != _idleSessions.end())
|
while (_nSessions > _minSessions && it != _idleSessions.end())
|
||||||
{
|
{
|
||||||
if ((*it)->idle() > _idleTime || !(*it)->session()->isConnected())
|
if ((*it)->idle() > _idleTime || !(*it)->session()->isConnected())
|
||||||
{
|
{
|
||||||
try
|
try { (*it)->session()->close(); }
|
||||||
{
|
catch (...) { }
|
||||||
(*it)->session()->close();
|
|
||||||
}
|
|
||||||
catch (...)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
it = _idleSessions.erase(it);
|
it = _idleSessions.erase(it);
|
||||||
--_nSessions;
|
--_nSessions;
|
||||||
}
|
}
|
||||||
@@ -258,4 +262,28 @@ void SessionPool::onJanitorTimer(Poco::Timer&)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SessionPool::shutdown()
|
||||||
|
{
|
||||||
|
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||||
|
if (_shutdown) return;
|
||||||
|
_shutdown = true;
|
||||||
|
_janitorTimer.stop();
|
||||||
|
closeAll(_idleSessions);
|
||||||
|
closeAll(_activeSessions);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SessionPool::closeAll(SessionList& sessionList)
|
||||||
|
{
|
||||||
|
SessionList::iterator it = sessionList.begin();
|
||||||
|
for (; it != sessionList.end();)
|
||||||
|
{
|
||||||
|
try { (*it)->session()->close(); }
|
||||||
|
catch (...) { }
|
||||||
|
it = sessionList.erase(it);
|
||||||
|
if (_nSessions > 0) --_nSessions;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} } // namespace Poco::Data
|
} } // namespace Poco::Data
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ void SessionPoolContainer::add(SessionPool* pPool)
|
|||||||
if (_sessionPools.find(pPool->name()) != _sessionPools.end())
|
if (_sessionPools.find(pPool->name()) != _sessionPools.end())
|
||||||
throw InvalidAccessException("Session pool already exists: " + pPool->name());
|
throw InvalidAccessException("Session pool already exists: " + pPool->name());
|
||||||
|
|
||||||
|
pPool->duplicate();
|
||||||
_sessionPools.insert(SessionPoolMap::ValueType(pPool->name(), pPool));
|
_sessionPools.insert(SessionPoolMap::ValueType(pPool->name(), pPool));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +73,7 @@ Session SessionPoolContainer::add(const std::string& sessionKey,
|
|||||||
int maxSessions,
|
int maxSessions,
|
||||||
int idleTime)
|
int idleTime)
|
||||||
{
|
{
|
||||||
AutoPtr<SessionPool> pSP =
|
SessionPool* pSP =
|
||||||
new SessionPool(sessionKey, connectionString, minSessions, maxSessions, idleTime);
|
new SessionPool(sessionKey, connectionString, minSessions, maxSessions, idleTime);
|
||||||
|
|
||||||
std::string name = pSP->name();
|
std::string name = pSP->name();
|
||||||
@@ -95,4 +96,12 @@ Session SessionPoolContainer::get(const std::string& name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SessionPoolContainer::shutdown()
|
||||||
|
{
|
||||||
|
SessionPoolMap::Iterator it = _sessionPools.begin();
|
||||||
|
SessionPoolMap::Iterator end = _sessionPools.end();
|
||||||
|
for (; it != end; ++it) it->second->shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} } // namespace Poco::Data
|
} } // namespace Poco::Data
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ void SessionImpl::rollback()
|
|||||||
|
|
||||||
void SessionImpl::close()
|
void SessionImpl::close()
|
||||||
{
|
{
|
||||||
|
_connected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ SessionPoolTest::~SessionPoolTest()
|
|||||||
|
|
||||||
void SessionPoolTest::testSessionPool()
|
void SessionPoolTest::testSessionPool()
|
||||||
{
|
{
|
||||||
SessionPool pool("test", "cs", 1, 4, 5);
|
SessionPool pool("test", "cs", 1, 4, 2);
|
||||||
|
|
||||||
pool.setFeature("f1", true);
|
pool.setFeature("f1", true);
|
||||||
assert (pool.getFeature("f1"));
|
assert (pool.getFeature("f1"));
|
||||||
@@ -150,9 +150,7 @@ void SessionPoolTest::testSessionPool()
|
|||||||
Session s6(pool.get());
|
Session s6(pool.get());
|
||||||
fail("pool exhausted - must throw");
|
fail("pool exhausted - must throw");
|
||||||
}
|
}
|
||||||
catch (SessionPoolExhaustedException&)
|
catch (SessionPoolExhaustedException&) { }
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
s5.close();
|
s5.close();
|
||||||
assert (pool.capacity() == 4);
|
assert (pool.capacity() == 4);
|
||||||
@@ -167,9 +165,7 @@ void SessionPoolTest::testSessionPool()
|
|||||||
s5 << "DROP TABLE IF EXISTS Test", now;
|
s5 << "DROP TABLE IF EXISTS Test", now;
|
||||||
fail("session unusable - must throw");
|
fail("session unusable - must throw");
|
||||||
}
|
}
|
||||||
catch (SessionUnavailableException&)
|
catch (SessionUnavailableException&) { }
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
s4.close();
|
s4.close();
|
||||||
assert (pool.capacity() == 4);
|
assert (pool.capacity() == 4);
|
||||||
@@ -179,7 +175,7 @@ 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());
|
||||||
|
|
||||||
Thread::sleep(10000); // time to clean up idle sessions
|
Thread::sleep(5000); // time to clean up idle sessions
|
||||||
|
|
||||||
assert (pool.capacity() == 4);
|
assert (pool.capacity() == 4);
|
||||||
assert (pool.allocated() == 2);
|
assert (pool.allocated() == 2);
|
||||||
@@ -207,6 +203,21 @@ void SessionPoolTest::testSessionPool()
|
|||||||
assert (pool.available() == 2);
|
assert (pool.available() == 2);
|
||||||
assert (pool.dead() == 0);
|
assert (pool.dead() == 0);
|
||||||
assert (pool.allocated() == pool.used() + pool.idle());
|
assert (pool.allocated() == pool.used() + pool.idle());
|
||||||
|
|
||||||
|
pool.shutdown();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Session s7(pool.get());
|
||||||
|
fail("pool shut down - must throw");
|
||||||
|
}
|
||||||
|
catch (InvalidAccessException&) { }
|
||||||
|
|
||||||
|
assert (pool.capacity() == 4);
|
||||||
|
assert (pool.allocated() == 0);
|
||||||
|
assert (pool.idle() == 0);
|
||||||
|
assert (pool.available() == 0);
|
||||||
|
assert (pool.dead() == 0);
|
||||||
|
assert (pool.allocated() == pool.used() + pool.idle());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user