mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-28 19:51:58 +01:00
added SessionPoolExistsException
when duplicate session add is requested by name, it is silently ignored
This commit is contained in:
@@ -59,6 +59,7 @@ POCO_DECLARE_EXCEPTION(Data_API, LimitException, DataException)
|
|||||||
POCO_DECLARE_EXCEPTION(Data_API, NotSupportedException, DataException)
|
POCO_DECLARE_EXCEPTION(Data_API, NotSupportedException, DataException)
|
||||||
POCO_DECLARE_EXCEPTION(Data_API, SessionUnavailableException, DataException)
|
POCO_DECLARE_EXCEPTION(Data_API, SessionUnavailableException, DataException)
|
||||||
POCO_DECLARE_EXCEPTION(Data_API, SessionPoolExhaustedException, DataException)
|
POCO_DECLARE_EXCEPTION(Data_API, SessionPoolExhaustedException, DataException)
|
||||||
|
POCO_DECLARE_EXCEPTION(Data_API, SessionPoolExistsException, DataException)
|
||||||
POCO_DECLARE_EXCEPTION(Data_API, NoDataException, DataException)
|
POCO_DECLARE_EXCEPTION(Data_API, NoDataException, DataException)
|
||||||
POCO_DECLARE_EXCEPTION(Data_API, LengthExceededException, DataException)
|
POCO_DECLARE_EXCEPTION(Data_API, LengthExceededException, DataException)
|
||||||
|
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ public:
|
|||||||
|
|
||||||
void add(SessionPool* pPool);
|
void add(SessionPool* pPool);
|
||||||
/// Adds existing session pool to the container.
|
/// Adds existing session pool to the container.
|
||||||
|
/// Throws SessionPoolExistsException if pool already exists.
|
||||||
|
|
||||||
Session add(const std::string& sessionKey,
|
Session add(const std::string& sessionKey,
|
||||||
const std::string& connectionString,
|
const std::string& connectionString,
|
||||||
@@ -69,7 +70,8 @@ public:
|
|||||||
int maxSessions = 32,
|
int maxSessions = 32,
|
||||||
int idleTime = 60);
|
int idleTime = 60);
|
||||||
/// Adds a new session pool to the container and returns a Session from
|
/// Adds a new session pool to the container and returns a Session from
|
||||||
/// newly created pool.
|
/// newly created pool. If pool already exists, request to add is silently
|
||||||
|
/// ignored and session is returned from the existing pool.
|
||||||
|
|
||||||
Session get(const std::string& name);
|
Session get(const std::string& name);
|
||||||
/// Returns a Session.
|
/// Returns a Session.
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ POCO_IMPLEMENT_EXCEPTION(LimitException, DataException, "Limit error")
|
|||||||
POCO_IMPLEMENT_EXCEPTION(NotSupportedException, DataException, "Feature or property not supported")
|
POCO_IMPLEMENT_EXCEPTION(NotSupportedException, DataException, "Feature or property not supported")
|
||||||
POCO_IMPLEMENT_EXCEPTION(SessionUnavailableException, DataException, "Session is unavailable")
|
POCO_IMPLEMENT_EXCEPTION(SessionUnavailableException, DataException, "Session is unavailable")
|
||||||
POCO_IMPLEMENT_EXCEPTION(SessionPoolExhaustedException, DataException, "No more sessions available from the session pool")
|
POCO_IMPLEMENT_EXCEPTION(SessionPoolExhaustedException, DataException, "No more sessions available from the session pool")
|
||||||
|
POCO_IMPLEMENT_EXCEPTION(SessionPoolExistsException, DataException, "Session already exists in the pool")
|
||||||
POCO_IMPLEMENT_EXCEPTION(NoDataException, DataException, "No data found")
|
POCO_IMPLEMENT_EXCEPTION(NoDataException, DataException, "No data found")
|
||||||
POCO_IMPLEMENT_EXCEPTION(LengthExceededException, DataException, "Data too long")
|
POCO_IMPLEMENT_EXCEPTION(LengthExceededException, DataException, "Data too long")
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ void SessionPoolContainer::add(SessionPool* pPool)
|
|||||||
poco_check_ptr (pPool);
|
poco_check_ptr (pPool);
|
||||||
|
|
||||||
if (_sessionPools.find(pPool->name()) != _sessionPools.end())
|
if (_sessionPools.find(pPool->name()) != _sessionPools.end())
|
||||||
throw InvalidAccessException("Session pool already exists: " + pPool->name());
|
throw SessionPoolExistsException("Session pool already exists: " + pPool->name());
|
||||||
|
|
||||||
pPool->duplicate();
|
pPool->duplicate();
|
||||||
_sessionPools.insert(SessionPoolMap::ValueType(pPool->name(), pPool));
|
_sessionPools.insert(SessionPoolMap::ValueType(pPool->name(), pPool));
|
||||||
@@ -73,14 +73,15 @@ Session SessionPoolContainer::add(const std::string& sessionKey,
|
|||||||
int maxSessions,
|
int maxSessions,
|
||||||
int idleTime)
|
int idleTime)
|
||||||
{
|
{
|
||||||
|
std::string name = SessionPool::name(sessionKey, connectionString);
|
||||||
|
SessionPoolMap::Iterator it = _sessionPools.find(name);
|
||||||
|
|
||||||
|
// pool already exists, silently return a session from it
|
||||||
|
if (it != _sessionPools.end()) return it->second->get();
|
||||||
|
|
||||||
SessionPool* pSP =
|
SessionPool* pSP =
|
||||||
new SessionPool(sessionKey, connectionString, minSessions, maxSessions, idleTime);
|
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 =
|
std::pair<SessionPoolMap::Iterator, bool> ins =
|
||||||
_sessionPools.insert(SessionPoolMap::ValueType(name, pSP));
|
_sessionPools.insert(SessionPoolMap::ValueType(name, pSP));
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ using Poco::Data::Session;
|
|||||||
using Poco::Data::SessionPool;
|
using Poco::Data::SessionPool;
|
||||||
using Poco::Data::SessionPoolContainer;
|
using Poco::Data::SessionPoolContainer;
|
||||||
using Poco::Data::SessionPoolExhaustedException;
|
using Poco::Data::SessionPoolExhaustedException;
|
||||||
|
using Poco::Data::SessionPoolExistsException;
|
||||||
using Poco::Data::SessionUnavailableException;
|
using Poco::Data::SessionUnavailableException;
|
||||||
|
|
||||||
|
|
||||||
@@ -228,13 +229,14 @@ void SessionPoolTest::testSessionPoolContainer()
|
|||||||
spc.add(pPool);
|
spc.add(pPool);
|
||||||
assert (1 == spc.count());
|
assert (1 == spc.count());
|
||||||
try { spc.add(pPool); fail ("must fail"); }
|
try { spc.add(pPool); fail ("must fail"); }
|
||||||
catch (InvalidAccessException&) { }
|
catch (SessionPoolExistsException&) { }
|
||||||
spc.remove(pPool->name());
|
spc.remove(pPool->name());
|
||||||
assert (0 == spc.count());
|
assert (0 == spc.count());
|
||||||
try { spc.get("test"); fail ("must fail"); }
|
try { spc.get("test"); fail ("must fail"); }
|
||||||
catch (NotFoundException&) { }
|
catch (NotFoundException&) { }
|
||||||
|
|
||||||
spc.add("test", "cs");
|
spc.add("test", "cs");
|
||||||
|
spc.add("test", "cs");//duplicate request, must be silently ignored
|
||||||
assert (1 == spc.count());
|
assert (1 == spc.count());
|
||||||
spc.remove("test://cs");
|
spc.remove("test://cs");
|
||||||
assert (0 == spc.count());
|
assert (0 == spc.count());
|
||||||
|
|||||||
Reference in New Issue
Block a user