| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <[email protected]> |
| 4 | // |
| 5 | // Distributed under: |
| 6 | // |
| 7 | // the Boost Software License, Version 1.0. |
| 8 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 9 | // http://www.boost.org/LICENSE_1_0.txt) |
| 10 | // |
| 11 | // or (at your opinion) under: |
| 12 | // |
| 13 | // The MIT License |
| 14 | // (See accompanying file MIT.txt or a copy at |
| 15 | // http://www.opensource.org/licenses/mit-license.php) |
| 16 | // |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | #define CPPDB_SOURCE |
| 19 | #include <cppdb/conn_manager.h> |
| 20 | #include <cppdb/backend.h> |
| 21 | #include <cppdb/pool.h> |
| 22 | #include <cppdb/driver_manager.h> |
| 23 | |
| 24 | namespace cppdb { |
| 25 | struct connections_manager::data{}; |
| 26 | connections_manager::connections_manager() {} |
| 27 | // Borland erros on hidden destructors in classes without only static methods. |
| 28 | #ifndef __BORLANDC__ |
| 29 | connections_manager::~connections_manager() {} |
| 30 | #endif |
| 31 | |
| 32 | connections_manager &connections_manager::instance() |
| 33 | { |
| 34 | static connections_manager mgr; |
| 35 | return mgr; |
| 36 | } |
| 37 | |
| 38 | namespace { |
| 39 | struct init { |
| 40 | init() |
| 41 | { connections_manager::instance(); } |
| 42 | } initializer; |
| 43 | } |
| 44 | |
| 45 | ref_ptr<backend::connection> connections_manager::open(std::string const &cs) |
| 46 | { |
| 47 | ref_ptr<pool> p; |
| 48 | /// seems we may be using pool |
| 49 | if(cs.find("@pool_size")!=std::string::npos) { |
| 50 | mutex::guard l(lock_); |
| 51 | connections_type::iterator pool_ptr = connections_.find(cs); |
| 52 | if(pool_ptr!=connections_.end()) |
| 53 | p = pool_ptr->second; |
| 54 | } |
| 55 | |
| 56 | if(p) { |
| 57 | return p->open(); |
| 58 | } |
| 59 | else { |
| 60 | connection_info ci(cs); |
| 61 | return open(ci); |
| 62 | } |
| 63 | } |
| 64 | ref_ptr<backend::connection> connections_manager::open(connection_info const &ci) |
| 65 | { |
| 66 | if(ci.get("@pool_size",0)==0) { |
| 67 | return driver_manager::instance().connect(ci); |
| 68 | } |
| 69 | ref_ptr<pool> p; |
| 70 | { |
| 71 | mutex::guard l(lock_); |
| 72 | ref_ptr<pool> &ref_p = connections_[ci.connection_string]; |
| 73 | if(!ref_p) { |
| 74 | ref_p = pool::create(ci); |
| 75 | } |
| 76 | p=ref_p; |
| 77 | } |
| 78 | return p->open(); |
| 79 | } |
| 80 | void connections_manager::gc() |
| 81 | { |
| 82 | std::vector<ref_ptr<pool> > pools_; |
| 83 | pools_.reserve(100); |
| 84 | { |
| 85 | mutex::guard l(lock_); |
| 86 | for(connections_type::iterator p=connections_.begin();p!=connections_.end();++p) { |
| 87 | pools_.push_back(p->second); |
| 88 | } |
| 89 | } |
| 90 | for(unsigned i=0;i<pools_.size();i++) { |
| 91 | pools_[i]->gc(); |
| 92 | } |
| 93 | pools_.clear(); |
| 94 | { |
| 95 | mutex::guard l(lock_); |
| 96 | for(connections_type::iterator p=connections_.begin();p!=connections_.end();) { |
| 97 | if(p->second->use_count() == 1) { |
| 98 | pools_.push_back(p->second); |
| 99 | connections_type::iterator tmp = p; |
| 100 | ++p; |
| 101 | connections_.erase(tmp); |
| 102 | } |
| 103 | else |
| 104 | ++p; |
| 105 | } |
| 106 | } |
| 107 | pools_.clear(); |
| 108 | |
| 109 | } |
| 110 | |
| 111 | |
| 112 | } // cppdb |