00001 00002 #ifndef d_Transaction_h 00003 #define d_Transaction_h 1 00004 00005 //------------------------------------------------------------------------------ 00006 #include <map> 00007 //------------------------------------------------------------------------------ 00008 #include "oContext.h" 00009 #include "oTxObjStreamManager.h" 00010 //------------------------------------------------------------------------------ 00011 class _ODLL d_Object; 00012 //------------------------------------------------------------------------------ 00013 /** 00014 * The transaction is a logical operation that executes more operations 00015 * together. 00016 * 00017 * All database operations must be executed inside an open transaction. 00018 * 00019 * Orient ODBMS's transactions have ACID properties: 00020 * 00021 * Atomicity: a transaction is treated as a unit of operation. Therefore, 00022 * either all or none of the transaction's actions are completed. 00023 * Consistency: a transaction must guaranty the consistency of the database. If 00024 * a failure happens while a transaction is open or is committing 00025 * the database must be consistent after the failure. 00026 * Isolation: an executing transaction cannot reveal its results to other 00027 * concurrent transactions before it commits. 00028 * Durability: referes to the property of transactions that ensures that once 00029 * a transaction commits, its results are permanent and cannot be 00030 * erased from the database. 00031 * 00032 * NESTED TRANSACTIONS: 00033 * Orient supports nested transactions. A nested transaction is a transaction 00034 * open inside another transaction. All operations executed being associated 00035 * to the last open transaction. 00036 * 00037 * Nested transactions are totally separate. Thus an abort to a transaction at 00038 * a lower level doesn't abort the higher level transaction. An abort to a 00039 * transaction at a higher level doesn't abort the lower level transaction. 00040 * 00041 * There is no limit to the number of transactions nested. 00042 * 00043 * JUST EDITION: 00044 * The most of operations are executed only in memory without involving the 00045 * disk storage optimizing performance with no I/O cost until commit operation. 00046 * 00047 * ENTERPRISE EDITION: 00048 * The most of operations are executed only on the client side without involving 00049 * the Dynamic Server optimizing performance with no network traffic and leaving 00050 * the Dynamic Server free. 00051 * 00052 * LOCATION: 00053 * d_Transaction.h 00054 * 00055 * USAGE: 00056 * d_Transaction <variable-name>; 00057 * 00058 * Where: 00059 * 00060 * <variable-name>: Name of variable 00061 * 00062 * EXAMPLES: 00063 * d_Transaction tx; 00064 * 00065 * tx.begin(); 00066 * 00067 * .. .. 00068 * 00069 * tx.commit(); 00070 * 00071 * ODMG: 00072 * Compliant 00073 */ 00074 class _ODLL d_Transaction : public oContext 00075 { 00076 public: 00077 00078 /** 00079 * Default constructor. 00080 * 00081 * The constructor doesn't begin the transaction. 00082 * 00083 * Optional parameter <iDb> makes the transaction persistent for crash 00084 * recovery. 00085 * 00086 * ODMG : Compliant + Extension 00087 */ 00088 d_Transaction ( d_Database* iDb = 0 ); 00089 00090 // Internal constructor. 00091 d_Transaction ( oStorage* iStg ); 00092 00093 /** 00094 * Destructor. 00095 * 00096 * If the transaction is open (begin()), then an abort will occurs. 00097 * 00098 * ODMG : Compliant 00099 */ 00100 ~d_Transaction(); 00101 00102 /** 00103 * Starts the transaction. 00104 * 00105 * All database operations must be executed inside an open transaction, 00106 * otherwise an exception d_Error_TransactionNotOpen is raised. 00107 * 00108 * If the transaction is already open, than an exception 00109 * d_Error_TransactionOpen is raised. 00110 * 00111 * SEE ALSO: commit(), abort() 00112 * ODMG : Compliant + Extension 00113 */ 00114 void begin( d_Database* iDb = 0 ); 00115 00116 /** 00117 * Commits the transaction. 00118 * 00119 * All database operations executed inside the transaction are committed 00120 * to the database permanently. 00121 * 00122 * If the transaction is not open, an exception d_Error_TransactionNotOpen 00123 * is raised. 00124 * 00125 * If an error occurs while the transaction commits, the transaction will 00126 * rollback (calling abort()) leaving the database consistent. 00127 * 00128 * After a commit, the persistent objects created in the transaction are 00129 * freed and the locks acquired are released. All objects loaded in the 00130 * transaction could be removed by local cache (see d_Object::pin(), 00131 * d_Object::unpin()). 00132 * 00133 * After this operation, all d_Ref and d_Ref_Any objects used in the 00134 * transaction will be invalid. 00135 * 00136 * SEE ALSO: checkpoint(), abort() 00137 * ODMG : Compliant 00138 */ 00139 void commit(); 00140 00141 /** 00142 * Aborts the transaction. 00143 * 00144 * All database operations executed inside the transaction are aborted, 00145 * thus none of that operations take effect. If the transaction is not 00146 * open, an exception d_Error_TransactionNotOpen is raised. 00147 * 00148 * After an abort the persistent objects created in the transaction are 00149 * freed and the locks acquired are released. All objects loaded in the 00150 * transaction could be removed by local cache (see d_Object::pin(), 00151 * d_Object::unpin()). 00152 * 00153 * After this operation, all d_Ref and d_Ref_Any objects used in the 00154 * transaction will be invalid. 00155 * 00156 * SEE ALSO: commit(), ~d_Transaction() 00157 * ODMG : Compliant 00158 */ 00159 void abort(); 00160 00161 /** 00162 * Commits the persistent objects in the transaction. 00163 * 00164 * All database operations executed inside the transaction are committed 00165 * to the database permanently. 00166 * 00167 * If the transaction is not open, an exception d_Error_TransactionNotOpen 00168 * is raised. 00169 * 00170 * If an error occurs while the transaction commits, the transaction will 00171 * rollback (calling abort()) leaving the database consistent. 00172 * 00173 * After a checkpoint, the persistent objects created in the transaction 00174 * are usable and the locks acquired are maintained. 00175 * 00176 * After this operation, all d_Ref and d_Ref_Any objects used in the 00177 * transaction remain valid. 00178 * 00179 * SEE ALSO: commit(), abort() 00180 * ODMG : Compliant 00181 */ 00182 void checkpoint(); 00183 00184 00185 /** 00186 * Returns true if the transaction is currently active (open by begin() 00187 * method), otherwise false. 00188 * 00189 * ODMG : Compliant 00190 */ 00191 d_Boolean is_active () const; 00192 00193 00194 /** 00195 * Returns the current active transaction for the current thread. 00196 * 00197 * If the transactions are nested, returns the most nested transaction. 00198 * 00199 * ODMG : Compliant 00200 */ 00201 static d_Transaction* current(); 00202 00203 00204 /** 00205 * Return status of transaction (see d_Transaction::statuses) 00206 */ 00207 int getStatus() const; 00208 00209 // Internal. 00210 void flushLocalObjects(); 00211 00212 // Internal. 00213 void createObject( d_Object* iObject ); 00214 // Internal. 00215 void loadObject (d_Object* iObj, oObjectId& iOid); 00216 // Internal. 00217 void updateObject( d_Object* iObj ); 00218 // Internal. 00219 void reserveObject( d_Object* iObject ); 00220 // Internal. 00221 void deleteObject( d_Object* iObj, oObjectId& iOid ); 00222 // Internal. 00223 void releaseObject( oObjectId& iOid ); 00224 00225 // Internal. 00226 virtual bool isTxAble() const; 00227 00228 //-------------------------------------------------------------------------- 00229 // CONSTANTS: 00230 //-------------------------------------------------------------------------- 00231 typedef enum{ AVAIL, BEGUN, COMMITTING, ABORTING, CHECKPOINTING } statuses; 00232 00233 private: 00234 void close(); 00235 //-------------------------------------------------------------------------- 00236 // ATTRIBUTES: 00237 //-------------------------------------------------------------------------- 00238 oTxObjStreamManager streamMgr; 00239 statuses status; 00240 00241 oVector<oObjectStream*>::iterator itObjectToRemove; 00242 oVector<oObjectStream*> objectToRemove; 00243 }; 00244 //------------------------------------------------------------------------------ 00245 /** 00246 * Returns always true since d_Transaction class can handle transaction. 00247 */ 00248 inline bool d_Transaction::isTxAble() const 00249 { 00250 return true; 00251 } 00252 //------------------------------------------------------------------------------ 00253 inline d_Boolean d_Transaction::is_active () const 00254 { 00255 return status == BEGUN ? true : false; 00256 } 00257 //------------------------------------------------------------------------------ 00258 inline int d_Transaction::getStatus() const 00259 { 00260 return status; 00261 } 00262 00263 #endif 00264
1.3-rc1