TeConnectionPool.h

Go to the documentation of this file.
00001 /************************************************************************************
00002 TerraLib - a library for developing GIS applications.
00003 Copyright 2001-2007 INPE and Tecgraf/PUC-Rio.
00004 
00005 This code is part of the TerraLib library.
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 You should have received a copy of the GNU Lesser General Public
00012 License along with this library.
00013 
00014 The authors reassure the license terms regarding the warranties.
00015 They specifically disclaim any warranties, including, but not limited to,
00016 the implied warranties of merchantability and fitness for a particular purpose.
00017 The library provided hereunder is on an "as is" basis, and the authors have no
00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
00020 indirect, special, incidental, or consequential damages arising out of the use
00021 of this library and its documentation.
00022 *************************************************************************************/
00023 
00024 /*! \file TeConnectionPool.h
00025     \brief This file contains a class that represents a pool of TerraLib Database connections.
00026     \author Douglas Uba <douglas@dpi.inpe.br>
00027     \author Mario Rocco Pettinati <mario@dpi.inpe.br>
00028 */
00029 
00030 #ifndef  __TERRALIB_INTERNAL_CONNECTION_POOL_H
00031 #define  __TERRALIB_INTERNAL_CONNECTION_POOL_H
00032 
00033 // TerraLib
00034 #include "TeConnection.h"
00035 #include "TeDefines.h"
00036 #include "TeMutex.h"
00037 #include "TeDatabaseFactoryParams.h"
00038 // STL
00039 #include <ctime>
00040 #include <set>
00041 #include <vector>
00042 
00043 // forward declarations
00044 class TeDatabase;
00045 
00046 class TL_DLL TeConnectionPool
00047 {
00048 
00049 public: 
00050         
00051     /** @name Constructor
00052      *  Initilizer methods.
00053      */
00054     //@{
00055 
00056     /*! \brief Constructor. 
00057         \param nConns The number of connections that will be controlled by the pool.
00058         \param timeOut Time limit to wait for a connection (in milliseconds).
00059     */
00060     TeConnectionPool(const unsigned int& maxConnections, const unsigned int& maxIdle=0, const clock_t& maxWait = 10000);
00061 
00062     /*! \brief Destructor. */
00063         ~TeConnectionPool();
00064 
00065     //@}
00066 
00067     /*! \brief Intializes the pool of connections given a set of parameters. 
00068         \param user The database user.
00069         \param password The user password.
00070         \param host The database host.
00071         \param dbName The database name.
00072         \param dbmsName The database manager system name (i.e. PostgreSQL, Access).
00073         \param portNumber The database port.
00074     */
00075     bool initialize(const std::string& user, const std::string& password, const std::string& host, 
00076                     const std::string& dbName, const std::string& dbmsName, const int& portNumber);
00077 
00078     /*! \brief Gets a free connection that can be used for a TeDatabase instance.
00079         \note This method is thread-safe.
00080         \note The caller of this method will NOT take the ownership of the returned pointer.
00081         \note Case not exists a free connection, the caller will wait on a queue for one, respecting the time out.
00082         \note Case time out exceeds a NULL pointer will be returned.
00083         \note The connection should be repassed again to the pool after used using the method releaseConnection.
00084         Example: 
00085         {
00086             TeConnection* c = pool->getConnection();
00087             db->setConnection(c); // TeDatabase instance
00088             // do something...
00089             pool->releaseConnection(c);
00090         }
00091     */
00092     TeConnection* getConnection();
00093 
00094     
00095     /*! \brief Releases the given connection.
00096         \note This method is thread-safe.
00097         \note The given connection will be deleted.
00098     */
00099     void releaseConnection(TeConnection* conn);
00100 
00101     /** @name Access method.
00102     * Method to access the pool attrbiutes.
00103     */
00104     //@{
00105 
00106     /*! \brief Sets the time limit to wait for a connection (in milliseconds). */
00107     void setTimeOut(const clock_t t);
00108 
00109     //@}
00110 
00111 private:
00112 
00113     /** @name Internal methods */
00114     //@{
00115 
00116     /*! \brief Gets a free connection that can be used for a TeDatabase instance. */
00117     TeConnection* getFreeConnection();
00118 
00119     /*! \brief Generates a number to control the get of connections. */
00120     unsigned int getTicket();
00121 
00122     /*! \brief Clear the connections and associates variables. */
00123     void clear();
00124     /*! \brief Create new connections on the database pool. */
00125     void createNewConnection();
00126         /*! \brief Delete connection on the pool with the requested id. */
00127     void deleteConnection(unsigned int& id);
00128 
00129     //@}
00130 
00131 private:
00132 
00133     TeDatabaseFactoryParams params_;             //!< Connection details to create new connections. (Set on initialization)
00134     unsigned int maxConnections_;            //!< The number of max connections controlled by this.
00135     unsigned int maxIdle_;                       //!< The number of max connections idle to be hold by this.
00136     clock_t maxWait_;                        //!< Time limit to wait for a connection (in milliseconds).
00137     unsigned int ticket_;                    //!< A number used to control the get of connections.
00138     unsigned int connectionsSeq_;                //!< Sequence to generate connection id
00139     std::map<unsigned int, TeDatabase*> databases_;     //!< A map of database connections.
00140     std::set<unsigned int> freeConns_;       //!< Informs which connections are free.
00141     std::set<unsigned int> freeTickets_;     //!< Informs which tickets numbers are free.
00142     std::vector<unsigned int> requestQueue_; //!< A wait queue for get connections.
00143 
00144     TeMutex mutexLock_;                      //!< For threads control.
00145 };
00146 
00147 #endif // __TERRALIB_INTERNAL_CONNECTION_POOL_H
00148         

Generated on Sun Jul 29 04:01:19 2012 for TerraLib - Development Source by  doxygen 1.5.3