Annotation of parser3/src/main/pa_db_manager.C, revision 1.8

1.1       parser      1: /** @file
                      2:        Parser: sql driver manager implementation.
                      3: 
                      4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
                      5:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      6: 
1.8     ! paf         7:        $Id: pa_db_manager.C,v 1.7 2001/10/26 13:48:19 paf Exp $
1.1       parser      8: */
                      9: 
1.3       parser     10: #include "pa_config_includes.h"
                     11: #ifdef HAVE_LIBDB
                     12: 
1.1       parser     13: #include "pa_db_manager.h"
1.2       parser     14: #include "pa_db_connection.h"
1.1       parser     15: #include "pa_exception.h"
                     16: #include "pa_threads.h"
                     17: #include "pa_stack.h"
                     18: 
                     19: // globals
                     20: 
                     21: DB_Manager *DB_manager;
                     22: 
                     23: // consts
                     24: 
                     25: const int EXPIRE_UNUSED_CONNECTION_SECONDS=60;
                     26: const int CHECK_EXPIRED_CONNECTIONS_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2;
                     27: 
1.4       parser     28: // callbacks
                     29: 
1.7       paf        30: static void expire_connection(const Hash::Key& key, Hash::Val *& value, void *info) {
1.6       paf        31:        DB_Connection& connection=*static_cast<DB_Connection *>(value);
                     32:        time_t older_dies=reinterpret_cast<time_t>(info);
                     33: 
1.7       paf        34:        if(connection.expired(older_dies)) {
                     35:                connection.~DB_Connection();  value=0;
                     36:        }
1.4       parser     37: }
1.1       parser     38: 
                     39: // DB_Manager
                     40: 
                     41: DB_Manager::DB_Manager(Pool& pool) : Pooled(pool),
                     42:        connection_cache(pool),
                     43:        prev_expiration_pass_time(0) {
                     44: 
                     45: }
                     46: 
                     47: DB_Manager::~DB_Manager() {
1.6       paf        48:        // close connections
1.7       paf        49:        connection_cache.for_each(expire_connection, 
1.6       paf        50:                reinterpret_cast<void *>(0/* =in the past = expire[close] all*/));
1.1       parser     51: }
                     52: 
1.7       paf        53: /// @test subpools mechanizm. one connection, one subpool. ~connection destructs it
                     54: DB_Connection_ptr DB_Manager::get_connection_ptr(const String& request_db_home,
                     55:                                                                                                   const String *source) {
1.6       paf        56:        if(request_db_home.size()==0)
                     57:                throw Exception(0, 0,
1.7       paf        58:                        source,
1.6       paf        59:                        "empty DB_HOME specified");
1.1       parser     60: 
                     61:        // first trying to get cached connection
1.6       paf        62:        DB_Connection *result=get_connection_from_cache(request_db_home);
1.7       paf        63:        if(!result) { // no cached connection
1.6       paf        64:                // make global_db_home C-string on global pool
1.7       paf        65:                const char *request_db_home_cstr=request_db_home.cstr(String::UL_AS_IS);
1.6       paf        66:                char *global_db_home_cstr=(char *)malloc(strlen(request_db_home_cstr)+1);
                     67:                strcpy(global_db_home_cstr, request_db_home_cstr);
                     68:                // make global_db_home string on global pool
                     69:                String& global_db_home=*new(this->pool()) String(this->pool(), global_db_home_cstr);
1.1       parser     70:                
                     71:                // allocate in global pool 
                     72:                // NOTE: never freed up!
1.6       paf        73:                result=new(this->pool()) DB_Connection(this->pool(), global_db_home);
1.7       paf        74:                // cache it
                     75:                put_connection_to_cache(global_db_home, *result);
1.1       parser     76:        }
                     77: 
1.7       paf        78:        // return auto-it
                     79:        return DB_Connection_ptr(result);
1.2       parser     80: }
                     81: 
1.1       parser     82: // connection cache
                     83: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
1.6       paf        84: DB_Connection *DB_Manager::get_connection_from_cache(const String& db_home) { 
1.1       parser     85:        SYNCHRONIZED;
1.8     ! paf        86: 
        !            87:        maybe_expire_connection_cache();
1.1       parser     88: 
1.7       paf        89:        return static_cast<DB_Connection *>(connection_cache.get(db_home));
1.1       parser     90: }
                     91: 
1.6       paf        92: void DB_Manager::put_connection_to_cache(const String& db_home, 
1.1       parser     93:                                                                                                 DB_Connection& connection) { 
                     94:        SYNCHRONIZED;
                     95: 
1.7       paf        96:        connection_cache.put(db_home, &connection);
1.1       parser     97: }
                     98: 
                     99: void DB_Manager::maybe_expire_connection_cache() {
                    100:        time_t now=time(0);
                    101: 
                    102:        if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {
1.7       paf       103:                connection_cache.for_each(expire_connection, 
1.1       parser    104:                        reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
                    105: 
                    106:                prev_expiration_pass_time=now;
                    107:        }
                    108: }
1.3       parser    109: 
                    110: #endif

E-mail: