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