Annotation of parser3/src/main/pa_sql_driver_manager.C, revision 1.38

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

E-mail: