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

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

E-mail: