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

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

E-mail: