*** empty log message ***
[csql.git] / src / storage / SessionImpl.cxx
blobb3a7972346f7fb817cfb4c197fb71e2202269e85
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include<DatabaseManagerImpl.h>
17 #include<DatabaseManager.h>
18 #include<CatalogTables.h>
19 #include<Database.h>
20 #include<SessionImpl.h>
21 #include<UserManager.h>
22 #include<UserManagerImpl.h>
23 #include<Transaction.h>
24 #include<Debug.h>
25 #include<Config.h>
26 #include<Process.h>
28 #if (defined MMDB && defined EMBED)
29 int SessionImpl::noOfThreads = 0;
30 #endif
32 //Before calling this method, application is required to call readConfigValues
33 DbRetVal SessionImpl::initSystemDatabase()
36 long value=0;
38 DbRetVal rv = OK;
39 rv = readConfigFile();
40 if (rv != OK)
42 printError(ErrSysInit, "Configuration file read failed\n");
43 return ErrSysInit;
46 // Conf::config.print();
48 if (dbMgr == NULL) dbMgr = new DatabaseManagerImpl();
49 rv = dbMgr->createDatabase(SYSTEMDB, Conf::config.getMaxSysDbSize());
50 if (OK != rv) return rv;
52 dbMgr->setSysDb(dbMgr->db());
53 dbMgr->setDb(NULL);
55 Database *db = dbMgr->sysDb();
57 rv = db->getXCheckpointMutex();
58 if (OK != rv)
60 printError(ErrLockTimeOut, "Unable to get Database Mutex");
61 return rv;
65 db->createAllCatalogTables();
67 //create the default dba user
68 CatalogTableUSER cUser(db);
69 rv = cUser.insert(I_USER, I_PASS);
70 if (OK != rv)
72 db->releaseCheckpointMutex();
73 return rv;
76 rv = cUser.insert(DBAUSER, DBAPASS);
77 if (OK != rv)
79 db->releaseCheckpointMutex();
80 return rv;
82 void *ret = NULL;
83 //Allocate space for the lock hash bucket
84 ret = db->allocLockHashBuckets();
85 if (NULL == ret)
87 db->releaseCheckpointMutex();
88 printError(ErrSysInit, "Allocation of Lock buckets failed");
89 return ErrSysInit;
91 db->releaseCheckpointMutex();
92 #if !(defined MMDB && defined EMBED)
93 printf("Sys_DB [Size=%4.4ldMB] \nUser_DB [Size=%4.4ldMB]\n", Conf::config.getMaxSysDbSize()/1048576, Conf::config.getMaxDbSize()/1048576);
94 #endif
95 //create user database
96 rv = dbMgr->createDatabase("userdb", Conf::config.getMaxDbSize());
97 if (OK != rv) return rv;
98 return OK;
101 DbRetVal SessionImpl::destroySystemDatabase()
103 DbRetVal rv = OK;
104 rv = dbMgr->deleteDatabase(SYSTEMDB);
105 if (OK != rv) return rv;
106 rv = dbMgr->deleteDatabase("userdb");
107 if (OK != rv) return rv;
108 delete dbMgr;
109 dbMgr = NULL;
110 return OK;
113 DbRetVal SessionImpl::open(const char *username, const char *password)
115 #if (defined MMDB) && (defined EMBED)
116 openEmbeddedConnection(username, password);
117 # else
118 DbRetVal rv = OK;
119 rv = readConfigFile();
120 if (rv != OK)
122 printError(ErrSysFatal, "Configuration file read failed\n");
123 return ErrSysFatal;
126 if ( NULL == dbMgr)
128 dbMgr = new DatabaseManagerImpl();
130 int ret = ProcessManager::mutex.tryLock(10, 100);
131 //If you are not getting lock ret !=0, it means somebody else is there.
132 if (ret != 0)
134 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
135 return ErrSysInternal;
137 rv = dbMgr->openSystemDatabase();
138 if (OK != rv)
140 printError(rv,"Unable to open the system database");
141 ProcessManager::mutex.releaseLock(-1, false);
142 return rv;
145 rv = authenticate(username, password);
146 if (OK != rv)
148 dbMgr->closeSystemDatabase();
149 delete dbMgr; dbMgr = NULL;
150 ProcessManager::mutex.releaseLock(-1, false);
151 return rv;
154 dbMgr->createTransactionManager();
155 dbMgr->createLockManager();
156 rv = dbMgr->registerThread();
157 if (OK != rv)
159 printError(rv,"Unable to register to csql server");
160 dbMgr->closeSystemDatabase();
161 ProcessManager::mutex.releaseLock(-1, false);
162 delete dbMgr; dbMgr = NULL;
163 return rv;
165 rv = dbMgr->openDatabase("userdb");
166 if (OK != rv) {
167 dbMgr->closeSystemDatabase();
168 ProcessManager::mutex.releaseLock(-1, false);
169 delete dbMgr; dbMgr = NULL;
170 return rv;
172 ProcessManager::mutex.releaseLock(-1, false);
173 ((DatabaseManagerImpl*)dbMgr)->setProcSlot();
174 //ProcessManager::systemDatabase = dbMgr->sysDb();
175 isXTaken = false;
176 return OK;
177 #endif
179 DbRetVal SessionImpl::authenticate(const char *username, const char *password)
181 CatalogTableUSER cUser(dbMgr->sysDb());
182 cUser.authenticate(username, password, isAuthenticated, isDba);
183 strcpy(userName, username);
184 if (!isAuthenticated)
186 printError(ErrNoPrivilege,"User Authentication failed");
187 return ErrNoPrivilege;
189 return OK;
191 DbRetVal SessionImpl::getExclusiveLock()
193 DbRetVal rv = dbMgr->sysDb()->getProcessTableMutex(true);
194 if (OK != rv) {
195 printError(ErrLockTimeOut, "Unable to acquire proc table mutex");
196 return rv;
198 if (dbMgr->isAnyOneRegistered()) {
199 printError(ErrLockTimeOut, "Unable to acquire exclusive lock. somebody is connected");
200 dbMgr->sysDb()->releaseProcessTableMutex(true);
201 return ErrLockTimeOut;
203 isXTaken = true;
204 return rv;
206 DbRetVal SessionImpl::close()
208 # if (defined MMDB) && (defined EMBED)
209 closeEmbeddedConnection();
210 # else
211 DbRetVal rv = OK;
212 if (isXTaken && dbMgr ) dbMgr->sysDb()->releaseProcessTableMutex(true);
213 if (dbMgr)
215 int ret = ProcessManager::mutex.tryLock(10,100);
216 //If you are not getting lock ret !=0, it means somebody else is there.
217 if (ret != 0)
219 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
220 return ErrSysInternal;
223 rv = dbMgr->closeDatabase();
224 if (rv != OK) {
225 ProcessManager::mutex.releaseLock(-1, false);
226 return ErrBadCall;
228 rv = dbMgr->deregisterThread();
229 if (rv != OK) {
230 ProcessManager::mutex.releaseLock(-1, false);
231 return ErrBadCall;
233 rv = dbMgr->closeSystemDatabase();
234 if (rv != OK) {
235 ProcessManager::mutex.releaseLock(-1, false);
236 return ErrBadCall;
238 ProcessManager::mutex.releaseLock(-1, false);
239 delete dbMgr;
240 dbMgr = NULL;
242 if (uMgr)
244 delete uMgr;
245 uMgr = NULL;
247 isXTaken = false;
248 return OK;
249 #endif
252 DatabaseManager* SessionImpl::getDatabaseManager()
254 return dbMgr;
257 UserManager* SessionImpl::getUserManager()
259 if (!isAuthenticated)
261 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
262 return NULL;
264 if (uMgr != NULL) return uMgr;
265 UserManagerImpl *userMgr = new UserManagerImpl();
266 if(0 == strcmp(userName, DBAUSER))
267 userMgr->setDba(true);
268 else
269 userMgr->setDba(false);
271 userMgr->setSysDb(dbMgr->sysDb());
273 userMgr->setUserName(userName);
274 uMgr = userMgr;
275 return userMgr;
278 DbRetVal SessionImpl::startTransaction(IsolationLevel level)
280 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
282 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
283 return ErrSysFatal;
285 DbRetVal rv = OK;
287 rv = dbMgr->txnMgr()->startTransaction(dbMgr->lockMgr(), level);
288 return rv;
292 DbRetVal SessionImpl::commit()
294 DbRetVal rv = OK;
295 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
297 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
298 return ErrSysFatal;
300 rv = dbMgr->txnMgr()->commit(dbMgr->lockMgr());
301 if (OK != rv)
303 printError(rv,"Transaction commit failed\n");
304 return rv;
306 return OK;
310 DbRetVal SessionImpl::rollback()
312 DbRetVal rv = OK;
313 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
315 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
316 return ErrSysFatal;
318 rv = dbMgr->txnMgr()->rollback(dbMgr->lockMgr());
319 if (OK != rv)
321 printError(rv, "Transaction rollback failed\n");
322 return rv;
324 return OK;
327 DbRetVal SessionImpl::readConfigFile()
329 // Check if env variable is set or not
330 char *confFilename = os::getenv("CSQL_CONFIG_FILE");
331 if (confFilename == NULL)
333 if (os::fileExists(DEFAULT_CONFIG_FILE)) {
334 confFilename = DEFAULT_CONFIG_FILE;
336 else {
337 printError(ErrSysInit, "CSQL_CONFIG_FILE environment variable "
338 "should be set.");
339 return ErrSysInit;
343 int rv = Conf::config.readAllValues(confFilename);
344 if (rv != 0) return ErrSysInit;
345 return OK;
347 Database* SessionImpl::getSystemDatabase()
349 return dbMgr->sysDb();
352 #if (defined MMDB && defined EMBED)
353 DbRetVal SessionImpl::openEmbeddedConnection(const char *username, const char *password)
355 DbRetVal rv = OK;
356 rv = readConfigFile();
357 if (rv != OK)
359 printError(ErrSysFatal, "Configuration file read failed\n");
360 return ErrSysFatal;
363 if ( NULL == dbMgr)
365 dbMgr = new DatabaseManagerImpl();
367 int ret = ProcessManager::mutex.tryLock(10, 100);
368 //If you are not getting lock ret !=0, it means somebody else is there.
369 if (ret != 0)
371 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
372 return ErrSysInternal;
375 if (!noOfThreads) {
376 ret = initSystemDatabase();
377 if (0 != ret) {
378 printError(ErrSysInternal, "Unable to initialize the Database");
379 ProcessManager::mutex.releaseLock(-1, false);
380 return ErrSysInternal;
382 ProcessManager::systemDatabase = dbMgr->sysDb();
383 } else {
384 rv = dbMgr->openSystemDatabase();
385 if (OK != rv)
387 printError(rv,"Unable to open the system database");
388 ProcessManager::mutex.releaseLock(-1, false);
389 return rv;
393 rv = authenticate(username, password);
394 if (OK != rv)
396 delete dbMgr; dbMgr = NULL;
397 ProcessManager::mutex.releaseLock(-1, false);
398 return rv;
401 dbMgr->createTransactionManager();
402 dbMgr->createLockManager();
403 if (noOfThreads) {
404 rv = dbMgr->openDatabase("userdb");
405 if (OK != rv) {
406 dbMgr->closeSystemDatabase();
407 ProcessManager::mutex.releaseLock(-1, false);
408 delete dbMgr; dbMgr = NULL;
409 return rv;
413 rv = dbMgr->registerThread();
414 if (OK != rv)
416 printError(rv,"Unable to register to csql server");
417 ProcessManager::mutex.releaseLock(-1, false);
418 delete dbMgr; dbMgr = NULL;
419 return rv;
422 ProcessManager::mutex.releaseLock(-1, false);
423 noOfThreads++;
424 return OK;
427 DbRetVal SessionImpl::closeEmbeddedConnection()
429 DbRetVal rv = OK;
430 if (dbMgr)
432 int ret = ProcessManager::mutex.tryLock(10,100);
433 //If you are not getting lock ret !=0, it means somebody else is there.
434 if (ret != 0)
436 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
437 return ErrSysInternal;
440 rv = dbMgr->deregisterThread();
441 if (rv != OK) {
442 ProcessManager::mutex.releaseLock(-1, false);
443 return ErrBadCall;
445 ProcessManager::mutex.releaseLock(-1, false);
447 if (uMgr)
449 delete uMgr;
450 uMgr = NULL;
452 if(noOfThreads == 1) {
453 destroySystemDatabase();
454 Conf::logger.stopLogger();
456 noOfThreads--;
457 return OK;
459 #endif