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<Transaction.h>
20 #include<CatalogTables.h>
23 DbRetVal
TransactionManager::startTransaction(IsolationLevel level
)
27 if (trans
->status_
== TransNotUsed
)
29 //the previous transaction shall be used again
30 trans
->status_
= TransRunning
;
31 trans
->isoLevel_
= level
;
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;
42 //Make this free slot, as the current transaction and
45 trans
->status_
= TransRunning
;
46 trans
->isoLevel_
= level
;
50 void TransactionManager::setFirstTrans(Transaction
*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");
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
76 sysdb
->getTransTableMutex();
77 trans
->status_
= TransNotUsed
;
78 sysdb
->releaseTransTableMutex();
79 printDebug(DM_Transaction
, "Committed transaction:%x",trans
);
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");
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
);