Bug: 1708102
[csql.git] / src / server / TransactionManager.cxx
blob6eb57d97e1ec2e60dd9589b7e932cdcf9ce6a7ad
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<Transaction.h>
17 #include<Lock.h>
18 #include<Database.h>
19 #include<Allocator.h>
20 #include<CatalogTables.h>
21 #include<Debug.h>
22 #include<Config.h>
23 DbRetVal TransactionManager::startTransaction(IsolationLevel level)
25 if (NULL != trans)
27 if (trans->status_ == TransNotUsed)
29 //the previous transaction shall be used again
30 trans->status_ = TransRunning;
31 trans->isoLevel_ = level;
32 return OK;
35 Transaction *iter = firstTrans;
36 //get free slot from transaction table
37 for (int i =0 ; i < config.getMaxTrans(); i++)
39 if (iter->status_ == TransNotUsed) break;
40 iter++;
42 //Make this free slot, as the current transaction and
43 //set the state
44 trans = iter;
45 trans->status_ = TransRunning;
46 trans->isoLevel_ = level;
47 return OK;
50 void TransactionManager::setFirstTrans(Transaction *trans)
52 firstTrans = trans;
55 DbRetVal TransactionManager::commit(LockManager *lockManager_)
57 Database *sysdb = lockManager_->systemDatabase_;
58 sysdb->getTransTableMutex();
59 if (trans->status_ != TransRunning)
61 printError(ErrBadCall, "Transaction is not in running state\n");
62 return ErrBadCall;
64 trans->status_ = TransCommitting;
65 sysdb->releaseTransTableMutex();
67 trans->releaseAllLocks(lockManager_);
68 if(NULL != trans->waitLock_)
70 printError(ErrSysInternal, "Trans WaitLock is not null\n");
71 return ErrSysInternal;
73 //TODO::flush all redo logs to disk
74 //TODO::remove all the logs in memory
75 trans->removeUndoLogs(sysdb);
76 sysdb->getTransTableMutex();
77 trans->status_ = TransNotUsed;
78 sysdb->releaseTransTableMutex();
79 printDebug(DM_Transaction, "Committed transaction:%x",trans);
80 return OK;
83 DbRetVal TransactionManager::rollback(LockManager *lockManager_)
85 Database *sysdb = lockManager_->systemDatabase_;
86 sysdb->getTransTableMutex();
87 if (trans->status_ != TransRunning)
89 printError(ErrBadCall, "Transaction is not in running state\n");
90 return ErrBadCall;
92 trans->status_ = TransAborting;
93 sysdb->releaseTransTableMutex();
95 trans->applyUndoLogs(sysdb);
96 //TODO::remove all the logs in memory
97 trans->releaseAllLocks(lockManager_);
98 if(NULL != trans->waitLock_)
100 printError(ErrSysInternal, "Trans waitlock is not null");
101 return ErrSysInternal;
104 sysdb->getTransTableMutex();
105 trans->status_ = TransNotUsed;
106 sysdb->releaseTransTableMutex();
107 printDebug(DM_Transaction, "Aborted transaction:%x",trans);
109 return OK;