1//
2// SessionPoolContainer.cpp
3//
4// Library: Data
5// Package: SessionPooling
6// Module: SessionPoolContainer
7//
8// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/Data/SessionPoolContainer.h"
16#include "Poco/Data/SessionFactory.h"
17#include "Poco/Data/DataException.h"
18#include "Poco/URI.h"
19#include "Poco/String.h"
20#include "Poco/Exception.h"
21#include <algorithm>
22
23
24using Poco::FastMutex;
25
26
27namespace Poco {
28namespace Data {
29
30
31SessionPoolContainer::SessionPoolContainer()
32{
33}
34
35
36SessionPoolContainer::~SessionPoolContainer()
37{
38}
39
40
41void SessionPoolContainer::add(SessionPool* pPool)
42{
43 poco_check_ptr (pPool);
44
45 FastMutex::ScopedLock lock(_mutex);
46 if (_sessionPools.find(pPool->name()) != _sessionPools.end())
47 throw SessionPoolExistsException("Session pool already exists: " + pPool->name());
48
49 pPool->duplicate();
50 _sessionPools.insert(SessionPoolMap::value_type(pPool->name(), pPool));
51}
52
53
54Session SessionPoolContainer::add(const std::string& sessionKey,
55 const std::string& connectionString,
56 int minSessions,
57 int maxSessions,
58 int idleTime)
59{
60 std::string name = SessionPool::name(sessionKey, connectionString);
61
62 FastMutex::ScopedLock lock(_mutex);
63 SessionPoolMap::iterator it = _sessionPools.find(name);
64
65 // pool already exists, silently return a session from it
66 if (it != _sessionPools.end()) return it->second->get();
67
68 SessionPool* pSP =
69 new SessionPool(sessionKey, connectionString, minSessions, maxSessions, idleTime);
70
71 std::pair<SessionPoolMap::iterator, bool> ins =
72 _sessionPools.insert(SessionPoolMap::value_type(name, pSP));
73
74 return ins.first->second->get();
75}
76
77
78bool SessionPoolContainer::isActive(const std::string& sessionKey,
79 const std::string& connectionString) const
80{
81 std::string name = connectionString.empty() ?
82 sessionKey : SessionPool::name(sessionKey, connectionString);
83
84 SessionPoolMap::const_iterator it = _sessionPools.find(name);
85 if (it != _sessionPools.end() && it->second->isActive())
86 {
87 return true;
88 }
89
90 return false;
91}
92
93
94Session SessionPoolContainer::get(const std::string& name)
95{
96 return getPool(name).get();
97}
98
99
100SessionPool& SessionPoolContainer::getPool(const std::string& name)
101{
102 URI uri(name);
103 std::string path = uri.getPath();
104 poco_assert (!path.empty());
105 std::string n = Session::uri(uri.getScheme(), path.substr(1));
106
107 FastMutex::ScopedLock lock(_mutex);
108 SessionPoolMap::iterator it = _sessionPools.find(n);
109 if (_sessionPools.end() == it) throw NotFoundException(n);
110 return *it->second;
111}
112
113
114void SessionPoolContainer::shutdown()
115{
116 SessionPoolMap::iterator it = _sessionPools.begin();
117 SessionPoolMap::iterator end = _sessionPools.end();
118 for (; it != end; ++it) it->second->shutdown();
119}
120
121
122} } // namespace Poco::Data
123