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 00051 /// Factory for mysql connections 00052 /// This is just mechanics of how the Poolcontainer class works 00053 /// and wraps the creation of the actual mysql conns 00054 class MySqlConnectionFactory: public dmlite::PoolElementFactory<MYSQL*> { 00055 public: 00056 MySqlConnectionFactory(); 00057 00058 MYSQL* create(); 00059 void destroy(MYSQL*); 00060 bool isValid(MYSQL*); 00061 00062 // Attributes 00063 std::string host; 00064 unsigned int port; 00065 std::string user; 00066 std::string passwd; 00067 00068 00069 int dirspacereportdepth; 00070 protected: 00071 private: 00072 }; 00073 00074 /// Holder of mysql connections, base class singleton holding the mysql conn pool 00075 class MySqlHolder { 00076 public: 00077 00078 static dmlite::PoolContainer<MYSQL*> &getMySqlPool() ; 00079 static bool configure(const std::string& key, const std::string& value); 00080 static void configure(std::string host, std::string username, std::string password, int port, int poolsize); 00081 00082 ~MySqlHolder(); 00083 00084 private: 00085 int poolsize; 00086 00087 // Ctor initializes the local mysql factory and 00088 // creates the shared pool of mysql conns 00089 MySqlHolder(); 00090 00091 static MySqlHolder *getInstance(); 00092 static MySqlHolder *instance; 00093 00094 /// Connection factory. 00095 MySqlConnectionFactory connectionFactory_; 00096 00097 /// Connection pool. 00098 static dmlite::PoolContainer<MYSQL*> *connectionPool_; 00099 00100 }; 00101 00102 00103 00104 } 00105 00106 00107 00108 #endif