1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
#ifndef MMAN_CXX
#define MMAN_CXX
#include "mman.h"
#include "mcon.h"
#include "mcon.h"
#include "s_mutx.h"
#include "s_ncur.h"
#include "s_tool.h"
using namespace std;
mman::mman(int i_initial, int i_max)
{
pthread_mutex_init( &mut_i_used_con , NULL);
this->i_used_connections=0;
if( i_max > MAXMSQL )
{
#ifdef VERBOSE
cerr << MYLIMIT << MAXMSQL << endl;
#endif
i_max = MAXMSQL;
}
if( i_initial > i_max )
i_initial = i_max;
this->i_initial_connections = i_initial;
this->i_max_connections = i_max;
#ifdef VERBOSE
cout << MYINITC << i_initial << endl
<< MYINITM << i_max << endl;
#endif
}
void mman::init( string host, string user, string passwd, string db, unsigned int port)
{
this->s_host=host;
this->s_user=user;
this->s_pass=passwd;
this->s_db=db;
this->i_port=port;
for(int i=0; i<this->i_initial_connections;i++)
this->mysql.push_back(new_connection( ));
}
#ifdef NCURSES
void
mman::print_init_ncurses()
{
string s_tmp( MYINITC );
s_tmp.append( s_tool::int2string( i_initial_connections ) );
s_ncur::get
().print( s_tmp );
string s_tmp2( MYINITM );
s_tmp2.append( s_tool::int2string( i_max_connections ) );
s_ncur::get
().print( s_tmp2 );
print_used_connections(1);
}
void
mman::print_used_connections( bool b_refresh )
{
pthread_mutex_lock ( &s_mutx::get().mut_stdout );
mvprintw( NCUR_MYSQL_X,NCUR_MYSQL_Y, "MySQL: %d ", i_used_connections);
if ( b_refresh )
refresh();
pthread_mutex_unlock( &s_mutx::get().mut_stdout );
}
#endif
mman::~mman()
{
for(int i = 0; i < this->mysql.size();i++)
{
if(this->mysql[i] != NULL)
mysql_close(this->mysql[i]);
}
pthread_mutex_destroy( &mut_i_used_con );
}
MYSQL *mman::get_connection()
{
if(!this->mysql.empty())
{
MYSQL *x=this->mysql[this->mysql.size()-1];
this->mysql.pop_back();
if(mysql_ping(x)==0)
{
pthread_mutex_lock ( &mut_i_used_con );
this->i_used_connections--;
#ifdef NCURSES
print_used_connections(1);
#endif
pthread_mutex_unlock( &mut_i_used_con );
mysql_close(x);
return new_connection();
}
return x;
}
#ifdef NCURSES
{
s_ncur::get
().print( MYERROR );
}
#endif
#ifdef SERVMSG
cerr << MYERROR << endl;
#endif
return NULL;
}
MYSQL *mman::new_connection( )
{
pthread_mutex_lock ( &mut_i_used_con );
if(this->i_used_connections>this->i_max_connections)
{
#ifdef NCURSES
s_ncur::get().shutdown();
cerr << MYERRO2 << endl;
#endif
#ifdef SERVMSG
cerr << MYERRO2 << endl;
#endif
return NULL;
}
pthread_mutex_unlock( &mut_i_used_con );
MYSQL *ms = mysql_init(NULL);
if(!ms)
{
#ifdef NCURSES
s_ncur::get().shutdown();
cerr << MYERRO1 << endl;
#endif
#ifdef SERVMSG
cerr << MYERRO1 << endl;
#endif
exit(1);
}
if(mysql_real_connect( ms, this->s_host.c_str(), this->s_user.c_str(), this->s_pass.c_str(), this->s_db.c_str()
, this->i_port, NULL, 0 )==NULL)
{
#ifdef NCURSES
s_ncur::get().shutdown();
cerr << MYMANAG << mysql_error(ms) << endl;
#endif
#ifdef SERVMSG
cerr << MYMANAG << mysql_error(ms) << endl;
#endif
exit(1);
}
pthread_mutex_lock ( &mut_i_used_con );
this->i_used_connections++;
#ifdef NCURSES
print_used_connections(0);
#endif
pthread_mutex_unlock( &mut_i_used_con );
return ms;
}
void mman::free_connection( MYSQL *msql)
{
pthread_mutex_lock ( &mut_i_used_con );
this->i_used_connections--;
#ifdef NCURSES
print_used_connections(1);
#endif
pthread_mutex_unlock( &mut_i_used_con );
if(mysql_ping( msql )==0)
{
mysql_close( msql );
this->mysql.push_back( msql );
}
else
this->mysql.push_back( new_connection() );
}
#endif
|