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