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 Bucket
* buckets
= dbMgr
->sysDb()->getLockHashBuckets();
93 char mutName
[IDENTIFIER_LENGTH
];
94 for (int i
=0; i
< LOCK_BUCKET_SIZE
; i
++)
96 bucket
= &(buckets
[i
]);
97 sprintf(mutName
, "LOCKBKT:%d", i
);
98 bucket
->mutex_
.init(mutName
);
101 db
->releaseCheckpointMutex();
102 #if !(defined MMDB && defined EMBED)
103 printf("Sys_DB [Size=%4.4ldMB] \nUser_DB [Size=%4.4ldMB]\n", Conf::config
.getMaxSysDbSize()/1048576, Conf::config
.getMaxDbSize()/1048576);
105 //create user database
106 rv
= dbMgr
->createDatabase("userdb", Conf::config
.getMaxDbSize());
107 if (OK
!= rv
) return rv
;
111 DbRetVal
SessionImpl::destroySystemDatabase()
114 rv
= dbMgr
->deleteDatabase(SYSTEMDB
);
115 if (OK
!= rv
) return rv
;
116 rv
= dbMgr
->deleteDatabase("userdb");
117 if (OK
!= rv
) return rv
;
123 DbRetVal
SessionImpl::open(const char *username
, const char *password
)
125 #if (defined MMDB) && (defined EMBED)
126 openEmbeddedConnection(username
, password
);
129 rv
= readConfigFile();
132 printError(ErrSysFatal
, "Configuration file read failed\n");
138 dbMgr
= new DatabaseManagerImpl();
140 int ret
= ProcessManager::mutex
.tryLock(10, 100);
141 //If you are not getting lock ret !=0, it means somebody else is there.
144 printError(ErrSysInternal
, "Another thread calling open:Wait and then Retry\n");
145 return ErrSysInternal
;
147 rv
= dbMgr
->openSystemDatabase();
150 printError(rv
,"Unable to open the system database");
151 ProcessManager::mutex
.releaseLock(-1, false);
155 rv
= authenticate(username
, password
);
158 dbMgr
->closeSystemDatabase();
159 delete dbMgr
; dbMgr
= NULL
;
160 ProcessManager::mutex
.releaseLock(-1, false);
164 dbMgr
->createTransactionManager();
165 dbMgr
->createLockManager();
166 rv
= dbMgr
->registerThread();
169 printError(rv
,"Unable to register to csql server");
170 dbMgr
->closeSystemDatabase();
171 ProcessManager::mutex
.releaseLock(-1, false);
172 delete dbMgr
; dbMgr
= NULL
;
175 rv
= dbMgr
->openDatabase("userdb");
177 dbMgr
->closeSystemDatabase();
178 ProcessManager::mutex
.releaseLock(-1, false);
179 delete dbMgr
; dbMgr
= NULL
;
182 ProcessManager::mutex
.releaseLock(-1, false);
183 ((DatabaseManagerImpl
*)dbMgr
)->setProcSlot();
184 //ProcessManager::systemDatabase = dbMgr->sysDb();
189 DbRetVal
SessionImpl::authenticate(const char *username
, const char *password
)
191 CatalogTableUSER
cUser(dbMgr
->sysDb());
192 cUser
.authenticate(username
, password
, isAuthenticated
, isDba
);
193 strcpy(userName
, username
);
194 if (!isAuthenticated
)
196 printError(ErrNoPrivilege
,"User Authentication failed");
197 return ErrNoPrivilege
;
201 DbRetVal
SessionImpl::getExclusiveLock()
203 DbRetVal rv
= dbMgr
->sysDb()->getProcessTableMutex(true);
205 printError(ErrLockTimeOut
, "Unable to acquire proc table mutex");
208 if (dbMgr
->isAnyOneRegistered()) {
209 printError(ErrLockTimeOut
, "Unable to acquire exclusive lock. somebody is connected");
210 dbMgr
->sysDb()->releaseProcessTableMutex(true);
211 return ErrLockTimeOut
;
216 DbRetVal
SessionImpl::close()
218 # if (defined MMDB) && (defined EMBED)
219 closeEmbeddedConnection();
222 if (isXTaken
&& dbMgr
) dbMgr
->sysDb()->releaseProcessTableMutex(true);
225 int ret
= ProcessManager::mutex
.tryLock(10,100);
226 //If you are not getting lock ret !=0, it means somebody else is there.
229 printError(ErrSysInternal
, "Another thread calling open:Wait and then Retry\n");
230 return ErrSysInternal
;
233 rv
= dbMgr
->closeDatabase();
235 ProcessManager::mutex
.releaseLock(-1, false);
238 rv
= dbMgr
->deregisterThread();
240 ProcessManager::mutex
.releaseLock(-1, false);
243 rv
= dbMgr
->closeSystemDatabase();
245 ProcessManager::mutex
.releaseLock(-1, false);
248 ProcessManager::mutex
.releaseLock(-1, false);
262 DatabaseManager
* SessionImpl::getDatabaseManager()
267 UserManager
* SessionImpl::getUserManager()
269 if (!isAuthenticated
)
271 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
274 if (uMgr
!= NULL
) return uMgr
;
275 UserManagerImpl
*userMgr
= new UserManagerImpl();
276 if(0 == strcmp(userName
, DBAUSER
))
277 userMgr
->setDba(true);
279 userMgr
->setDba(false);
281 userMgr
->setSysDb(dbMgr
->sysDb());
283 userMgr
->setUserName(userName
);
288 DbRetVal
SessionImpl::startTransaction(IsolationLevel level
)
290 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
292 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
297 rv
= dbMgr
->txnMgr()->startTransaction(dbMgr
->lockMgr(), level
);
302 DbRetVal
SessionImpl::commit()
305 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
307 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
310 rv
= dbMgr
->txnMgr()->commit(dbMgr
->lockMgr());
313 printError(rv
,"Transaction commit failed\n");
320 DbRetVal
SessionImpl::rollback()
323 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
325 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
328 rv
= dbMgr
->txnMgr()->rollback(dbMgr
->lockMgr());
331 printError(rv
, "Transaction rollback failed\n");
337 DbRetVal
SessionImpl::readConfigFile()
339 // Check if env variable is set or not
340 char *confFilename
= os::getenv("CSQL_CONFIG_FILE");
341 if (confFilename
== NULL
)
343 if (os::fileExists(DEFAULT_CONFIG_FILE
)) {
344 confFilename
= DEFAULT_CONFIG_FILE
;
347 printError(ErrSysInit
, "CSQL_CONFIG_FILE environment variable "
353 int rv
= Conf::config
.readAllValues(confFilename
);
354 if (rv
!= 0) return ErrSysInit
;
357 Database
* SessionImpl::getSystemDatabase()
359 return dbMgr
->sysDb();
362 #if (defined MMDB && defined EMBED)
363 DbRetVal
SessionImpl::openEmbeddedConnection(const char *username
, const char *password
)
366 rv
= readConfigFile();
369 printError(ErrSysFatal
, "Configuration file read failed\n");
375 dbMgr
= new DatabaseManagerImpl();
377 int ret
= ProcessManager::mutex
.tryLock(10, 100);
378 //If you are not getting lock ret !=0, it means somebody else is there.
381 printError(ErrSysInternal
, "Another thread calling open:Wait and then Retry\n");
382 return ErrSysInternal
;
386 ret
= initSystemDatabase();
388 printError(ErrSysInternal
, "Unable to initialize the Database");
389 ProcessManager::mutex
.releaseLock(-1, false);
390 return ErrSysInternal
;
392 ProcessManager::systemDatabase
= dbMgr
->sysDb();
394 rv
= dbMgr
->openSystemDatabase();
397 printError(rv
,"Unable to open the system database");
398 ProcessManager::mutex
.releaseLock(-1, false);
403 rv
= authenticate(username
, password
);
406 delete dbMgr
; dbMgr
= NULL
;
407 ProcessManager::mutex
.releaseLock(-1, false);
411 dbMgr
->createTransactionManager();
412 dbMgr
->createLockManager();
414 rv
= dbMgr
->openDatabase("userdb");
416 dbMgr
->closeSystemDatabase();
417 ProcessManager::mutex
.releaseLock(-1, false);
418 delete dbMgr
; dbMgr
= NULL
;
423 rv
= dbMgr
->registerThread();
426 printError(rv
,"Unable to register to csql server");
427 ProcessManager::mutex
.releaseLock(-1, false);
428 delete dbMgr
; dbMgr
= NULL
;
432 ProcessManager::mutex
.releaseLock(-1, false);
437 DbRetVal
SessionImpl::closeEmbeddedConnection()
442 int ret
= ProcessManager::mutex
.tryLock(10,100);
443 //If you are not getting lock ret !=0, it means somebody else is there.
446 printError(ErrSysInternal
, "Another thread calling open:Wait and then Retry\n");
447 return ErrSysInternal
;
450 rv
= dbMgr
->deregisterThread();
452 ProcessManager::mutex
.releaseLock(-1, false);
455 ProcessManager::mutex
.releaseLock(-1, false);
462 if(noOfThreads
== 1) {
463 destroySystemDatabase();
464 Conf::logger
.stopLogger();