Annotation of parser3/src/main/pa_db_manager.C, revision 1.1
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:
! 7: $Id: pa_DB_manager.C,v 1.41 2001/10/19 12:43:30 parser Exp $
! 8: */
! 9:
! 10: #include "pa_db_manager.h"
! 11: #include "ltdl.h"
! 12: #include "pa_DB_connection.h"
! 13: #include "pa_exception.h"
! 14: #include "pa_common.h"
! 15: #include "pa_threads.h"
! 16: #include "pa_stack.h"
! 17:
! 18: // globals
! 19:
! 20: DB_Manager *DB_manager;
! 21:
! 22: // consts
! 23:
! 24: const int EXPIRE_UNUSED_CONNECTION_SECONDS=60;
! 25: const int CHECK_EXPIRED_CONNECTIONS_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2;
! 26:
! 27:
! 28: // DB_Manager
! 29:
! 30: /// @test db_paniccall & co
! 31: DB_Manager::DB_Manager(Pool& pool) : Pooled(pool),
! 32: connection_cache(pool),
! 33: prev_expiration_pass_time(0) {
! 34:
! 35: memset(&dbenv, 0, sizeof(dbenv));
! 36: check("db_appinit", db_appinit(
! 37: 0/*db_home*/,
! 38: 0/*db_config*/,
! 39: &dbenv,
! 40: DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN));
! 41: }
! 42:
! 43: DB_Manager::~DB_Manager() {
! 44: check("db_appexit", db_appexit(&dbenv));
! 45: }
! 46:
! 47:
! 48: void DB_Manager::check(const char *operation, int error) {
! 49: switch(error) {
! 50: case 0:
! 51: // no error
! 52: break;
! 53:
! 54: default:
! 55: throw Exception(0, 0,
! 56: 0/*source*/,
! 57: "db %s error: %s (%d)",
! 58: operation, strerror(errno), errno);
! 59: }
! 60: }
! 61:
! 62: DB_Connection& DB_Manager::get_connection(const String& request_file_spec,
! 63: const String& request_origin) {
! 64: Pool& pool=request_origin.pool(); // request pool
! 65:
! 66: // first trying to get cached connection
! 67: DB_Connection *result=get_connection_from_cache(request_file_spec);
! 68: if(result && !result->ping()) { // we have some cached connection, is it pingable?
! 69: result->disconnect(); // kill unpingabe=dead connection
! 70: result=0;
! 71: }
! 72:
! 73: char *request_file_spec_cstr;
! 74: if(result)
! 75: request_file_spec_cstr=0; // calm, compiler
! 76: else { // no cached connection
! 77: // make global_file_spec C-string on global pool
! 78: request_file_spec_cstr=request_file_spec.cstr(String::UL_AS_IS);
! 79: char *global_file_spec_cstr=(char *)malloc(strlen(request_file_spec_cstr)+1);
! 80: strcpy(global_file_spec_cstr, request_file_spec_cstr);
! 81: // make global_file_spec string on global pool
! 82: String& global_file_spec=*new(this->pool()) String(this->pool(), global_file_spec_cstr);
! 83:
! 84: // allocate in global pool
! 85: // associate with services[request]
! 86: // NOTE: never freed up!
! 87: result=new(this->pool()) DB_Connection(this->pool(), global_file_spec, dbenv);
! 88: }
! 89:
! 90: // associate with services[request] (deassociates at close)
! 91: result->set_services(&pool);
! 92: // if not connected yet, do that now, when result has services
! 93: if(!result->connected())
! 94: result->connect();
! 95: // return it
! 96: return *result;
! 97: }
! 98:
! 99: void DB_Manager::close_connection(const String& file_spec,
! 100: DB_Connection& connection) {
! 101: // deassociate from services[request]
! 102: connection.set_services(0);
! 103: put_connection_to_cache(file_spec, connection);
! 104: }
! 105:
! 106:
! 107: // connection cache
! 108: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
! 109: DB_Connection *DB_Manager::get_connection_from_cache(const String& file_spec) {
! 110: SYNCHRONIZED;
! 111:
! 112: maybe_expire_connection_cache();
! 113:
! 114: if(Stack *connections=static_cast<Stack *>(connection_cache.get(file_spec)))
! 115: while(connections->top_index()>=0) { // there are cached connections to that 'file_spec'
! 116: DB_Connection *result=static_cast<DB_Connection *>(connections->pop());
! 117: if(result->connected()) // not expired?
! 118: return result;
! 119: }
! 120:
! 121: return 0;
! 122: }
! 123:
! 124: void DB_Manager::put_connection_to_cache(const String& file_spec,
! 125: DB_Connection& connection) {
! 126: SYNCHRONIZED;
! 127:
! 128: Stack *connections=static_cast<Stack *>(connection_cache.get(file_spec));
! 129: if(!connections) { // there are no cached connections to that 'file_spec' yet?
! 130: connections=NEW Stack(pool()); // NOTE: never freed up!
! 131: connection_cache.put(file_spec, connections);
! 132: }
! 133: connections->push(&connection);
! 134: }
! 135:
! 136: static void expire_connection(Array::Item *value, void *info) {
! 137: DB_Connection& connection=*static_cast<DB_Connection *>(value);
! 138: time_t older_dies=reinterpret_cast<time_t>(info);
! 139:
! 140: if(connection.connected() && connection.expired(older_dies))
! 141: connection.disconnect();
! 142: }
! 143: static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {
! 144: Stack& stack=*static_cast<Stack *>(value);
! 145: for(int i=0; i<=stack.top_index(); i++)
! 146: expire_connection(stack.get(i), info);
! 147: }
! 148: void DB_Manager::maybe_expire_connection_cache() {
! 149: time_t now=time(0);
! 150:
! 151: if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {
! 152: connection_cache.for_each(expire_connections,
! 153: reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
! 154:
! 155: prev_expiration_pass_time=now;
! 156: }
! 157: }
E-mail: