1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
16 #include<DatabaseManagerImpl.h>
17 #include<DatabaseManager.h>
18 #include<CatalogTables.h>
20 #include<SessionImpl.h>
21 #include<UserManager.h>
22 #include<UserManagerImpl.h>
23 #include<Transaction.h>
28 #if (defined MMDB && defined EMBED)
29 int SessionImpl::noOfThreads
= 0;
32 //Before calling this method, application is required to call readConfigValues
33 DbRetVal
SessionImpl::initSystemDatabase()
39 rv
= readConfigFile();
42 printError(ErrSysInit
, "Configuration file read failed\n");
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());
55 Database
*db
= dbMgr
->sysDb();
57 rv
= db
->getXCheckpointMutex();
60 printError(ErrLockTimeOut
, "Unable to get Database Mutex");
65 db
->createAllCatalogTables();
67 //create the default dba user
68 CatalogTableUSER
cUser(db
);
69 rv
= cUser
.insert(I_USER
, I_PASS
);
72 db
->releaseCheckpointMutex();
76 rv
= cUser
.insert(DBAUSER
, DBAPASS
);
79 db
->releaseCheckpointMutex();
83 //Allocate space for the lock hash bucket
84 ret
= db
->allocLockHashBuckets();
87 db
->releaseCheckpointMutex();
88 printError(ErrSysInit
, "Allocation of Lock buckets failed");
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);
95 //create user database
96 rv
= dbMgr
->createDatabase("userdb", Conf::config
.getMaxDbSize());
97 if (OK
!= rv
) return rv
;
101 DbRetVal
SessionImpl::destroySystemDatabase()
104 rv
= dbMgr
->deleteDatabase(SYSTEMDB
);
105 if (OK
!= rv
) return rv
;
106 rv
= dbMgr
->deleteDatabase("userdb");
107 if (OK
!= rv
) return rv
;
113 DbRetVal
SessionImpl::open(const char *username
, const char *password
)
115 #if (defined MMDB) && (defined EMBED)
116 openEmbeddedConnection(username
, password
);
119 rv
= readConfigFile();
122 printError(ErrSysFatal
, "Configuration file read failed\n");
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.
134 printError(ErrSysInternal
, "Another thread calling open:Wait and then Retry\n");
135 return ErrSysInternal
;
137 rv
= dbMgr
->openSystemDatabase();
140 printError(rv
,"Unable to open the system database");
141 ProcessManager::mutex
.releaseLock(-1, false);
145 rv
= authenticate(username
, password
);
148 dbMgr
->closeSystemDatabase();
149 delete dbMgr
; dbMgr
= NULL
;
150 ProcessManager::mutex
.releaseLock(-1, false);
154 dbMgr
->createTransactionManager();
155 dbMgr
->createLockManager();
156 rv
= dbMgr
->registerThread();
159 printError(rv
,"Unable to register to csql server");
160 dbMgr
->closeSystemDatabase();
161 ProcessManager::mutex
.releaseLock(-1, false);
162 delete dbMgr
; dbMgr
= NULL
;
165 rv
= dbMgr
->openDatabase("userdb");
167 dbMgr
->closeSystemDatabase();
168 ProcessManager::mutex
.releaseLock(-1, false);
169 delete dbMgr
; dbMgr
= NULL
;
172 ProcessManager::mutex
.releaseLock(-1, false);
173 ((DatabaseManagerImpl
*)dbMgr
)->setProcSlot();
174 //ProcessManager::systemDatabase = dbMgr->sysDb();
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
;
191 DbRetVal
SessionImpl::getExclusiveLock()
193 DbRetVal rv
= dbMgr
->sysDb()->getProcessTableMutex(true);
195 printError(ErrLockTimeOut
, "Unable to acquire proc table mutex");
198 if (dbMgr
->isAnyOneRegistered()) {
199 printError(ErrLockTimeOut
, "Unable to acquire exclusive lock. somebody is connected");
200 dbMgr
->sysDb()->releaseProcessTableMutex(true);
201 return ErrLockTimeOut
;
206 DbRetVal
SessionImpl::close()
208 # if (defined MMDB) && (defined EMBED)
209 closeEmbeddedConnection();
212 if (isXTaken
&& dbMgr
) dbMgr
->sysDb()->releaseProcessTableMutex(true);
215 int ret
= ProcessManager::mutex
.tryLock(10,100);
216 //If you are not getting lock ret !=0, it means somebody else is there.
219 printError(ErrSysInternal
, "Another thread calling open:Wait and then Retry\n");
220 return ErrSysInternal
;
223 rv
= dbMgr
->closeDatabase();
225 ProcessManager::mutex
.releaseLock(-1, false);
228 rv
= dbMgr
->deregisterThread();
230 ProcessManager::mutex
.releaseLock(-1, false);
233 rv
= dbMgr
->closeSystemDatabase();
235 ProcessManager::mutex
.releaseLock(-1, false);
238 ProcessManager::mutex
.releaseLock(-1, false);
252 DatabaseManager
* SessionImpl::getDatabaseManager()
257 UserManager
* SessionImpl::getUserManager()
259 if (!isAuthenticated
)
261 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
264 if (uMgr
!= NULL
) return uMgr
;
265 UserManagerImpl
*userMgr
= new UserManagerImpl();
266 if(0 == strcmp(userName
, DBAUSER
))
267 userMgr
->setDba(true);
269 userMgr
->setDba(false);
271 userMgr
->setSysDb(dbMgr
->sysDb());
273 userMgr
->setUserName(userName
);
278 DbRetVal
SessionImpl::startTransaction(IsolationLevel level
)
280 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
282 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
287 rv
= dbMgr
->txnMgr()->startTransaction(dbMgr
->lockMgr(), level
);
292 DbRetVal
SessionImpl::commit()
295 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
297 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
300 rv
= dbMgr
->txnMgr()->commit(dbMgr
->lockMgr());
303 printError(rv
,"Transaction commit failed\n");
310 DbRetVal
SessionImpl::rollback()
313 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
315 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
318 rv
= dbMgr
->txnMgr()->rollback(dbMgr
->lockMgr());
321 printError(rv
, "Transaction rollback failed\n");
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
;
337 printError(ErrSysInit
, "CSQL_CONFIG_FILE environment variable "
343 int rv
= Conf::config
.readAllValues(confFilename
);
344 if (rv
!= 0) return ErrSysInit
;
347 Database
* SessionImpl::getSystemDatabase()
349 return dbMgr
->sysDb();
352 #if (defined MMDB && defined EMBED)
353 DbRetVal
SessionImpl::openEmbeddedConnection(const char *username
, const char *password
)
356 rv
= readConfigFile();
359 printError(ErrSysFatal
, "Configuration file read failed\n");
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.
371 printError(ErrSysInternal
, "Another thread calling open:Wait and then Retry\n");
372 return ErrSysInternal
;
376 ret
= initSystemDatabase();
378 printError(ErrSysInternal
, "Unable to initialize the Database");
379 ProcessManager::mutex
.releaseLock(-1, false);
380 return ErrSysInternal
;
382 ProcessManager::systemDatabase
= dbMgr
->sysDb();
384 rv
= dbMgr
->openSystemDatabase();
387 printError(rv
,"Unable to open the system database");
388 ProcessManager::mutex
.releaseLock(-1, false);
393 rv
= authenticate(username
, password
);
396 delete dbMgr
; dbMgr
= NULL
;
397 ProcessManager::mutex
.releaseLock(-1, false);
401 dbMgr
->createTransactionManager();
402 dbMgr
->createLockManager();
404 rv
= dbMgr
->openDatabase("userdb");
406 dbMgr
->closeSystemDatabase();
407 ProcessManager::mutex
.releaseLock(-1, false);
408 delete dbMgr
; dbMgr
= NULL
;
413 rv
= dbMgr
->registerThread();
416 printError(rv
,"Unable to register to csql server");
417 ProcessManager::mutex
.releaseLock(-1, false);
418 delete dbMgr
; dbMgr
= NULL
;
422 ProcessManager::mutex
.releaseLock(-1, false);
427 DbRetVal
SessionImpl::closeEmbeddedConnection()
432 int ret
= ProcessManager::mutex
.tryLock(10,100);
433 //If you are not getting lock ret !=0, it means somebody else is there.
436 printError(ErrSysInternal
, "Another thread calling open:Wait and then Retry\n");
437 return ErrSysInternal
;
440 rv
= dbMgr
->deregisterThread();
442 ProcessManager::mutex
.releaseLock(-1, false);
445 ProcessManager::mutex
.releaseLock(-1, false);
452 if(noOfThreads
== 1) {
453 destroySystemDatabase();
454 Conf::logger
.stopLogger();