00001 /* 00002 * Copyright 2015 CERN 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 * 00016 */ 00017 00018 00019 00020 /// @file MySqlPools.h 00021 /// @brief MySQL pool implementation 00022 /// @author Fabrizio Furano <furano@cern.ch> 00023 /// @date Dec 2015 00024 00025 00026 00027 #ifndef MYSQLPOOLS_H 00028 #define MYSQLPOOLS_H 00029 00030 00031 #ifdef __APPLE__ 00032 #include <bsm/audit_errno.h> 00033 #endif 00034 00035 #include <algorithm> 00036 #include <stdlib.h> 00037 #include "utils/logger.h" 00038 #include "utils/poolcontainer.h" 00039 #include <mysql/mysql.h> 00040 00041 namespace dmlite { 00042 00043 00044 extern pthread_once_t initialize_mysql_thread; 00045 extern pthread_key_t destructor_key; 00046 00047 void destroy_thread(void*); 00048 void init_thread(void); 00049 00050 class MysqlWrap { 00051 public: 00052 MYSQL *sqlinst; 00053 time_t creationtime; 00054 00055 bool isValid() { 00056 return (time(0) < creationtime + 1800); 00057 } 00058 00059 MysqlWrap(MYSQL *sql) { 00060 sqlinst = sql; 00061 creationtime = time(0); 00062 }; 00063 MysqlWrap() { 00064 sqlinst = NULL; 00065 creationtime = time(0); 00066 }; 00067 00068 ~MysqlWrap() { 00069 }; 00070 00071 operator MYSQL* () 00072 { 00073 return sqlinst; 00074 } 00075 }; 00076 00077 00078 /// Factory for mysql connections 00079 /// This is just mechanics of how the Poolcontainer class works 00080 /// and wraps the creation of the actual mysql conns 00081 class MySqlConnectionFactory: public dmlite::PoolElementFactory<MysqlWrap *> { 00082 public: 00083 MySqlConnectionFactory(); 00084 00085 MysqlWrap* create(); 00086 void destroy(MysqlWrap*); 00087 bool isValid(MysqlWrap*); 00088 00089 // Attributes 00090 std::string host; 00091 unsigned int port; 00092 std::string user; 00093 std::string passwd; 00094 00095 00096 int dirspacereportdepth; 00097 protected: 00098 private: 00099 }; 00100 00101 /// Holder of mysql connections, base class singleton holding the mysql conn pool 00102 class MySqlHolder { 00103 public: 00104 00105 static dmlite::PoolContainer<MysqlWrap*> &getMySqlPool() ; 00106 static bool configure(const std::string& key, const std::string& value); 00107 static void configure(std::string host, std::string username, std::string password, int port, int poolsize); 00108 00109 ~MySqlHolder(); 00110 00111 private: 00112 int poolsize; 00113 00114 // Ctor initializes the local mysql factory and 00115 // creates the shared pool of mysql conns 00116 MySqlHolder(); 00117 00118 static MySqlHolder *getInstance(); 00119 static MySqlHolder *instance; 00120 00121 /// Connection factory. 00122 MySqlConnectionFactory connectionFactory_; 00123 00124 /// Connection pool. 00125 static dmlite::PoolContainer<MysqlWrap*> *connectionPool_; 00126 00127 }; 00128 00129 00130 00131 } 00132 00133 00134 00135 #endif