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