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