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