Commit a transaction
(&self, transaction_id: TransactionId)
| 189 | |
| 190 | /// Commit a transaction |
| 191 | pub fn commit_transaction(&self, transaction_id: TransactionId) -> Result<(), ExecutionError> { |
| 192 | let active_txns = self.active_transactions.read().map_err(|_| { |
| 193 | ExecutionError::RuntimeError("Failed to acquire transactions lock".to_string()) |
| 194 | })?; |
| 195 | |
| 196 | if let Some(txn_arc) = active_txns.get(&transaction_id) { |
| 197 | let mut transaction = txn_arc.lock().map_err(|_| { |
| 198 | ExecutionError::RuntimeError("Failed to acquire transaction lock".to_string()) |
| 199 | })?; |
| 200 | |
| 201 | if !transaction.is_active() { |
| 202 | return Err(ExecutionError::RuntimeError(format!( |
| 203 | "Transaction {} is not active", |
| 204 | transaction_id |
| 205 | ))); |
| 206 | } |
| 207 | |
| 208 | let final_sequence = transaction.get_sequence_number(); |
| 209 | let commit_description = format!( |
| 210 | "COMMIT TRANSACTION - final sequence number: {}", |
| 211 | final_sequence |
| 212 | ); |
| 213 | |
| 214 | // Write COMMIT entry to WAL |
| 215 | let wal_entry = WALEntry::new( |
| 216 | WALEntryType::Commit, |
| 217 | transaction_id, |
| 218 | self.wal.next_global_sequence(), |
| 219 | final_sequence, |
| 220 | None, |
| 221 | commit_description.clone(), |
| 222 | ); |
| 223 | |
| 224 | if let Err(e) = self.wal.write_entry(wal_entry) { |
| 225 | return Err(ExecutionError::RuntimeError(format!( |
| 226 | "Failed to write COMMIT to WAL: {}", |
| 227 | e |
| 228 | ))); |
| 229 | } |
| 230 | |
| 231 | // Also log to in-memory transaction log |
| 232 | transaction.add_operation(OperationType::Other, commit_description); |
| 233 | transaction.commit(); |
| 234 | |
| 235 | Ok(()) |
| 236 | } else { |
| 237 | Err(ExecutionError::RuntimeError(format!( |
| 238 | "Transaction {} not found", |
| 239 | transaction_id |
| 240 | ))) |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /// Rollback a transaction |
| 245 | pub fn rollback_transaction( |
no test coverage detected