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