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>
27 //Before calling this method, application is required to call readConfigValues
28 DbRetVal
SessionImpl::initSystemDatabase()
34 rv
= readConfigFile();
37 printError(ErrSysInit
, "Configuration file read failed\n");
41 // Conf::config.print();
43 dbMgr
= new DatabaseManagerImpl();
44 rv
= dbMgr
->createDatabase(SYSTEMDB
, Conf::config
.getMaxSysDbSize());
45 if (OK
!= rv
) return rv
;
47 dbMgr
->setSysDb(dbMgr
->db());
50 Database
*db
= dbMgr
->sysDb();
52 rv
= db
->getXCheckpointMutex();
55 printError(ErrLockTimeOut
, "Unable to get Database Mutex");
60 db
->createAllCatalogTables();
62 //create the default dba user
63 CatalogTableUSER
cUser(db
);
64 rv
= cUser
.insert(I_USER
, I_PASS
);
67 db
->releaseCheckpointMutex();
71 rv
= cUser
.insert(DBAUSER
, DBAPASS
);
74 db
->releaseCheckpointMutex();
78 //Allocate space for the lock hash bucket
79 ret
= db
->allocLockHashBuckets();
82 db
->releaseCheckpointMutex();
83 printError(ErrSysInit
, "Allocation of Lock buckets failed");
86 db
->releaseCheckpointMutex();
88 printf("Sys_DB [Size=%4.4ldMB] \nUser_DB [Size=%4.4ldMB]\n", Conf::config
.getMaxSysDbSize()/1048576, Conf::config
.getMaxDbSize()/1048576);
89 //create user database
90 rv
= dbMgr
->createDatabase("userdb", Conf::config
.getMaxDbSize());
91 if (OK
!= rv
) return rv
;
95 DbRetVal
SessionImpl::destroySystemDatabase()
98 rv
= dbMgr
->deleteDatabase(SYSTEMDB
);
99 if (OK
!= rv
) return rv
;
100 rv
= dbMgr
->deleteDatabase("userdb");
101 if (OK
!= rv
) return rv
;
107 DbRetVal
SessionImpl::open(const char *username
, const char *password
)
110 rv
= readConfigFile();
113 printError(ErrSysFatal
, "Configuration file read failed\n");
119 dbMgr
= new DatabaseManagerImpl();
121 int ret
= ProcessManager::mutex
.tryLock(10, 100);
122 //If you are not getting lock ret !=0, it means somebody else is there.
125 printError(ErrSysInternal
, "Another thread calling open:Wait and then Retry\n");
126 return ErrSysInternal
;
128 rv
= dbMgr
->openSystemDatabase();
131 printError(rv
,"Unable to open the system database");
132 ProcessManager::mutex
.releaseLock(-1, false);
136 rv
= authenticate(username
, password
);
139 dbMgr
->closeSystemDatabase();
140 delete dbMgr
; dbMgr
= NULL
;
141 ProcessManager::mutex
.releaseLock(-1, false);
145 dbMgr
->createTransactionManager();
146 dbMgr
->createLockManager();
147 rv
= dbMgr
->registerThread();
150 printError(rv
,"Unable to register to csql server");
151 dbMgr
->closeSystemDatabase();
152 ProcessManager::mutex
.releaseLock(-1, false);
153 delete dbMgr
; dbMgr
= NULL
;
156 rv
= dbMgr
->openDatabase("userdb");
158 dbMgr
->closeSystemDatabase();
159 ProcessManager::mutex
.releaseLock(-1, false);
160 delete dbMgr
; dbMgr
= NULL
;
163 ProcessManager::mutex
.releaseLock(-1, false);
164 ((DatabaseManagerImpl
*)dbMgr
)->setProcSlot();
165 //ProcessManager::systemDatabase = dbMgr->sysDb();
169 DbRetVal
SessionImpl::authenticate(const char *username
, const char *password
)
171 CatalogTableUSER
cUser(dbMgr
->sysDb());
172 cUser
.authenticate(username
, password
, isAuthenticated
, isDba
);
173 strcpy(userName
, username
);
174 if (!isAuthenticated
)
176 printError(ErrNoPrivilege
,"User Authentication failed");
177 return ErrNoPrivilege
;
181 DbRetVal
SessionImpl::getExclusiveLock()
183 DbRetVal rv
= dbMgr
->sysDb()->getProcessTableMutex(true);
185 printError(ErrLockTimeOut
, "Unable to acquire proc table mutex");
188 if (dbMgr
->isAnyOneRegistered()) {
189 printError(ErrLockTimeOut
, "Unable to acquire exclusive lock. somebody is connected");
190 dbMgr
->sysDb()->releaseProcessTableMutex(true);
191 return ErrLockTimeOut
;
196 DbRetVal
SessionImpl::close()
199 if (isXTaken
&& dbMgr
) dbMgr
->sysDb()->releaseProcessTableMutex(true);
202 int ret
= ProcessManager::mutex
.tryLock(10,100);
203 //If you are not getting lock ret !=0, it means somebody else is there.
206 printError(ErrSysInternal
, "Another thread calling open:Wait and then Retry\n");
207 return ErrSysInternal
;
210 rv
= dbMgr
->closeDatabase();
212 ProcessManager::mutex
.releaseLock(-1, false);
215 rv
= dbMgr
->deregisterThread();
217 ProcessManager::mutex
.releaseLock(-1, false);
220 rv
= dbMgr
->closeSystemDatabase();
222 ProcessManager::mutex
.releaseLock(-1, false);
225 ProcessManager::mutex
.releaseLock(-1, false);
238 DatabaseManager
* SessionImpl::getDatabaseManager()
243 UserManager
* SessionImpl::getUserManager()
245 if (!isAuthenticated
)
247 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
250 if (uMgr
!= NULL
) return uMgr
;
251 UserManagerImpl
*userMgr
= new UserManagerImpl();
252 if(0 == strcmp(userName
, DBAUSER
))
253 userMgr
->setDba(true);
255 userMgr
->setDba(false);
257 userMgr
->setSysDb(dbMgr
->sysDb());
259 userMgr
->setUserName(userName
);
264 DbRetVal
SessionImpl::startTransaction(IsolationLevel level
)
266 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
268 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
273 rv
= dbMgr
->txnMgr()->startTransaction(dbMgr
->lockMgr(), level
);
278 DbRetVal
SessionImpl::commit()
281 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
283 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
286 rv
= dbMgr
->txnMgr()->commit(dbMgr
->lockMgr());
289 printError(rv
,"Transaction commit failed\n");
296 DbRetVal
SessionImpl::rollback()
299 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
301 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
304 rv
= dbMgr
->txnMgr()->rollback(dbMgr
->lockMgr());
307 printError(rv
, "Transaction rollback failed\n");
313 DbRetVal
SessionImpl::readConfigFile()
315 // Check if env variable is set or not
316 char *confFilename
= os::getenv("CSQL_CONFIG_FILE");
317 if (confFilename
== NULL
)
319 if (os::fileExists(DEFAULT_CONFIG_FILE
)) {
320 confFilename
= DEFAULT_CONFIG_FILE
;
323 printError(ErrSysInit
, "CSQL_CONFIG_FILE environment variable "
329 int rv
= Conf::config
.readAllValues(confFilename
);
330 if (rv
!= 0) return ErrSysInit
;
333 Database
* SessionImpl::getSystemDatabase()
335 return dbMgr
->sysDb();