You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The TransactionManager class manages the lifecycle of transactions for a specific database. Each Database object has its own TransactionManager accessible via Database.TransactionManager. It controls transaction creation, nesting, and coordination.
[CommandMethod("MONITORTRANS")]publicvoidMonitorTransactionsExample(){Documentdoc=Application.DocumentManager.MdiActiveDocument;Databasedb=doc.Database;Editored=doc.Editor;TransactionManagertm=db.TransactionManager;// Subscribe to eventstm.TransactionStarted+=(sender,e)=>ed.WriteMessage("\n>>> Transaction STARTED");tm.TransactionCommitted+=(sender,e)=>ed.WriteMessage("\n>>> Transaction COMMITTED");tm.TransactionAborted+=(sender,e)=>ed.WriteMessage("\n>>> Transaction ABORTED");// Perform some operationsusing(Transactiontr=tm.StartTransaction()){BlockTablebt=tr.GetObject(db.BlockTableId,OpenMode.ForRead)asBlockTable;ed.WriteMessage($"\n>>> Working with {bt.Count} blocks");tr.Commit();}// Cleanup (important!)// In production, store event handlers and unsubscribe when done}
[CommandMethod("GRAPHICSFLUSH")]publicvoidGraphicsFlushExample(){Documentdoc=Application.DocumentManager.MdiActiveDocument;Databasedb=doc.Database;TransactionManagertm=db.TransactionManager;// Disable automatic graphics updates for performancetm.EnableGraphicsFlush(false);using(Transactiontr=tm.StartTransaction()){BlockTablebt=tr.GetObject(db.BlockTableId,OpenMode.ForRead)asBlockTable;BlockTableRecordbtr=tr.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite)asBlockTableRecord;// Create many objects without updating graphicsfor(inti=0;i<1000;i++){Circlecircle=newCircle(newPoint3d(i*2,0,0),Vector3d.ZAxis,1.0);btr.AppendEntity(circle);tr.AddNewlyCreatedDBObject(circle,true);}tr.Commit();}// Re-enable and force updatetm.EnableGraphicsFlush(true);tm.FlushGraphics();doc.Editor.WriteMessage("\nCreated 1000 circles with deferred graphics update");}
5. Multi-Document Transaction Management
[CommandMethod("MULTIDOC")]publicvoidMultiDocumentExample(){DocumentCollectiondocs=Application.DocumentManager;Editored=docs.MdiActiveDocument.Editor;// Each document has its own transaction managerforeach(Documentdocindocs){Databasedb=doc.Database;TransactionManagertm=db.TransactionManager;ed.WriteMessage($"\nDocument: {doc.Name}");ed.WriteMessage($"\n Active transactions: {tm.NumberOfActiveTransactions}");}}
6. Safe Transaction Pattern with Manager
[CommandMethod("SAFETM")]publicvoidSafeTransactionManagerPattern(){Documentdoc=Application.DocumentManager.MdiActiveDocument;Databasedb=doc.Database;Editored=doc.Editor;TransactionManagertm=db.TransactionManager;// Check if transaction is already active (e.g., called from another command)if(tm.NumberOfActiveTransactions>0){ed.WriteMessage("\nWarning: Transaction already active!");ed.WriteMessage($"\nActive count: {tm.NumberOfActiveTransactions}");}using(Transactiontr=tm.StartTransaction()){try{// Your database operations hereBlockTablebt=tr.GetObject(db.BlockTableId,OpenMode.ForRead)asBlockTable;ed.WriteMessage($"\nProcessing {bt.Count} blocks");tr.Commit();}catch(System.Exceptionex){ed.WriteMessage($"\nError: {ex.Message}");// Transaction will auto-abort}}}
Transaction Lifecycle
graph TD
A[TransactionManager.StartTransaction] --> B[Transaction Created]
B --> C{Operations Successful?}
C -->|Yes| D[Transaction.Commit]
C -->|No| E[Transaction.Abort]
D --> F[TransactionCommitted Event]
E --> G[TransactionAborted Event]
F --> H[Transaction.Dispose]
G --> H
H --> I[TransactionEnded Event]
Loading
Best Practices
One transaction manager per database: Never share transaction managers across databases
Monitor active count: Check NumberOfActiveTransactions to avoid conflicts
Use events for debugging: Subscribe to transaction events during development
Disable graphics flush for bulk operations: Improves performance significantly
Always flush graphics after disabling: Ensure UI updates after bulk operations
Unsubscribe from events: Prevent memory leaks by removing event handlers
Performance Considerations
Scenario
Recommendation
Creating 1-10 objects
Use default graphics flush
Creating 100+ objects
Disable graphics flush, flush manually after commit