Skip to content

Commit dcede96

Browse files
committed
added SessionPoolExistsException
when duplicate session add is requested by name, it is silently ignored
1 parent 4e0cc97 commit dcede96

5 files changed

Lines changed: 15 additions & 8 deletions

File tree

Data/include/Poco/Data/DataException.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ POCO_DECLARE_EXCEPTION(Data_API, LimitException, DataException)
5959
POCO_DECLARE_EXCEPTION(Data_API, NotSupportedException, DataException)
6060
POCO_DECLARE_EXCEPTION(Data_API, SessionUnavailableException, DataException)
6161
POCO_DECLARE_EXCEPTION(Data_API, SessionPoolExhaustedException, DataException)
62+
POCO_DECLARE_EXCEPTION(Data_API, SessionPoolExistsException, DataException)
6263
POCO_DECLARE_EXCEPTION(Data_API, NoDataException, DataException)
6364
POCO_DECLARE_EXCEPTION(Data_API, LengthExceededException, DataException)
6465

Data/include/Poco/Data/SessionPoolContainer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ class Data_API SessionPoolContainer
6262

6363
void add(SessionPool* pPool);
6464
/// Adds existing session pool to the container.
65+
/// Throws SessionPoolExistsException if pool already exists.
6566

6667
Session add(const std::string& sessionKey,
6768
const std::string& connectionString,
6869
int minSessions = 1,
6970
int maxSessions = 32,
7071
int idleTime = 60);
7172
/// Adds a new session pool to the container and returns a Session from
72-
/// newly created pool.
73+
/// newly created pool. If pool already exists, request to add is silently
74+
/// ignored and session is returned from the existing pool.
7375

7476
Session get(const std::string& name);
7577
/// Returns a Session.

Data/src/DataException.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ POCO_IMPLEMENT_EXCEPTION(LimitException, DataException, "Limit error")
5353
POCO_IMPLEMENT_EXCEPTION(NotSupportedException, DataException, "Feature or property not supported")
5454
POCO_IMPLEMENT_EXCEPTION(SessionUnavailableException, DataException, "Session is unavailable")
5555
POCO_IMPLEMENT_EXCEPTION(SessionPoolExhaustedException, DataException, "No more sessions available from the session pool")
56+
POCO_IMPLEMENT_EXCEPTION(SessionPoolExistsException, DataException, "Session already exists in the pool")
5657
POCO_IMPLEMENT_EXCEPTION(NoDataException, DataException, "No data found")
5758
POCO_IMPLEMENT_EXCEPTION(LengthExceededException, DataException, "Data too long")
5859

Data/src/SessionPoolContainer.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void SessionPoolContainer::add(SessionPool* pPool)
6060
poco_check_ptr (pPool);
6161

6262
if (_sessionPools.find(pPool->name()) != _sessionPools.end())
63-
throw InvalidAccessException("Session pool already exists: " + pPool->name());
63+
throw SessionPoolExistsException("Session pool already exists: " + pPool->name());
6464

6565
pPool->duplicate();
6666
_sessionPools.insert(SessionPoolMap::ValueType(pPool->name(), pPool));
@@ -73,13 +73,14 @@ Session SessionPoolContainer::add(const std::string& sessionKey,
7373
int maxSessions,
7474
int idleTime)
7575
{
76-
SessionPool* pSP =
77-
new SessionPool(sessionKey, connectionString, minSessions, maxSessions, idleTime);
76+
std::string name = SessionPool::name(sessionKey, connectionString);
77+
SessionPoolMap::Iterator it = _sessionPools.find(name);
7878

79-
std::string name = pSP->name();
79+
// pool already exists, silently return a session from it
80+
if (it != _sessionPools.end()) return it->second->get();
8081

81-
if (_sessionPools.find(name) != _sessionPools.end())
82-
throw InvalidAccessException("Session pool already exists: " + name);
82+
SessionPool* pSP =
83+
new SessionPool(sessionKey, connectionString, minSessions, maxSessions, idleTime);
8384

8485
std::pair<SessionPoolMap::Iterator, bool> ins =
8586
_sessionPools.insert(SessionPoolMap::ValueType(name, pSP));

Data/testsuite/src/SessionPoolTest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ using Poco::Data::Session;
5050
using Poco::Data::SessionPool;
5151
using Poco::Data::SessionPoolContainer;
5252
using Poco::Data::SessionPoolExhaustedException;
53+
using Poco::Data::SessionPoolExistsException;
5354
using Poco::Data::SessionUnavailableException;
5455

5556

@@ -228,13 +229,14 @@ void SessionPoolTest::testSessionPoolContainer()
228229
spc.add(pPool);
229230
assert (1 == spc.count());
230231
try { spc.add(pPool); fail ("must fail"); }
231-
catch (InvalidAccessException&) { }
232+
catch (SessionPoolExistsException&) { }
232233
spc.remove(pPool->name());
233234
assert (0 == spc.count());
234235
try { spc.get("test"); fail ("must fail"); }
235236
catch (NotFoundException&) { }
236237

237238
spc.add("test", "cs");
239+
spc.add("test", "cs");//duplicate request, must be silently ignored
238240
assert (1 == spc.count());
239241
spc.remove("test://cs");
240242
assert (0 == spc.count());

0 commit comments

Comments
 (0)