1//
2// SessionPoolContainer.cpp
3//
4// Library: SQL
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/SQL/SessionPoolContainer.h"
16#include "Poco/SQL/SessionFactory.h"
17#include "Poco/SQL/SQLException.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 SQL {
29
30
31SessionPoolContainer::SessionPoolContainer()
32{
33}
34
35
36SessionPoolContainer::~SessionPoolContainer()
37{
38}
39
40
41void SessionPoolContainer::add(SessionPool::Ptr pPool)
42{
43 poco_check_ptr (pPool.get());
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 _sessionPools.insert(SessionPoolMap::value_type(pPool->name(), pPool));
50}
51
52
53Session SessionPoolContainer::add(const std::string& sessionKey,
54 const std::string& connectionString,
55 int minSessions,
56 int maxSessions,
57 int idleTime)
58{
59 std::string name = SessionPool::name(sessionKey, connectionString);
60
61 FastMutex::ScopedLock lock(_mutex);
62 SessionPoolMap::iterator it = _sessionPools.find(name);
63
64 // pool already exists, silently return a session from it
65 if (it != _sessionPools.end()) return it->second->get();
66
67 SessionPool::Ptr pSP =
68 new SessionPool(sessionKey, connectionString, minSessions, maxSessions, idleTime);
69
70 std::pair<SessionPoolMap::iterator, bool> ins =
71 _sessionPools.insert(SessionPoolMap::value_type(name, pSP));
72
73 return ins.first->second->get();
74}
75
76
77bool SessionPoolContainer::isActive(const std::string& sessionKey,
78 const std::string& connectionString) const
79{
80 std::string name = connectionString.empty() ?
81 sessionKey : SessionPool::name(sessionKey, connectionString);
82
83 SessionPoolMap::const_iterator it = _sessionPools.find(name);
84 if (it != _sessionPools.end() && it->second->isActive())
85 {
86 return true;
87 }
88
89 return false;
90}
91
92
93Session SessionPoolContainer::get(const std::string& name)
94{
95 return getPool(name).get();
96}
97
98
99SessionPool& SessionPoolContainer::getPool(const std::string& name)
100{
101 URI uri(name);
102 std::string path = uri.getPath();
103 poco_assert (!path.empty());
104 std::string n = Session::uri(uri.getScheme(), path.substr(1));
105
106 FastMutex::ScopedLock lock(_mutex);
107 SessionPoolMap::iterator it = _sessionPools.find(n);
108 if (_sessionPools.end() == it) throw NotFoundException(n);
109 return *it->second;
110}
111
112
113void SessionPoolContainer::shutdown()
114{
115 SessionPoolMap::iterator it = _sessionPools.begin();
116 SessionPoolMap::iterator end = _sessionPools.end();
117 for (; it != end; ++it) it->second->shutdown();
118}
119
120
121} } // namespace Poco::SQL
122