All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ConnectionPoolManager.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/dataaccess/datasource/ConnectionPoolManager.cpp
22 
23  \brief A singleton for managing the connection pools available in the system.
24 */
25 
26 // TerraLib
27 #include "AbstractConnectionPool.h"
28 #include "ConnectionPoolManager.h"
29 
31 {
32  boost::lock_guard<boost::mutex> lock(m_mtx);
33 
34  if(pool)
35  m_pools.push_back(pool);
36 }
37 
39 {
40  boost::lock_guard<boost::mutex> lock(m_mtx);
41 
42  std::vector<AbstractConnectionPool*>::iterator it = std::find(m_pools.begin(), m_pools.end(), pool);
43 
44  if(it != m_pools.end())
45  m_pools.erase(it);
46 }
47 
49 {
50  boost::lock_guard<boost::mutex> lock(m_mtx);
51 
52 // is manager already initialized?
53  if(m_initialized)
54  return;
55 
56 // start all the pools
57  const std::size_t size = m_pools.size();
58 
59  for(std::size_t i = 0; i < size; ++i)
60  m_pools[i]->initialize();
61 
62  m_initialized = true;
63 
64 // create a monitoring thread
65  // calling a static method
66  //m_thread = boost::thread(te::da::ConnectionPoolManager::monitore);
67  m_thread = boost::thread(&te::da::ConnectionPoolManager::monitore, this);
68 }
69 
71 {
72 // ok, let's signal the monitoring thread that it must interrupt and then le't wait for it to finish its job
73  m_thread.interrupt();
74  m_thread.join();
75 
76 // let's hold the mutex!
77  boost::lock_guard<boost::mutex> lock(m_mtx);
78 
79 // finalizes all the pools
80  const std::size_t size = m_pools.size();
81 
82  for(std::size_t i = 0; i < size; ++i)
83  m_pools[i]->finalize();
84 
85  m_initialized = false;
86 }
87 
89 {
90  return m_initialized;
91 }
92 
94  : m_initialized(false)
95 {
96 }
97 
99 {
100  //m_thread.interrupt();
101  //m_thread.join();
102 }
103 
105 {
106  try
107  {
108  while(true)
109  {
110 // keep a nested scope to hold the mutex just for a while
111  {
112  boost::lock_guard<boost::mutex> lock(getInstance().m_mtx);
113 
114  std::vector<AbstractConnectionPool*>& pools = getInstance().m_pools;
115 
116  const std::size_t size = pools.size();
117 
118  for(std::size_t i = 0; i < size; ++i)
119  pools[i]->idle();
120  }
121 
122  boost::posix_time::seconds sleepTime(TERRALIB_POOL_DEFAULT_MONITORING_TIME);
123  boost::this_thread::sleep(sleepTime);
124  }
125  }
126  catch(boost::thread_interrupted&)
127  {
128  return; // let's finish!
129  }
130  catch(...)
131  {
132  return; // let's finish!
133  }
134 }
135 
void start()
It starts a new thread of execution that will monitore all the connection pools.
void monitore()
This static method encapsulates the execution thread that monitores all the pools.
void remove(AbstractConnectionPool *pool)
It removes (un-register) the pool from the monitoring list.
std::vector< AbstractConnectionPool * > m_pools
The list of monitored pools.
void add(AbstractConnectionPool *pool)
It adds the given pool to the list of monitored pools.
void stop()
It stops the connection pools monitoring thread.
ConnectionPoolManager()
Singleton constructor is protected.
~ConnectionPoolManager()
Singleton destructor is protected.
This class defines the basic interface for a connection pool.
#define TERRALIB_POOL_DEFAULT_MONITORING_TIME
This sets the default monitoring time in seconds for the connection pool manager. ...
Definition: Config.h:67
boost::mutex m_mtx
A mutex to lock the manager access.
This class defines the basic interface for a connection pool.
A singleton for managing the connection pools available in the system.
bool isInitialized() const
It returns true if the manager was already started.