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