We demonstrated long time ago that committing transactions performs better than not committing for reading purposes as far as the currently active database (associated with the current document) is of concern. It was a bit surprise to us since it was a common practice not to commit transactions for reading operations, but it was not a big deal. We confirmed another good practice anyway, though not common.
We also demonstrated a while back that the document transaction manager for an external database is nowhere, so we have to use the database transaction manager always in this case. It was easy to understand since an external database is only loaded into memory and generally is not associated with the current document.
JeffH raised a good point recently about whether we’d better commit transactions as well for external databases. In this post, let’s address it. The following two methods/commands were created for this purpose.
[CommandMethod("CommitExtDBTransaction")]
public static void CommitExtDBTransaction_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
Document doc = MgdAcApplication.DocumentManager.MdiActiveDocument;
Database db = HostApplicationServices.WorkingDatabase;
try
{
AcadPerformanceMeter realTimer = new AcadPerformanceMeter(MgdAcApplication.DocumentManager.MdiActiveDocument.Editor, "CommitExtDBTransaction");
using (Database extDb = new Database(false, true))
{
extDb.ReadDwgFile("c:\\temp\\test1.dwg", FileShare.ReadWrite, false, "");
using (Transaction dbTr = extDb.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)dbTr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(extDb), OpenMode.ForRead);
foreach (ObjectId id in btr)
{
Entity ent = dbTr.GetObject(id, OpenMode.ForRead) as Entity;
for (int i = 0; i < 100; i++)
{
bool value = ent.ColorIndex == 1;
value = ent.Linetype == "DASHED";
value = ent.Layer == "TEST";
value = ent.LinetypeScale == 2.0;
value = ent.XData != null;
}
}
dbTr.Commit();
}
//extDb.SaveAs(extDb.Filename, DwgVersion.Current);
}
realTimer.CodeEnd();
}
catch (System.Exception ex)
{
MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(Environment.NewLine + ex.Message);
}
}
[CommandMethod("NotCommitExtDBTransaction")]
public static void NotCommitExtDBTransaction_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
Document doc = MgdAcApplication.DocumentManager.MdiActiveDocument;
Database db = HostApplicationServices.WorkingDatabase;
try
{
AcadPerformanceMeter realTimer = new AcadPerformanceMeter(MgdAcApplication.DocumentManager.MdiActiveDocument.Editor, "NotCommitExtDBTransaction");
using (Database extDb = new Database(false, true))
{
extDb.ReadDwgFile("c:\\temp\\test1.dwg", FileShare.ReadWrite, false, "");
using (Transaction dbTr = extDb.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)dbTr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(extDb), OpenMode.ForRead);
foreach (ObjectId id in btr)
{
Entity ent = dbTr.GetObject(id, OpenMode.ForRead) as Entity;
for (int i = 0; i < 100; i++)
{
bool value = ent.ColorIndex == 1;
value = ent.Linetype == "DASHED";
value = ent.Layer == "TEST";
value = ent.LinetypeScale == 2.0;
value = ent.XData != null;
}
}
//dbTr.Commit();
}
//extDb.SaveAs(extDb.Filename, DwgVersion.Current);
}
realTimer.CodeEnd();
}
catch (System.Exception ex)
{
MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(Environment.NewLine + ex.Message);
}
}
We ran the two commands in a new AutoCAD session with only the default drawing open and got the following output.
Command: COMMITEXTDBTRANSACTION
CommitExtDBTransaction in ms. CodeEnd:38297.7492 CmdEnd:38301.4761
RealEnd:38307.5159
Command: NOTCOMMITEXTDBTRANSACTION
NotCommitExtDBTransaction in ms. CodeEnd:42819.1134 CmdEnd:42819.2665
RealEnd:42819.4587
The two commands were run again in another AutoCAD session of the same version and the following similar results were got.
Command: NOTCOMMITEXTDBTRANSACTION
NotCommitExtDBTransaction in ms. CodeEnd:45529.0848 CmdEnd:45529.2135
RealEnd:45529.4053
Command: COMMITEXTDBTRANSACTION
CommitExtDBTransaction in ms. CodeEnd:42853.7209 CmdEnd:42853.8566
RealEnd:42854.0382
As can be seen, it still ran a bit quicker (about 10%) if the external database transaction is committed for reading operations than not. So, the Transaction.Abort() call (which is called implicitly by the database transaction either external or current if the Commit() is not made) does seem to do more time-consuming stuffs (necessary or not) than what the Transaction.Commit() method does in the same situation.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders and widgets to help program AutoCAD .NET addins.
Recent Comments