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 
51 /// Factory for mysql connections
52 /// This is just mechanics of how the Poolcontainer class works
53 /// and wraps the creation of the actual mysql conns
55 public:
57 
58  MYSQL* create();
59  void destroy(MYSQL*);
60  bool isValid(MYSQL*);
61 
62  // Attributes
63  std::string host;
64  unsigned int port;
65  std::string user;
66  std::string passwd;
67 
68 
70 protected:
71 private:
72 };
73 
74 /// Holder of mysql connections, base class singleton holding the mysql conn pool
75 class MySqlHolder {
76 public:
77 
79  static bool configure(const std::string& key, const std::string& value);
80  static void configure(std::string host, std::string username, std::string password, int port, int poolsize);
81 
82  ~MySqlHolder();
83 
84 private:
85  int poolsize;
86 
87  // Ctor initializes the local mysql factory and
88  // creates the shared pool of mysql conns
89  MySqlHolder();
90 
91  static MySqlHolder *getInstance();
93 
94  /// Connection factory.
96 
97  /// Connection pool.
99 
100 };
101 
102 
103 
104 }
105 
106 
107 
108 #endif
MYSQL * create()
Creates an element.
Implements a pool of whichever resource.
Definition: poolcontainer.h:38
int poolsize
Definition: mysqlpools.h:85
static bool configure(const std::string &key, const std::string &value)
pthread_once_t initialize_mysql_thread
std::string host
Definition: mysqlpools.h:63
void destroy(MYSQL *)
Destroys an element.
static MySqlHolder * instance
Definition: mysqlpools.h:92
unsigned int port
Definition: mysqlpools.h:64
Base exception class.
Definition: exceptions.h:17
int dirspacereportdepth
Definition: mysqlpools.h:69
void init_thread(void)
static dmlite::PoolContainer< MYSQL * > * connectionPool_
Connection pool.
Definition: mysqlpools.h:98
MySqlConnectionFactory connectionFactory_
Connection factory.
Definition: mysqlpools.h:95
std::string passwd
Definition: mysqlpools.h:66
static dmlite::PoolContainer< MYSQL * > & getMySqlPool()
Definition: mysqlpools.h:54
pthread_key_t destructor_key
static MySqlHolder * getInstance()
void destroy_thread(void *)
bool isValid(MYSQL *)
Check it is still valid.
std::string user
Definition: mysqlpools.h:65
Namespace for the dmlite C++ API.
Definition: authn.h:15
Definition: poolcontainer.h:20
Holder of mysql connections, base class singleton holding the mysql conn pool.
Definition: mysqlpools.h:75