dmlite  0.6
mysqlpools.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015 CERN
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 
19 
20 /// @file MySqlPools.h
21 /// @brief MySQL pool implementation
22 /// @author Fabrizio Furano <furano@cern.ch>
23 /// @date Dec 2015
24 
25 
26 
27 #ifndef MYSQLPOOLS_H
28 #define MYSQLPOOLS_H
29 
30 
31 #ifdef __APPLE__
32 #include <bsm/audit_errno.h>
33 #endif
34 
35 #include <algorithm>
36 #include <stdlib.h>
37 #include "utils/logger.h"
38 #include "utils/poolcontainer.h"
39 #include <mysql/mysql.h>
40 
41 namespace dmlite {
42 
43 
44 extern pthread_once_t initialize_mysql_thread;
45 extern pthread_key_t destructor_key;
46 
47 void destroy_thread(void*);
48 void init_thread(void);
49 
50 class MysqlWrap {
51 public:
52  MYSQL *sqlinst;
53  time_t creationtime;
54 
55  bool isValid() {
56  return (time(0) < creationtime + 1800);
57  }
58 
59  MysqlWrap(MYSQL *sql) {
60  sqlinst = sql;
61  creationtime = time(0);
62  };
64  sqlinst = NULL;
65  creationtime = time(0);
66  };
67 
69  };
70 
71  operator MYSQL* ()
72  {
73  return sqlinst;
74  }
75 };
76 
77 
78 /// Factory for mysql connections
79 /// This is just mechanics of how the Poolcontainer class works
80 /// and wraps the creation of the actual mysql conns
82 public:
84 
85  MysqlWrap* create();
86  void destroy(MysqlWrap*);
87  bool isValid(MysqlWrap*);
88 
89  // Attributes
90  std::string host;
91  unsigned int port;
92  std::string user;
93  std::string passwd;
94 
95 
97 protected:
98 private:
99 };
100 
101 /// Holder of mysql connections, base class singleton holding the mysql conn pool
102 class MySqlHolder {
103 public:
104 
106  static bool configure(const std::string& key, const std::string& value);
107  static void configure(std::string host, std::string username, std::string password, int port, int poolsize);
108 
109  ~MySqlHolder();
110 
111 private:
112  int poolsize;
113 
114  // Ctor initializes the local mysql factory and
115  // creates the shared pool of mysql conns
116  MySqlHolder();
117 
118  static MySqlHolder *getInstance();
120 
121  /// Connection factory.
123 
124  /// Connection pool.
126 
127 };
128 
129 
130 
131 }
132 
133 
134 
135 #endif
static dmlite::PoolContainer< MysqlWrap * > * connectionPool_
Connection pool.
Definition: mysqlpools.h:125
Implements a pool of whichever resource.
Definition: poolcontainer.h:38
int poolsize
Definition: mysqlpools.h:112
static bool configure(const std::string &key, const std::string &value)
pthread_once_t initialize_mysql_thread
std::string host
Definition: mysqlpools.h:90
static MySqlHolder * instance
Definition: mysqlpools.h:119
unsigned int port
Definition: mysqlpools.h:91
int dirspacereportdepth
Definition: mysqlpools.h:96
MysqlWrap()
Definition: mysqlpools.h:63
void init_thread(void)
MySqlConnectionFactory connectionFactory_
Connection factory.
Definition: mysqlpools.h:122
std::string passwd
Definition: mysqlpools.h:93
Definition: mysqlpools.h:81
static dmlite::PoolContainer< MysqlWrap * > & getMySqlPool()
pthread_key_t destructor_key
time_t creationtime
Definition: mysqlpools.h:53
MysqlWrap * create()
Creates an element.
void destroy(MysqlWrap *)
Destroys an element.
static MySqlHolder * getInstance()
void destroy_thread(void *)
MYSQL * sqlinst
Definition: mysqlpools.h:52
bool isValid()
Definition: mysqlpools.h:55
~MysqlWrap()
Definition: mysqlpools.h:68
std::string user
Definition: mysqlpools.h:92
Definition: mysqlpools.h:50
MysqlWrap(MYSQL *sql)
Definition: mysqlpools.h:59
Definition: poolcontainer.h:20
bool isValid(MysqlWrap *)
Check it is still valid.
Holder of mysql connections, base class singleton holding the mysql conn pool.
Definition: mysqlpools.h:102