Annotation of parser3/src/main/pa_sql_driver_manager.C, revision 1.19
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.19 ! parser 8: $Id: pa_sql_driver_manager.C,v 1.18 2001/05/17 18:36:33 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.16 parser 58: ////__asm int 3;
1.12 parser 59: Pool& pool=request_url.pool(); // request pool
1.1 paf 60:
61: // we have table for locating protocol's library
1.7 paf 62: if(!protocol2driver_and_client)
1.1 paf 63: PTHROW(0, 0,
1.12 parser 64: &request_url,
1.6 paf 65: "$SQL:drivers table must be defined");
1.1 paf 66:
67: // first trying to get cached connection
1.15 parser 68: SQL_Connection *result=get_connection_from_cache(request_url);
69: if(result && !result->ping()) { // we have some cached connection, is it pingable?
70: result->disconnect(); // kill unpingabe=dead connection
71: result=0;
72: }
1.1 paf 73:
1.16 parser 74: char *request_url_cstr;
75: if(result)
1.17 parser 76: request_url_cstr=0; // calm, compiler
1.16 parser 77: else { // no cached connection or it were unpingabe: connect/reconnect
1.15 parser 78: int pos=request_url.pos("://", 3);
79: if(pos<0)
80: PTHROW(0, 0,
81: &request_url,
82: "no protocol specified"); // NOTE: not THROW, but PTHROW
1.1 paf 83:
1.15 parser 84: // make global_url C-string on global pool
1.16 parser 85: request_url_cstr=request_url.cstr(String::UL_AS_IS);
1.15 parser 86: char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1);
87: strcpy(global_url_cstr, request_url_cstr);
88: // make global_url string on global pool
89: String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr);
90:
91: char *request_protocol_cstr=lsplit(&request_url_cstr, ':');
92: // skip "//" after ':'
93: while(*request_url_cstr=='/')
94: request_url_cstr++;
95: // make global_protocol C-string on global pool
96: char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1);
97: strcpy(global_protocol_cstr, request_protocol_cstr);
98: // make global_protocol string on global pool
99: String& global_protocol=*new(this->pool()) String(this->pool(),
100: global_protocol_cstr);
101:
102: SQL_Driver *driver;
103: const String *dlopen_file_spec=0;
104: // first trying to get cached driver
105: if(!(driver=get_driver_from_cache(global_protocol))) {
106: // no cached
107: const String *library=0;
108: if(protocol2driver_and_client->locate(0, global_protocol)) {
109: if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)
110: PTHROW(0, 0,
111: protocol2driver_and_client->origin_string(),
112: "driver library column for protocol '%s' is empty",
113: request_protocol_cstr);
114: if(!(dlopen_file_spec=protocol2driver_and_client->item(2)) || dlopen_file_spec->size()==0)
115: PTHROW(0, 0,
116: protocol2driver_and_client->origin_string(),
117: "client library column for protocol '%s' is empty",
118: request_protocol_cstr);
119: } else
1.1 paf 120: PTHROW(0, 0,
1.15 parser 121: &request_url,
122: "undefined protocol '%s'",
1.12 parser 123: request_protocol_cstr);
1.15 parser 124:
125: const char *filename=library->cstr(String::UL_FILE_NAME);
126: lt_dlhandle handle=lt_dlopen(filename);
127: if (!handle)
1.7 paf 128: PTHROW(0, 0,
1.15 parser 129: library,
130: "can not open the module, %s", lt_dlerror());
1.1 paf 131:
1.15 parser 132: SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle,
133: LIBRARY_CREATE_FUNC_NAME);
134: if(!create)
135: PTHROW(0, 0,
136: library,
137: "function '%s' was not found", LIBRARY_CREATE_FUNC_NAME);
1.1 paf 138:
1.15 parser 139: // create library-driver!
140: driver=(*create)();
1.1 paf 141:
1.15 parser 142: // validate driver api version
143: int driver_api_version=driver->api_version();
144: if(driver_api_version!=SQL_DRIVER_API_VERSION)
145: PTHROW(0, 0,
146: library,
147: "driver implements API version 0x%04X not equal to 0x%04X",
148: driver_api_version, SQL_DRIVER_API_VERSION);
149:
150: // initialise by connecting to sql client dynamic link library
151: const char *dlopen_file_spec_cstr=dlopen_file_spec->cstr(String::UL_FILE_NAME);
152: if(const char *error=driver->initialize(
153: dlopen_file_spec_cstr))
154: PTHROW(0, 0,
155: library,
156: "driver failed to initialize client library '%s', %s",
157: dlopen_file_spec_cstr, error);
158:
159: // cache it
160: put_driver_to_cache(global_protocol, *driver);
161: }
162:
163: // allocate in global pool
1.16 parser 164: // associate with services[request]
1.17 parser 165: // NOTE: never freed up!
1.16 parser 166: result=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver);
1.15 parser 167: }
1.1 paf 168:
1.16 parser 169: // associate with services[request] (deassociates at close)
1.15 parser 170: result->set_services(new(pool) SQL_Driver_services_impl(pool, request_url));
1.16 parser 171: // if not connected yet, do that now, when result has services
1.17 parser 172: if(!result->connected())
1.16 parser 173: result->connect(request_url_cstr);
174: // return it
1.15 parser 175: return *result;
1.1 paf 176: }
177:
178: void SQL_Driver_manager::close_connection(const String& url,
1.16 parser 179: SQL_Connection& connection) {
1.15 parser 180: // deassociate from services[request]
181: connection.set_services(0);
1.1 paf 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) {
1.13 parser 189: SYNCHRONIZED;
190:
1.15 parser 191: return static_cast<SQL_Driver *>(driver_cache.get(protocol));
1.1 paf 192: }
193:
194: void SQL_Driver_manager::put_driver_to_cache(const String& protocol,
195: SQL_Driver& driver) {
1.13 parser 196: SYNCHRONIZED;
197:
1.1 paf 198: driver_cache.put(protocol, &driver);
199: }
200:
201: // connection cache
202:
203: SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) {
1.13 parser 204: SYNCHRONIZED;
205:
1.19 ! parser 206: maybe_expire_connection_cache();
! 207:
1.1 paf 208: if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
1.17 parser 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: }
1.1 paf 214:
215: return 0;
216: }
217:
218: void SQL_Driver_manager::put_connection_to_cache(const String& url,
219: SQL_Connection& connection) {
1.13 parser 220: SYNCHRONIZED;
1.17 parser 221:
1.15 parser 222: Stack *connections=static_cast<Stack *>(connection_cache.get(url));
223: if(!connections) { // there are no cached connections to that 'url' yet?
1.1 paf 224: connections=NEW Stack(pool()); // NOTE: never freed up!
225: connection_cache.put(url, connections);
226: }
227: connections->push(&connection);
228: }
1.17 parser 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);
1.18 parser 233:
1.17 parser 234: if(connection.expired(older_dies))
235: connection.disconnect();
236: }
237: static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {
1.18 parser 238: Stack& stack=*static_cast<Stack *>(value);
239: for(int i=0; i<=stack.top_index(); i++)
240: expire_connection(stack.get(i), info);
1.17 parser 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: }
1.18 parser 251: }
E-mail: