Annotation of parser3/src/main/pa_sql_driver_manager.C, revision 1.46
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.46 ! paf 7: $Id: pa_sql_driver_manager.C,v 1.45 2001/10/29 13:04:46 paf 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.45 paf 35: virtual void *malloc(size_t size) { return Pooled::malloc(size, 8); }
1.43 paf 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:
1.44 paf 100: // construct services[request] (deassociates at close)
101: SQL_Driver_services *services=new(pool) SQL_Driver_services_impl(pool, request_url);
102:
1.29 parser 103: // first trying to get cached connection
104: SQL_Connection *result=get_connection_from_cache(request_url);
1.44 paf 105: if(result) {
106: result->set_services(services);
107: if(!result->ping()) { // we have some cached connection, is it pingable?
108: result->disconnect(); // kill unpingabe=dead connection
109: result=0;
110: }
1.29 parser 111: }
112:
113: char *request_url_cstr;
114: if(result)
115: request_url_cstr=0; // calm, compiler
116: else { // no cached connection or it were unpingabe: connect/reconnect
117: int pos=request_url.pos("://", 3);
118: if(pos<0)
1.41 parser 119: throw Exception(0, 0,
1.36 parser 120: request_url.size()?&request_url:&request_origin,
121: "connection string must start with protocol://"); // NOTE: not THROW, but PTHROW
1.29 parser 122:
123: // make global_url C-string on global pool
1.46 ! paf 124: request_url_cstr=request_url.cstr();
1.29 parser 125: char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1);
126: strcpy(global_url_cstr, request_url_cstr);
127: // make global_url string on global pool
128: String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr);
129:
130: char *request_protocol_cstr=lsplit(&request_url_cstr, ':');
131: // skip "//" after ':'
132: while(*request_url_cstr=='/')
133: request_url_cstr++;
134: // make global_protocol C-string on global pool
135: char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1);
136: strcpy(global_protocol_cstr, request_protocol_cstr);
137: // make global_protocol string on global pool
138: String& global_protocol=*new(this->pool()) String(this->pool(),
139: global_protocol_cstr);
140:
141: SQL_Driver *driver;
142: const String *dlopen_file_spec=0;
143: // first trying to get cached driver
144: if(!(driver=get_driver_from_cache(global_protocol))) {
145: // no cached
146: const String *library=0;
147: if(protocol2driver_and_client->locate(0, global_protocol)) {
148: if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)
1.41 parser 149: throw Exception(0, 0,
1.29 parser 150: protocol2driver_and_client->origin_string(),
151: "driver library column for protocol '%s' is empty",
152: request_protocol_cstr);
153: dlopen_file_spec=protocol2driver_and_client->item(2);
154: } else
1.41 parser 155: throw Exception(0, 0,
1.29 parser 156: &request_url,
157: "undefined protocol '%s'",
158: request_protocol_cstr);
159:
160: if(lt_dlinit())
1.41 parser 161: throw Exception(0, 0,
1.29 parser 162: library,
163: "prepare to dynamic loading failed, %s", lt_dlerror());
164:
1.34 parser 165: const char *filename=library->cstr(String::UL_FILE_SPEC);
1.29 parser 166: lt_dlhandle handle=lt_dlopen(filename);
167: if (!handle)
1.41 parser 168: throw Exception(0, 0,
1.29 parser 169: library,
170: "can not open the module, %s", lt_dlerror());
171:
172: SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle,
1.33 parser 173: SQL_DRIVER_CREATE_NAME);
1.29 parser 174: if(!create)
1.41 parser 175: throw Exception(0, 0,
1.29 parser 176: library,
1.33 parser 177: "function '"SQL_DRIVER_CREATE_NAME"' was not found");
1.29 parser 178:
179: // create library-driver!
180: driver=(*create)();
181:
182: // validate driver api version
183: int driver_api_version=driver->api_version();
184: if(driver_api_version!=SQL_DRIVER_API_VERSION)
1.41 parser 185: throw Exception(0, 0,
1.29 parser 186: library,
187: "driver implements API version 0x%04X not equal to 0x%04X",
188: driver_api_version, SQL_DRIVER_API_VERSION);
189:
190: // initialise by connecting to sql client dynamic link library
191: bool specified_dlopen_file_spec=dlopen_file_spec && dlopen_file_spec->size();
192: const char *dlopen_file_spec_cstr=
193: specified_dlopen_file_spec?
1.34 parser 194: dlopen_file_spec->cstr(String::UL_FILE_SPEC):0;
1.29 parser 195: if(const char *error=driver->initialize(
196: dlopen_file_spec_cstr))
1.41 parser 197: throw Exception(0, 0,
1.29 parser 198: library,
199: "driver failed to initialize client library '%s', %s",
200: specified_dlopen_file_spec?dlopen_file_spec_cstr:"unspecifed",
201: error);
202:
203: // cache it
204: put_driver_to_cache(global_protocol, *driver);
205: }
206:
207: // allocate in global pool
208: // associate with services[request]
209: // NOTE: never freed up!
210: result=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver);
1.44 paf 211: // associate with services[request] (deassociates at close)
212: result->set_services(services);
1.29 parser 213: }
214:
215: // if not connected yet, do that now, when result has services
216: if(!result->connected())
217: result->connect(request_url_cstr);
218: // return it
219: return *result;
220: }
221:
222: void SQL_Driver_manager::close_connection(const String& url,
223: SQL_Connection& connection) {
224: // deassociate from services[request]
225: connection.set_services(0);
226: put_connection_to_cache(url, connection);
227: }
228:
229:
230: // driver cache
231:
232: SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
233: SYNCHRONIZED;
234:
235: return static_cast<SQL_Driver *>(driver_cache.get(protocol));
236: }
237:
238: void SQL_Driver_manager::put_driver_to_cache(const String& protocol,
239: SQL_Driver& driver) {
240: SYNCHRONIZED;
241:
242: driver_cache.put(protocol, &driver);
243: }
244:
245: // connection cache
246: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
247: SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) {
248: SYNCHRONIZED;
249:
250: maybe_expire_connection_cache();
251:
252: if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
253: while(connections->top_index()>=0) { // there are cached connections to that 'url'
254: SQL_Connection *result=static_cast<SQL_Connection *>(connections->pop());
255: if(result->connected()) // not expired?
256: return result;
257: }
258:
259: return 0;
260: }
261:
262: void SQL_Driver_manager::put_connection_to_cache(const String& url,
263: SQL_Connection& connection) {
264: SYNCHRONIZED;
265:
266: Stack *connections=static_cast<Stack *>(connection_cache.get(url));
267: if(!connections) { // there are no cached connections to that 'url' yet?
268: connections=NEW Stack(pool()); // NOTE: never freed up!
269: connection_cache.put(url, connections);
270: }
271: connections->push(&connection);
272: }
273:
274: void SQL_Driver_manager::maybe_expire_connection_cache() {
275: time_t now=time(0);
276:
277: if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {
278: connection_cache.for_each(expire_connections,
279: reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
280:
281: prev_expiration_pass_time=now;
282: }
283: }
E-mail: