27 #include "../common/Enums.h"
28 #include "../common/STLUtils.h"
29 #include "../common/Translator.h"
30 #include "../dataaccess/datasource/ConnectionPoolManager.h"
41 #include <boost/thread/locks.hpp>
42 #include <boost/thread/mutex.hpp>
86 throw Exception(
TE_TR(
"The connection pool is already initialized!"));
91 std::map<std::string, std::string>::const_iterator itend = connInfo.end();
93 std::map<std::string, std::string>::const_iterator it = connInfo.find(
"PG_INITIAL_POOL_SIZE");
96 it = connInfo.find(
"PG_MIN_POOL_SIZE");
99 it = connInfo.find(
"PG_MAX_POOL_SIZE");
111 it = connInfo.find(
"PG_MAX_IDLE_TIME");
114 it = connInfo.find(
"PG_CLIENT_ENCODING");
131 std::string off =
"off";
133 std::string answer = PQparameterStatus(
m_pImpl->
m_connections.front()->m_pgconn,
"integer_datetimes");
147 boost::lock_guard<boost::mutex> lock(m_pImpl->m_mtx);
150 std::list<te::pgis::Connection*>::iterator it = m_pImpl->m_connections.begin();
151 std::list<te::pgis::Connection*>::iterator itend = m_pImpl->m_connections.end();
156 throw Exception(
TE_TR(
"There are opened connections. Please, close all connections before finalizing the connection pool."));
164 m_pImpl->m_connections.clear();
165 m_pImpl->m_poolSize = 0;
167 m_pImpl->m_initialized =
false;
173 boost::lock_guard<boost::mutex> lock(m_pImpl->m_mtx);
180 if((m_pImpl->m_maxIdleTime == 0) ||
182 (m_pImpl->m_maxPoolSize == m_pImpl->m_minPoolSize) ||
183 (m_pImpl->m_poolSize == m_pImpl->m_minPoolSize))
187 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
190 std::list<te::pgis::Connection*>::iterator it = m_pImpl->m_connections.begin();
191 std::list<te::pgis::Connection*>::iterator itend = m_pImpl->m_connections.end();
195 if((*it)->m_inuse ==
false)
197 boost::posix_time::time_duration d = now - (*it)->m_lastuse;
199 if(d.seconds() >
static_cast<long>(m_pImpl->m_maxIdleTime))
201 std::list<te::pgis::Connection*>::iterator itaux = it;
204 m_pImpl->m_connections.erase(itaux);
205 --(m_pImpl->m_poolSize);
207 if(m_pImpl->m_poolSize == m_pImpl->m_minPoolSize)
221 boost::lock_guard<boost::mutex> lock(m_pImpl->m_mtx);
223 std::list<te::pgis::Connection*>::iterator it = m_pImpl->m_connections.begin();
224 std::list<te::pgis::Connection*>::iterator itend = m_pImpl->m_connections.end();
228 ConnStatusType status = PQstatus((*it)->m_pgconn);
230 if(status != CONNECTION_OK)
241 return m_pImpl->m_initialized;
246 return m_pImpl->m_ds;
251 return m_pImpl->m_poolSize;
256 return m_pImpl->m_initialPoolSize;
262 boost::lock_guard<boost::mutex> lock(m_pImpl->m_mtx);
264 m_pImpl->m_initialPoolSize = size;
269 return m_pImpl->m_minPoolSize;
275 boost::lock_guard<boost::mutex> lock(m_pImpl->m_mtx);
277 m_pImpl->m_minPoolSize = size;
282 return m_pImpl->m_maxPoolSize;
288 boost::lock_guard<boost::mutex> lock(m_pImpl->m_mtx);
290 m_pImpl->m_maxPoolSize = size;
296 boost::lock_guard<boost::mutex> lock(m_pImpl->m_mtx);
299 std::list<te::pgis::Connection*>::iterator it = m_pImpl->m_connections.begin();
300 std::list<te::pgis::Connection*>::iterator itend = m_pImpl->m_connections.end();
304 if((*it)->m_inuse ==
false)
306 (*it)->m_inuse =
true;
314 if(m_pImpl->m_poolSize < m_pImpl->m_maxPoolSize)
316 Connection* newConn =
new Connection(
this, m_pImpl->m_conninfo.c_str(), m_pImpl->m_cencoding.c_str(),
true);
317 m_pImpl->m_connections.push_back(newConn);
318 ++(m_pImpl->m_poolSize);
329 boost::lock_guard<boost::mutex> lock(m_pImpl->m_mtx);
332 conn->
m_lastuse = boost::posix_time::second_clock::local_time();
337 return m_pImpl->m_maxIdleTime;
343 boost::lock_guard<boost::mutex> lock(m_pImpl->m_mtx);
345 m_pImpl->m_maxIdleTime = t;
unsigned int m_maxIdleTime
The number of seconds waiting for releasing connections.
unsigned int getMaxIdleTime() const
It returns the maximum idle time in seconds that a connection can be maintained in the pool without b...
void idle()
It releases the connections that are not in use for a long time.
bool isValid() const
It checks if all the connections in the pool are valid (the communication channel is ok)...
std::string MakeConnectionStr(const std::map< std::string, std::string > &dsInfo)
void setInitialPoolSize(std::size_t size)
It sets the initial number of connections opened by the pool at its startup.
std::string m_cencoding
Client encoding.
std::list< te::pgis::Connection * > m_connections
The list of available connections ready to be used.
bool isInitialized() const
It returns true if the connection pool is initialized, otherwise it returns false.
#define PGIS_DEFAULT_INITIAL_POOL_SIZE
This sets the default initial number of connections opened by a connection pool.
void setMaxIdleTime(unsigned int t)
It sets the maximum idle time that a connection can be maintained in the pool without being used...
std::size_t getPoolSize() const
It returns the number of connections in the pool.
void setMaxPoolSize(std::size_t size)
It sets the maximum number of connections managed by the pool.
boost::mutex m_mtx
A mutex for preventing reading/writing in the connection list.
A class that implements a connection to a PostgreSQL database.
const char * GetPGEncoding(te::common::CharEncoding encoding)
#define TE_TR(message)
It marks a string in order to get translated.
std::size_t getMinPoolSize() const
It returns the minimum number of connections managed by the pool.
void setTimeAsInteger(bool timeIsInteger)
void release(Connection *conn)
It brings the informed connection back to the pool.
#define PGIS_DEFAULT_MIN_POOL_SIZE
This sets the default minimum number of connections to be kept in the pool.
const std::map< std::string, std::string > & getConnectionInfo() const
It returns the set of parameters used to set up the access channel to the underlying repository...
static ConnectionPoolManager & getInstance()
It returns a reference to the singleton instance.
Utility functions for PostgreSQL.
boost::posix_time::ptime m_lastuse
It marks the last time this connection was used.
#define PGIS_DEFAULT_MAX_IDLE_TIME
This sets the default maximum time that a connection can be in the pool without being used...
static CharEncoding getCharEncodingType(const std::string &name)
It returns the charset type of the given charset name.
#define PGIS_DEFAULT_MAX_POOL_SIZE
This sets the default maximum number of connections in the pool.
ConnectionPoolImpl(te::pgis::DataSource *ds)
void initialize()
It initializes the connections to be managed by the pool.
std::size_t getInitialPoolSize() const
It returns the initial number of connections opened by the pool at its startup.
void setMinPoolSize(std::size_t size)
It sets the minimum number of connections managed by the pool.
ConnectionPoolImpl * m_pImpl
A pointer to the pool implementation.
A class that implements a connection to a PostgreSQL database.
std::size_t m_maxPoolSize
It indicates the maximum number of connections in the pool.
Implementation of the data source for the PostGIS driver.
~ConnectionPool()
Destructor.
An exception class for the PostGIS driver.
te::pgis::DataSource * m_ds
The data source using the pool.
std::size_t m_initialPoolSize
It indicates the initial number of connections opened by the pool.
std::size_t m_minPoolSize
It indicates the minimum number of connections in the pool.
Connection * getConnection()
It returns a connection from the pool.
std::string m_conninfo
The connection info.
te::pgis::DataSource * getDataSource() const
A class that implements a connection pool for PostGIS.
std::size_t getMaxPoolSize() const
It returns the maximum number of connections managed by the pool.
ConnectionPool(DataSource *ds)
It creates a new connection pool for the database informed.
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
void finalize()
It closes all connections and clears all resources managed by the pool.
bool m_inuse
Tells if the connection is in use or not.
std::size_t m_poolSize
It indicates the maximum number of connections in the pool.
bool m_initialized
A flag that indicates if the pool was initialized or not.