Annotation of parser3/src/main/pa_sql_driver_manager.C, revision 1.35
1.29 parser 1: /** @file
2: Parser: sql driver manager implementation.
3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
5:
6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7: */
1.35 ! parser 8: static const char *RCSId="$Id: pa_sql_driver_manager.C,v 1.34 2001/09/14 15:41:59 parser Exp $";
1.29 parser 9:
10: #include "pa_sql_driver_manager.h"
11: #include "ltdl.h"
12: #include "pa_sql_connection.h"
13: #include "pa_exception.h"
14: #include "pa_common.h"
15: #include "pa_threads.h"
16:
17: // globals
18:
19: SQL_Driver_manager *SQL_driver_manager;
20:
21: // consts
22:
23: const int EXPIRE_UNUSED_CONNECTION_SECONDS=60;
1.30 parser 24: const int CHECK_EXPIRED_CONNECTIONS_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2;
1.29 parser 25:
26:
27: /// SQL_Driver_services Pooled implementation
28: class SQL_Driver_services_impl : public SQL_Driver_services, public Pooled {
29: public:
30: SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool),
31: furl(aurl) {
32: }
33:
34: /// allocates some bytes on pool
35: void *malloc(size_t size) { return Pooled::malloc(size); }
36: /// allocates some bytes clearing them with zeros
37: void *calloc(size_t size) { return Pooled::calloc(size); }
38: /// throw exception
39: void _throw(const char *comment) {
40: THROW(0, 0,
41: &furl,
42: comment);
43: }
44:
45: private:
46: const String& furl;
47: };
48:
49: // SQL_Driver_manager
50:
51: /// @param request_url protocol://[driver-dependent]
52: SQL_Connection& SQL_Driver_manager::get_connection(const String& request_url,
53: Table *protocol2driver_and_client) {
54: Pool& pool=request_url.pool(); // request pool
55:
56: // we have table for locating protocol's library
57: if(!protocol2driver_and_client)
58: PTHROW(0, 0,
59: &request_url,
1.31 parser 60: "$"MAIN_SQL_NAME":"MAIN_SQL_DRIVERS_NAME" table must be defined");
1.29 parser 61:
62: // first trying to get cached connection
63: SQL_Connection *result=get_connection_from_cache(request_url);
64: if(result && !result->ping()) { // we have some cached connection, is it pingable?
65: result->disconnect(); // kill unpingabe=dead connection
66: result=0;
67: }
68:
69: char *request_url_cstr;
70: if(result)
71: request_url_cstr=0; // calm, compiler
72: else { // no cached connection or it were unpingabe: connect/reconnect
73: int pos=request_url.pos("://", 3);
74: if(pos<0)
75: PTHROW(0, 0,
76: &request_url,
77: "no protocol specified"); // NOTE: not THROW, but PTHROW
78:
79: // make global_url C-string on global pool
80: request_url_cstr=request_url.cstr(String::UL_AS_IS);
81: char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1);
82: strcpy(global_url_cstr, request_url_cstr);
83: // make global_url string on global pool
84: String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr);
85:
86: char *request_protocol_cstr=lsplit(&request_url_cstr, ':');
87: // skip "//" after ':'
88: while(*request_url_cstr=='/')
89: request_url_cstr++;
90: // make global_protocol C-string on global pool
91: char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1);
92: strcpy(global_protocol_cstr, request_protocol_cstr);
93: // make global_protocol string on global pool
94: String& global_protocol=*new(this->pool()) String(this->pool(),
95: global_protocol_cstr);
96:
97: SQL_Driver *driver;
98: const String *dlopen_file_spec=0;
99: // first trying to get cached driver
100: if(!(driver=get_driver_from_cache(global_protocol))) {
101: // no cached
102: const String *library=0;
103: if(protocol2driver_and_client->locate(0, global_protocol)) {
104: if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)
105: PTHROW(0, 0,
106: protocol2driver_and_client->origin_string(),
107: "driver library column for protocol '%s' is empty",
108: request_protocol_cstr);
109: dlopen_file_spec=protocol2driver_and_client->item(2);
110: } else
111: PTHROW(0, 0,
112: &request_url,
113: "undefined protocol '%s'",
114: request_protocol_cstr);
115:
116: if(lt_dlinit())
117: PTHROW(0, 0,
118: library,
119: "prepare to dynamic loading failed, %s", lt_dlerror());
120:
1.34 parser 121: const char *filename=library->cstr(String::UL_FILE_SPEC);
1.29 parser 122: lt_dlhandle handle=lt_dlopen(filename);
123: if (!handle)
124: PTHROW(0, 0,
125: library,
126: "can not open the module, %s", lt_dlerror());
127:
128: SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle,
1.33 parser 129: SQL_DRIVER_CREATE_NAME);
1.29 parser 130: if(!create)
131: PTHROW(0, 0,
132: library,
1.33 parser 133: "function '"SQL_DRIVER_CREATE_NAME"' was not found");
1.29 parser 134:
135: // create library-driver!
136: driver=(*create)();
137:
138: // validate driver api version
139: int driver_api_version=driver->api_version();
140: if(driver_api_version!=SQL_DRIVER_API_VERSION)
141: PTHROW(0, 0,
142: library,
143: "driver implements API version 0x%04X not equal to 0x%04X",
144: driver_api_version, SQL_DRIVER_API_VERSION);
145:
146: // initialise by connecting to sql client dynamic link library
147: bool specified_dlopen_file_spec=dlopen_file_spec && dlopen_file_spec->size();
148: const char *dlopen_file_spec_cstr=
149: specified_dlopen_file_spec?
1.34 parser 150: dlopen_file_spec->cstr(String::UL_FILE_SPEC):0;
1.29 parser 151: if(const char *error=driver->initialize(
152: dlopen_file_spec_cstr))
153: PTHROW(0, 0,
154: library,
155: "driver failed to initialize client library '%s', %s",
156: specified_dlopen_file_spec?dlopen_file_spec_cstr:"unspecifed",
157: error);
158:
159: // cache it
160: put_driver_to_cache(global_protocol, *driver);
161: }
162:
163: // allocate in global pool
164: // associate with services[request]
165: // NOTE: never freed up!
166: result=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver);
167: }
168:
169: // associate with services[request] (deassociates at close)
170: result->set_services(new(pool) SQL_Driver_services_impl(pool, request_url));
171: // if not connected yet, do that now, when result has services
172: if(!result->connected())
173: result->connect(request_url_cstr);
174: // return it
175: return *result;
176: }
177:
178: void SQL_Driver_manager::close_connection(const String& url,
179: SQL_Connection& connection) {
180: // deassociate from services[request]
181: connection.set_services(0);
182: put_connection_to_cache(url, connection);
183: }
184:
185:
186: // driver cache
187:
188: SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
189: SYNCHRONIZED;
190:
191: return static_cast<SQL_Driver *>(driver_cache.get(protocol));
192: }
193:
194: void SQL_Driver_manager::put_driver_to_cache(const String& protocol,
195: SQL_Driver& driver) {
196: SYNCHRONIZED;
197:
198: driver_cache.put(protocol, &driver);
199: }
200:
201: // connection cache
202: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
203: SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) {
204: SYNCHRONIZED;
205:
206: maybe_expire_connection_cache();
207:
208: if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
209: while(connections->top_index()>=0) { // there are cached connections to that 'url'
210: SQL_Connection *result=static_cast<SQL_Connection *>(connections->pop());
211: if(result->connected()) // not expired?
212: return result;
213: }
214:
215: return 0;
216: }
217:
218: void SQL_Driver_manager::put_connection_to_cache(const String& url,
219: SQL_Connection& connection) {
220: SYNCHRONIZED;
221:
222: Stack *connections=static_cast<Stack *>(connection_cache.get(url));
223: if(!connections) { // there are no cached connections to that 'url' yet?
224: connections=NEW Stack(pool()); // NOTE: never freed up!
225: connection_cache.put(url, connections);
226: }
227: connections->push(&connection);
228: }
229:
230: static void expire_connection(Array::Item *value, void *info) {
231: SQL_Connection& connection=*static_cast<SQL_Connection *>(value);
232: time_t older_dies=reinterpret_cast<time_t>(info);
233:
234: if(connection.connected() && connection.expired(older_dies))
235: connection.disconnect();
236: }
237: static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {
238: Stack& stack=*static_cast<Stack *>(value);
239: for(int i=0; i<=stack.top_index(); i++)
240: expire_connection(stack.get(i), info);
241: }
242: void SQL_Driver_manager::maybe_expire_connection_cache() {
243: time_t now=time(0);
244:
245: if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {
246: connection_cache.for_each(expire_connections,
247: reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
248:
249: prev_expiration_pass_time=now;
250: }
251: }
E-mail: