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");
64 db
->createAllCatalogTables();
66 //create the default dba user
67 CatalogTableUSER
cUser(db
);
68 rv
= cUser
.insert(I_USER
, I_PASS
);
71 db
->releaseCheckpointMutex();
74 rv
= cUser
.insert(DBAUSER
, DBAPASS
);
77 db
->releaseCheckpointMutex();
81 //Allocate space for the lock hash bucket
82 ret
= db
->allocLockHashBuckets();
85 db
->releaseCheckpointMutex();
86 printError(ErrSysInit
, "Allocation of Lock buckets failed");
89 Bucket
* buckets
= dbMgr
->sysDb()->getLockHashBuckets();
91 char mutName
[IDENTIFIER_LENGTH
];
92 for (int i
=0; i
< LOCK_BUCKET_SIZE
; i
++)
94 bucket
= &(buckets
[i
]);
95 sprintf(mutName
, "LOCKBKT:%d", i
);
96 bucket
->mutex_
.init(mutName
);
99 db
->releaseCheckpointMutex();
100 #if !(defined MMDB && defined EMBED)
101 printf("Sys_DB [Size=%4.4ldMB] \nUser_DB [Size=%4.4ldMB]\n", Conf::config
.getMaxSysDbSize()/1048576, Conf::config
.getMaxDbSize()/1048576);
103 //create user database
104 rv
= dbMgr
->createDatabase("userdb", Conf::config
.getMaxDbSize());
105 if (OK
!= rv
) return rv
;
109 DbRetVal
SessionImpl::destroySystemDatabase()
112 rv
= dbMgr
->deleteDatabase(SYSTEMDB
);
113 if (OK
!= rv
) return rv
;
114 rv
= dbMgr
->deleteDatabase("userdb");
115 if (OK
!= rv
) return rv
;
121 DbRetVal
SessionImpl::open(const char *username
, const char *password
)
123 #if (defined MMDB) && (defined EMBED)
124 openEmbeddedConnection(username
, password
);
127 rv
= readConfigFile();
130 printError(ErrSysFatal
, "Configuration file read failed\n");
136 dbMgr
= new DatabaseManagerImpl();
138 int ret
= ProcessManager::mutex
.tryLock(10, 100);
139 //If you are not getting lock ret !=0, it means somebody else is there.
142 printError(ErrSysInternal
, "Another thread calling open:Wait and then Retry\n");
143 return ErrSysInternal
;
145 rv
= dbMgr
->openSystemDatabase();
148 printError(rv
,"Unable to open the system database");
149 ProcessManager::mutex
.releaseLock(-1, false);
153 rv
= authenticate(username
, password
);
156 dbMgr
->closeSystemDatabase();
157 delete dbMgr
; dbMgr
= NULL
;
158 ProcessManager::mutex
.releaseLock(-1, false);
162 dbMgr
->createTransactionManager();
163 dbMgr
->createLockManager();
164 rv
= dbMgr
->registerThread();
167 printError(rv
,"Unable to register to csql server");
168 dbMgr
->closeSystemDatabase();
169 ProcessManager::mutex
.releaseLock(-1, false);
170 delete dbMgr
; dbMgr
= NULL
;
173 rv
= dbMgr
->openDatabase("userdb");
175 dbMgr
->closeSystemDatabase();
176 ProcessManager::mutex
.releaseLock(-1, false);
177 delete dbMgr
; dbMgr
= NULL
;
180 ProcessManager::mutex
.releaseLock(-1, false);
181 ((DatabaseManagerImpl
*)dbMgr
)->setProcSlot();
182 //ProcessManager::systemDatabase = dbMgr->sysDb();
187 DbRetVal
SessionImpl::authenticate(const char *username
, const char *password
)
189 CatalogTableUSER
cUser(dbMgr
->sysDb());
190 cUser
.authenticate(username
, password
, isAuthenticated
, isDba
);
191 strcpy(userName
, username
);
192 if (!isAuthenticated
)
194 printError(ErrNoPrivilege
,"User Authentication failed");
195 return ErrNoPrivilege
;
199 DbRetVal
SessionImpl::getExclusiveLock()
201 DbRetVal rv
= dbMgr
->sysDb()->getProcessTableMutex(true);
203 printError(ErrLockTimeOut
, "Unable to acquire proc table mutex");
206 if (dbMgr
->isAnyOneRegistered()) {
207 printError(ErrLockTimeOut
, "Unable to acquire exclusive lock. somebody is connected");
208 dbMgr
->sysDb()->releaseProcessTableMutex(true);
209 return ErrLockTimeOut
;
215 DbRetVal
SessionImpl::close()
217 # if (defined MMDB) && (defined EMBED)
218 closeEmbeddedConnection();
221 if (isXTaken
&& dbMgr
) dbMgr
->sysDb()->releaseProcessTableMutex(true);
224 int ret
= ProcessManager::mutex
.tryLock(10,100);
225 //If you are not getting lock ret !=0, it means somebody else is there.
228 printError(ErrSysInternal
, "Another thread calling open:Wait and then Retry\n");
229 return ErrSysInternal
;
232 rv
= dbMgr
->closeDatabase();
234 ProcessManager::mutex
.releaseLock(-1, false);
237 rv
= dbMgr
->deregisterThread();
239 ProcessManager::mutex
.releaseLock(-1, false);
242 rv
= dbMgr
->closeSystemDatabase();
244 ProcessManager::mutex
.releaseLock(-1, false);
247 ProcessManager::mutex
.releaseLock(-1, false);
261 DatabaseManager
* SessionImpl::getDatabaseManager()
266 UserManager
* SessionImpl::getUserManager()
268 if (!isAuthenticated
)
270 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
273 if (uMgr
!= NULL
) return uMgr
;
274 UserManagerImpl
*userMgr
= new UserManagerImpl();
275 if(0 == strcmp(userName
, DBAUSER
))
276 userMgr
->setDba(true);
278 userMgr
->setDba(false);
280 userMgr
->setSysDb(dbMgr
->sysDb());
282 userMgr
->setUserName(userName
);
287 DbRetVal
SessionImpl::startTransaction(IsolationLevel level
)
289 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
291 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
296 rv
= dbMgr
->txnMgr()->startTransaction(dbMgr
->lockMgr(), level
);
301 DbRetVal
SessionImpl::commit()
304 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
306 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
309 rv
= dbMgr
->txnMgr()->commit(dbMgr
->lockMgr());
312 printError(rv
,"Transaction commit failed\n");
318 DbRetVal
SessionImpl::rollback()
321 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
323 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
326 rv
= dbMgr
->txnMgr()->rollback(dbMgr
->lockMgr());
329 printError(rv
, "Transaction rollback failed\n");
335 DbRetVal
SessionImpl::readConfigFile()
337 // Check if env variable is set or not
338 char *confFilename
= os::getenv("CSQL_CONFIG_FILE");
339 if (confFilename
== NULL
)
341 if (os::fileExists(DEFAULT_CONFIG_FILE
)) {
342 confFilename
= DEFAULT_CONFIG_FILE
;
345 printError(ErrSysInit
, "CSQL_CONFIG_FILE environment variable "
351 int rv
= Conf::config
.readAllValues(confFilename
);
352 if (rv
!= 0) return ErrSysInit
;
356 Database
* SessionImpl::getSystemDatabase()
358 return dbMgr
->sysDb();
361 #if (defined MMDB && defined EMBED)
362 DbRetVal
SessionImpl::openEmbeddedConnection(const char *username
, const char *password
)
365 rv
= readConfigFile();
368 printError(ErrSysFatal
, "Configuration file read failed\n");
374 dbMgr
= new DatabaseManagerImpl();
376 int ret
= ProcessManager::mutex
.tryLock(10, 100);
377 //If you are not getting lock ret !=0, it means somebody else is there.
380 printError(ErrSysInternal
, "Another thread calling open:Wait and then Retry\n");
381 return ErrSysInternal
;
385 ret
= initSystemDatabase();
387 printError(ErrSysInternal
, "Unable to initialize the Database");
388 ProcessManager::mutex
.releaseLock(-1, false);
389 return ErrSysInternal
;
391 ProcessManager::systemDatabase
= dbMgr
->sysDb();
393 rv
= dbMgr
->openSystemDatabase();
396 printError(rv
,"Unable to open the system database");
397 ProcessManager::mutex
.releaseLock(-1, false);
402 rv
= authenticate(username
, password
);
405 delete dbMgr
; dbMgr
= NULL
;
406 ProcessManager::mutex
.releaseLock(-1, false);
410 dbMgr
->createTransactionManager();
411 dbMgr
->createLockManager();
413 rv
= dbMgr
->openDatabase("userdb");
415 dbMgr
->closeSystemDatabase();
416 ProcessManager::mutex
.releaseLock(-1, false);
417 delete dbMgr
; dbMgr
= NULL
;
422 rv
= dbMgr
->registerThread();
425 printError(rv
,"Unable to register to csql server");
426 ProcessManager::mutex
.releaseLock(-1, false);
427 delete dbMgr
; dbMgr
= NULL
;
431 ProcessManager::mutex
.releaseLock(-1, false);
436 DbRetVal
SessionImpl::closeEmbeddedConnection()
441 int ret
= ProcessManager::mutex
.tryLock(10,100);
442 //If you are not getting lock ret !=0, it means somebody else is there.
445 printError(ErrSysInternal
, "Another thread calling open:Wait and then Retry\n");
446 return ErrSysInternal
;
449 rv
= dbMgr
->deregisterThread();
451 ProcessManager::mutex
.releaseLock(-1, false);
454 ProcessManager::mutex
.releaseLock(-1, false);
461 if(noOfThreads
== 1) {
462 destroySystemDatabase();
463 Conf::logger
.stopLogger();