27 #include "../common/Enums.h" 28 #include "../common/STLUtils.h" 29 #include "../core/translator/Translator.h" 30 #include "../core/uri/URI.h" 31 #include "../core/uri/Utils.h" 46 #include <boost/thread.hpp> 91 throw Exception(
TE_TR(
"The connection pool is already initialized!"));
96 std::map<std::string, std::string>::const_iterator it = kvp.begin();
97 std::map<std::string, std::string>::const_iterator itend = kvp.end();
99 it = kvp.find(
"PG_INITIAL_POOL_SIZE");
102 it = kvp.find(
"PG_MIN_POOL_SIZE");
105 it = kvp.find(
"PG_MAX_POOL_SIZE");
117 it = kvp.find(
"PG_MAX_IDLE_TIME");
137 std::string off =
"off";
139 std::string answer = PQparameterStatus(
m_pImpl->
m_connections.front()->m_pgconn,
"integer_datetimes");
162 throw Exception(
TE_TR(
"There are opened connections. Please, close all connections before finalizing the connection pool."));
194 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
202 if((*it)->m_inuse ==
false)
204 boost::posix_time::time_duration
d = now - (*it)->m_lastuse;
208 std::list<te::pgis::Connection*>::iterator itaux = it;
235 ConnStatusType status = PQstatus((*it)->m_pgconn);
237 if(status != CONNECTION_OK)
303 std::chrono::time_point<std::chrono::system_clock> start, end;
304 start = std::chrono::system_clock::now();
306 bool gettingConnection =
true;
307 while (gettingConnection)
320 if (conn !=
nullptr && !conn->
m_inuse)
332 end = std::chrono::system_clock::now();
333 std::chrono::duration<double> elapsed_seconds = end - start;
336 gettingConnection =
false;
339 boost::int_least64_t sleepTime = boost::int_least64_t(100) * boost::int_least64_t(1000000);
340 boost::this_thread::sleep_for(boost::chrono::nanoseconds(sleepTime));
367 conn->
m_lastuse = boost::posix_time::second_clock::local_time();
390 if ((*it)->m_id ==
id)
392 (*it)->m_inuse = inUse;
424 if ((*it)->m_id ==
id)
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.
std::vector< int > m_availableConnectionsIds
A vector with the ids of available connections.
std::string MakeConnectionStr(const te::core::URI &connInfo)
bool isValid() const
It checks if all the connections in the pool are valid (the communication channel is ok)...
void setInitialPoolSize(std::size_t size)
It sets the initial number of connections opened by the pool at its startup.
A class that control the use of connection to a PostgreSQL database.
std::string m_cencoding
Client encoding.
std::list< te::pgis::Connection * > m_connections
The list of available connections ready to be used.
#define PGIS_DEFAULT_INITIAL_POOL_SIZE
This sets the default initial number of connections opened by a connection pool.
bool isInitialized() const
It returns true if the connection pool is initialized, otherwise it returns false.
Base exception class for plugin module.
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.
static te::dt::Date ds(2010, 01, 01)
#define TE_TR(message)
It marks a string in order to get translated.
std::string query() const
Retrieving the query.
std::size_t getMinPoolSize() const
It returns the minimum number of connections managed by the pool.
virtual te::core::EncodingType getEncoding()
It return the DataSource current encoding.
void setTimeAsInteger(bool timeIsInteger)
void release(Connection *conn)
It brings the informed connection back to the pool.
#define PGIS_DEFAULT_MAX_POOL_SIZE
This sets the default maximum number of connections in the pool.
An exception class for the PostGIS driver.
static std::string getEncodingName(EncodingType et)
Retrive a string from a given character encoding type enum.
#define PGIS_DEFAULT_MAX_IDLE_TIME
This sets the default maximum time that a connection can be in the pool without being used...
boost::posix_time::ptime m_lastuse
It marks the last time this connection was used.
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
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.
int getAvailableConnectionId()
Gets the first available connection.
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.
#define PGIS_DEFAULT_MIN_POOL_SIZE
This sets the default minimum number of connections to be kept in the pool.
std::size_t m_maxPoolSize
It indicates the maximum number of connections in the pool.
~ConnectionPool()
Destructor.
A class for representing an Uniform Resource Identifier (URI).
Connection * getConnection(int id=-1)
It returns a connection from the pool.
This file contains utility functions used to manipulate data from a URI.
int m_id
The connection Id.
const te::core::URI & getConnectionInfo() const
An Uniform Resource Identifier used to describe the datasource connection.
TECOREEXPORT std::map< std::string, std::string > Expand(const std::string &query_str)
Split a query string into its components.
te::pgis::DataSource * m_ds
The data source using the pool.
te::pgis::Connection * getConnectionById(const int &id)
Gets a connection by id.
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.
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 setConnectionInUse(const int &id, const bool &inUse)
Sets the state of the connection by id.
Implementation of the data source for the PostGIS driver.
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.