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