CppDB
|
00001 00002 // 00003 // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com> 00004 // 00005 // Distributed under: 00006 // 00007 // the Boost Software License, Version 1.0. 00008 // (See accompanying file LICENSE_1_0.txt or copy at 00009 // http://www.boost.org/LICENSE_1_0.txt) 00010 // 00011 // or (at your opinion) under: 00012 // 00013 // The MIT License 00014 // (See accompanying file MIT.txt or a copy at 00015 // http://www.opensource.org/licenses/mit-license.php) 00016 // 00018 #ifndef CPPDB_POOL_H 00019 #define CPPDB_POOL_H 00020 00021 #include <cppdb/defs.h> 00022 #include <cppdb/ref_ptr.h> 00023 #include <cppdb/mutex.h> 00024 #include <cppdb/utils.h> 00025 #include <memory> 00026 #include <list> 00027 00028 00029 namespace cppdb { 00030 class connection_info; 00031 namespace backend { 00032 class connection; 00033 } 00034 00045 class CPPDB_API pool : public ref_counted { 00046 pool(); 00047 pool(pool const &); 00048 void operator=(pool const &); 00049 pool(connection_info const &ci); 00050 public: 00052 static ref_ptr<pool> create(std::string const &connection_string); 00054 static ref_ptr<pool> create(connection_info const &ci); 00055 00061 typedef ref_ptr<pool> pointer; 00062 00063 ~pool(); 00064 00068 ref_ptr<backend::connection> open(); 00072 void gc(); 00073 00077 void clear(); 00078 00080 void put(backend::connection *c_in); 00082 private: 00083 ref_ptr<backend::connection> get(); 00084 00085 struct data; 00086 std::auto_ptr<data> d; 00087 00088 struct entry { 00089 entry() : last_used(0) {} 00090 ref_ptr<backend::connection> conn; 00091 std::time_t last_used; 00092 }; 00093 00094 typedef std::list<entry> pool_type; 00095 // non-mutable members 00096 00097 size_t limit_; 00098 int life_time_; 00099 connection_info ci_; 00100 00101 // mutex protected begin 00102 mutex lock_; 00103 size_t size_; 00104 pool_type pool_; 00105 // mutex protected end 00106 00107 }; 00108 } 00109 00110 00111 #endif