added SessionPoolExistsException

when duplicate session add is requested by name, it is silently ignored
This commit is contained in:
Aleksandar Fabijanic 2008-10-21 13:39:47 +00:00
parent 4e0cc97d6a
commit dcede96ede
5 changed files with 15 additions and 8 deletions

View File

@ -59,6 +59,7 @@ POCO_DECLARE_EXCEPTION(Data_API, LimitException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, NotSupportedException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, SessionUnavailableException, 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, LengthExceededException, DataException)

View File

@ -62,6 +62,7 @@ public:
void add(SessionPool* pPool);
/// Adds existing session pool to the container.
/// Throws SessionPoolExistsException if pool already exists.
Session add(const std::string& sessionKey,
const std::string& connectionString,
@ -69,7 +70,8 @@ public:
int maxSessions = 32,
int idleTime = 60);
/// 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);
/// Returns a Session.

View File

@ -53,6 +53,7 @@ POCO_IMPLEMENT_EXCEPTION(LimitException, DataException, "Limit error")
POCO_IMPLEMENT_EXCEPTION(NotSupportedException, DataException, "Feature or property not supported")
POCO_IMPLEMENT_EXCEPTION(SessionUnavailableException, DataException, "Session is unavailable")
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(LengthExceededException, DataException, "Data too long")

View File

@ -60,7 +60,7 @@ void SessionPoolContainer::add(SessionPool* pPool)
poco_check_ptr (pPool);
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();
_sessionPools.insert(SessionPoolMap::ValueType(pPool->name(), pPool));
@ -73,14 +73,15 @@ Session SessionPoolContainer::add(const std::string& sessionKey,
int maxSessions,
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 =
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));

View File

@ -50,6 +50,7 @@ using Poco::Data::Session;
using Poco::Data::SessionPool;
using Poco::Data::SessionPoolContainer;
using Poco::Data::SessionPoolExhaustedException;
using Poco::Data::SessionPoolExistsException;
using Poco::Data::SessionUnavailableException;
@ -228,13 +229,14 @@ void SessionPoolTest::testSessionPoolContainer()
spc.add(pPool);
assert (1 == spc.count());
try { spc.add(pPool); fail ("must fail"); }
catch (InvalidAccessException&) { }
catch (SessionPoolExistsException&) { }
spc.remove(pPool->name());
assert (0 == spc.count());
try { spc.get("test"); fail ("must fail"); }
catch (NotFoundException&) { }
spc.add("test", "cs");
spc.add("test", "cs");//duplicate request, must be silently ignored
assert (1 == spc.count());
spc.remove("test://cs");
assert (0 == spc.count());