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