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