Annotation of parser3/src/main/pa_db_manager.C, revision 1.6
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.6 ! paf 7: $Id: pa_db_manager.C,v 1.5 2001/10/25 09:48:18 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.6 ! paf 30: static void expire_connection(Array::Item *value, void *info) {
! 31: DB_Connection& connection=*static_cast<DB_Connection *>(value);
! 32: time_t older_dies=reinterpret_cast<time_t>(info);
! 33:
! 34: if(connection.ping() && connection.expired(older_dies))
! 35: connection.disconnect();
1.4 parser 36: }
1.6 ! paf 37: static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {
! 38: Stack& stack=*static_cast<Stack *>(value);
! 39: for(int i=0; i<=stack.top_index(); i++)
! 40: expire_connection(stack.get(i), info);
1.4 parser 41: }
1.1 parser 42:
43: // DB_Manager
44:
45: DB_Manager::DB_Manager(Pool& pool) : Pooled(pool),
46: connection_cache(pool),
47: prev_expiration_pass_time(0) {
48:
49: }
50:
51: DB_Manager::~DB_Manager() {
1.6 ! paf 52: // close connections
! 53: connection_cache.for_each(expire_connections,
! 54: reinterpret_cast<void *>(0/* =in the past = expire[close] all*/));
1.1 parser 55: }
56:
57:
1.6 ! paf 58: DB_Connection& DB_Manager::get_connection(const String& request_db_home,
! 59: const String& request_origin) {
! 60: if(request_db_home.size()==0)
! 61: throw Exception(0, 0,
! 62: &request_origin,
! 63: "empty DB_HOME specified");
1.1 parser 64:
65: Pool& pool=request_origin.pool(); // request pool
66:
67: // first trying to get cached connection
1.6 ! paf 68: DB_Connection *result=get_connection_from_cache(request_db_home);
1.1 parser 69:
1.6 ! paf 70: char *request_db_home_cstr;
1.1 parser 71: if(result)
1.6 ! paf 72: request_db_home_cstr=0; // calm, compiler
1.1 parser 73: else { // no cached connection
1.6 ! paf 74: // make global_db_home C-string on global pool
! 75: request_db_home_cstr=request_db_home.cstr(String::UL_AS_IS);
! 76: char *global_db_home_cstr=(char *)malloc(strlen(request_db_home_cstr)+1);
! 77: strcpy(global_db_home_cstr, request_db_home_cstr);
! 78: // make global_db_home string on global pool
! 79: String& global_db_home=*new(this->pool()) String(this->pool(), global_db_home_cstr);
1.1 parser 80:
81: // allocate in global pool
82: // associate with services[request]
83: // NOTE: never freed up!
1.6 ! paf 84: result=new(this->pool()) DB_Connection(this->pool(), global_db_home);
1.1 parser 85: }
86:
87: // associate with services[request] (deassociates at close)
88: result->set_services(&pool);
89: // if not connected yet, do that now, when result has services
90: if(!result->connected())
91: result->connect();
92: // return it
93: return *result;
1.2 parser 94: }
95:
1.6 ! paf 96: void DB_Manager::close_connection(const String& db_home,
1.1 parser 97: DB_Connection& connection) {
98: // deassociate from services[request]
99: connection.set_services(0);
1.6 ! paf 100: put_connection_to_cache(db_home, connection);
1.1 parser 101: }
102:
103:
104: // connection cache
105: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
1.6 ! paf 106: DB_Connection *DB_Manager::get_connection_from_cache(const String& db_home) {
1.1 parser 107: SYNCHRONIZED;
108:
1.6 ! paf 109: if(Stack *connections=static_cast<Stack *>(connection_cache.get(db_home)))
! 110: while(connections->top_index()>=0) { // there are cached connections to that 'db_home'
1.1 parser 111: DB_Connection *result=static_cast<DB_Connection *>(connections->pop());
112: if(result->connected()) // not expired?
113: return result;
114: }
115:
116: return 0;
117: }
118:
1.6 ! paf 119: void DB_Manager::put_connection_to_cache(const String& db_home,
1.1 parser 120: DB_Connection& connection) {
121: SYNCHRONIZED;
122:
1.6 ! paf 123: Stack *connections=static_cast<Stack *>(connection_cache.get(db_home));
! 124: if(!connections) { // there are no cached connections to that 'db_home' yet?
1.1 parser 125: connections=NEW Stack(pool()); // NOTE: never freed up!
1.6 ! paf 126: connection_cache.put(db_home, connections);
1.1 parser 127: }
128: connections->push(&connection);
129: }
130:
131: void DB_Manager::maybe_expire_connection_cache() {
132: time_t now=time(0);
133:
134: if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {
135: connection_cache.for_each(expire_connections,
136: reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
137:
138: prev_expiration_pass_time=now;
139: }
140: }
1.3 parser 141:
142: #endif
E-mail: