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
41namespace dmlite {
42
43
44extern pthread_once_t initialize_mysql_thread;
45extern pthread_key_t destructor_key;
46
47void destroy_thread(void*);
48void init_thread(void);
49
50class MysqlWrap {
51public:
52 MYSQL *sqlinst;
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
82public:
84
88
89 // Attributes
90 std::string host;
91 unsigned int port;
92 std::string user;
93 std::string passwd;
94
95
97protected:
98private:
99};
100
101/// Holder of mysql connections, base class singleton holding the mysql conn pool
103public:
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
110
111private:
113
114 // Ctor initializes the local mysql factory and
115 // creates the shared pool of mysql conns
117
120
121 /// Connection factory.
123
124 /// Connection pool.
126
127};
128
129
130
131}
132
133
134
135#endif
Definition: mysqlpools.h:81
bool isValid(MysqlWrap *)
Check it is still valid.
std::string host
Definition: mysqlpools.h:90
void destroy(MysqlWrap *)
Destroys an element.
std::string passwd
Definition: mysqlpools.h:93
int dirspacereportdepth
Definition: mysqlpools.h:96
MysqlWrap * create()
Creates an element.
std::string user
Definition: mysqlpools.h:92
unsigned int port
Definition: mysqlpools.h:91
Holder of mysql connections, base class singleton holding the mysql conn pool.
Definition: mysqlpools.h:102
static MySqlHolder * instance
Definition: mysqlpools.h:119
static MySqlHolder * getInstance()
static dmlite::PoolContainer< MysqlWrap * > & getMySqlPool()
int poolsize
Definition: mysqlpools.h:112
MySqlConnectionFactory connectionFactory_
Connection factory.
Definition: mysqlpools.h:122
static bool configure(const std::string &key, const std::string &value)
static dmlite::PoolContainer< MysqlWrap * > * connectionPool_
Connection pool.
Definition: mysqlpools.h:125
static void configure(std::string host, std::string username, std::string password, int port, int poolsize)
Definition: mysqlpools.h:50
MYSQL * sqlinst
Definition: mysqlpools.h:52
MysqlWrap(MYSQL *sql)
Definition: mysqlpools.h:59
bool isValid()
Definition: mysqlpools.h:55
time_t creationtime
Definition: mysqlpools.h:53
~MysqlWrap()
Definition: mysqlpools.h:68
MysqlWrap()
Definition: mysqlpools.h:63
Implements a pool of whichever resource.
Definition: poolcontainer.h:38
Definition: poolcontainer.h:20
Namespace for the dmlite C++ API.
Definition: authn.h:16
void init_thread(void)
pthread_key_t destructor_key
pthread_once_t initialize_mysql_thread
void destroy_thread(void *)