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