.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

diference between TransactionManager and Transaction

5 REPLIES 5
Reply
Message 1 of 6
netcai
348 Views, 5 Replies

diference between TransactionManager and Transaction

I am confused by samples in autocad and AutoCAD [1].NET labs. In autocad samples: the code is :
TransactionManager tm = db.TransactionManager;
Transaction trans = tm.StartTransaction(); // begin the transaction
try
{
...
newBtr.AppendEntity(circle); //Append our entities...
newBtr.AppendEntity(attDef);
newBtr.AppendEntity(ellipse);
tm.AddNewlyCreatedDBObject(circle, true); //Again, let the transaction know about our newly added entities.
tm.AddNewlyCreatedDBObject(attDef, true);
tm.AddNewlyCreatedDBObject(ellipse, true);
}
trans.Commit(); //All done, no errors? Go ahead and commit!
}
...


but in AutoCAD [1].NET labs,the code is :

TransactionManager tm = db.TransactionManager;
Transaction trans = tm.StartTransaction(); // begin the transaction
try
{
...

newBtr.AppendEntity(circle); //Append our entities...
newBtr.AppendEntity(attDef);
newBtr.AppendEntity(ellipse);
trans.AddNewlyCreatedDBObject(circle, true); //Again, let the transaction know about our newly added entities.
trans.AddNewlyCreatedDBObject(attDef, true);
trans.AddNewlyCreatedDBObject(ellipse, true);
}
trans.Commit(); //All done, no errors? Go ahead and commit!
}
...
5 REPLIES 5
Message 2 of 6
Anonymous
in reply to: netcai

There's no difference. This (somewhat confusing) duality in the .NET API is
due to the same duality in the underlying C++ API (granted, not a very good
reason). Using the transaction related methods on the TransactionManager is
equivalent of calling the same methods on the "top" transaction.

Albert

wrote in message news:4879129@discussion.autodesk.com...
I am confused by samples in autocad and AutoCAD [1].NET labs. In autocad
samples: the code is :
TransactionManager tm = db.TransactionManager;
Transaction trans = tm.StartTransaction(); // begin the
transaction
try
{
...
newBtr.AppendEntity(circle); //Append our entities...
newBtr.AppendEntity(attDef);
newBtr.AppendEntity(ellipse);
tm.AddNewlyCreatedDBObject(circle, true); //Again, let the transaction know
about our newly added entities.
tm.AddNewlyCreatedDBObject(attDef, true);
tm.AddNewlyCreatedDBObject(ellipse, true);
}
trans.Commit(); //All done, no errors? Go ahead and commit!
}
...


but in AutoCAD [1].NET labs,the code is :

TransactionManager tm = db.TransactionManager;
Transaction trans = tm.StartTransaction(); // begin the
transaction
try
{
...

newBtr.AppendEntity(circle); //Append our entities...
newBtr.AppendEntity(attDef);
newBtr.AppendEntity(ellipse);
trans.AddNewlyCreatedDBObject(circle, true); //Again, let the transaction
know about our newly added entities.
trans.AddNewlyCreatedDBObject(attDef, true);
trans.AddNewlyCreatedDBObject(ellipse, true);
}
trans.Commit(); //All done, no errors? Go ahead and commit!
}
...
Message 3 of 6
netcai
in reply to: netcai

thanks albert.
Since there is no difference between TransactionManager and Transaction. in the following two methods ,Could you point out which one is better.

//method 1

public static void MainApp ( )
{
Database db = HostApplicationServices.WorkingDatabase;
TransactionManager tm = db.TransactionManager;
Transaction trans = tm.StartTransaction();
try
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "S_Cols_Pat1";
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 251);
AddLayer(ltr, trans);
trans.Commit();
}
finally { trans.Dispose(); }
}
public static ObjectId AddLayer( LayerTableRecord ltr , Transaction trans)
{
ObjectId retId = ObjectId.Null;
LayerTable lt = trans.GetObject(Db.LayerTableId, OpenMode.ForWrite, false) as LayerTable;
if (!lt.Has(ltr.Name))
{
retId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
}
else
{
retId = lt[ltr.Name];
}
return retId;
}



//method 2

public static void MainApp()
{
Database db = HostApplicationServices.WorkingDatabase;
TransactionManager tm = db.TransactionManager;
Transaction trans = tm.StartTransaction();
try
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "S_Cols_Pat1";
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 251);
AddLayer(ltr);
trans.Commit();
}
finally { trans.Dispose(); }
}
public static ObjectId AddLayer( LayerTableRecord ltr )
{
Database db = HostApplicationServices.WorkingDatabase;
TransactionManager tm = db.TransactionManager;
ObjectId retId = ObjectId.Null;
LayerTable lt = tm.GetObject(Db.LayerTableId, OpenMode.ForWrite, false) as LayerTable;
if (!lt.Has(ltr.Name))
{
retId = lt.Add(ltr);
tm.AddNewlyCreatedDBObject(ltr, true);
}
else
{
retId = lt[ltr.Name];
}
return retId;
}
Message 4 of 6
Anonymous
in reply to: netcai

Like I said, technically they are equivalent. I prefer method 1 because it
is clearer there what's happening.

Albert
wrote in message news:4880032@discussion.autodesk.com...
thanks albert.
Since there is no difference between TransactionManager and Transaction. in
the following two methods ,Could you point out which one is better.

//method 1

public static void MainApp ( )
{
Database db = HostApplicationServices.WorkingDatabase;
TransactionManager tm = db.TransactionManager;
Transaction trans = tm.StartTransaction();
try
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "S_Cols_Pat1";
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 251);
AddLayer(ltr, trans);
trans.Commit();
}
finally { trans.Dispose(); }
}
public static ObjectId AddLayer( LayerTableRecord ltr , Transaction
trans)
{
ObjectId retId = ObjectId.Null;
LayerTable lt = trans.GetObject(Db.LayerTableId,
OpenMode.ForWrite, false) as LayerTable;
if (!lt.Has(ltr.Name))
{
retId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
}
else
{
retId = lt[ltr.Name];
}
return retId;
}



//method 2

public static void MainApp()
{
Database db = HostApplicationServices.WorkingDatabase;
TransactionManager tm = db.TransactionManager;
Transaction trans = tm.StartTransaction();
try
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "S_Cols_Pat1";
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 251);
AddLayer(ltr);
trans.Commit();
}
finally { trans.Dispose(); }
}
public static ObjectId AddLayer( LayerTableRecord ltr )
{
Database db = HostApplicationServices.WorkingDatabase;
TransactionManager tm = db.TransactionManager;
ObjectId retId = ObjectId.Null;
LayerTable lt = tm.GetObject(Db.LayerTableId, OpenMode.ForWrite,
false) as LayerTable;
if (!lt.Has(ltr.Name))
{
retId = lt.Add(ltr);
tm.AddNewlyCreatedDBObject(ltr, true);
}
else
{
retId = lt[ltr.Name];
}
return retId;
}
Message 5 of 6
Anonymous
in reply to: netcai

Since Autodesk is telling us that we need to go through
transactions to open objects, the TransactionManager's
methods that operate on the top transaction become
more important, because there can be many cases where
objects need to be opened for read, to examine them and
then closed immediately. Encapsulating something like
that into a library function would otherwise require it to
start and end a transaction just to open one object.

With the transaction manager, a library function that opens
an object can use the top transaction (if there is one) to do
it without having to incurr the overhead of starting/ending a
transaction

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

wrote in message news:4880032@discussion.autodesk.com...
thanks albert.
Since there is no difference between TransactionManager and Transaction. in the following two methods ,Could you point out which
one is better.

//method 1

public static void MainApp ( )
{
Database db = HostApplicationServices.WorkingDatabase;
TransactionManager tm = db.TransactionManager;
Transaction trans = tm.StartTransaction();
try
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "S_Cols_Pat1";
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 251);
AddLayer(ltr, trans);
trans.Commit();
}
finally { trans.Dispose(); }
}
public static ObjectId AddLayer( LayerTableRecord ltr , Transaction trans)
{
ObjectId retId = ObjectId.Null;
LayerTable lt = trans.GetObject(Db.LayerTableId, OpenMode.ForWrite, false) as LayerTable;
if (!lt.Has(ltr.Name))
{
retId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
}
else
{
retId = lt[ltr.Name];
}
return retId;
}



//method 2

public static void MainApp()
{
Database db = HostApplicationServices.WorkingDatabase;
TransactionManager tm = db.TransactionManager;
Transaction trans = tm.StartTransaction();
try
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "S_Cols_Pat1";
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 251);
AddLayer(ltr);
trans.Commit();
}
finally { trans.Dispose(); }
}
public static ObjectId AddLayer( LayerTableRecord ltr )
{
Database db = HostApplicationServices.WorkingDatabase;
TransactionManager tm = db.TransactionManager;
ObjectId retId = ObjectId.Null;
LayerTable lt = tm.GetObject(Db.LayerTableId, OpenMode.ForWrite, false) as LayerTable;
if (!lt.Has(ltr.Name))
{
retId = lt.Add(ltr);
tm.AddNewlyCreatedDBObject(ltr, true);
}
else
{
retId = lt[ltr.Name];
}
return retId;
}
Message 6 of 6
Anonymous
in reply to: netcai

Library functions could also have an existing transaction passed to them.

Dan

"Tony Tanzillo" wrote in message
news:4880097@discussion.autodesk.com...
Since Autodesk is telling us that we need to go through
transactions to open objects, the TransactionManager's
methods that operate on the top transaction become
more important, because there can be many cases where
objects need to be opened for read, to examine them and
then closed immediately. Encapsulating something like
that into a library function would otherwise require it to
start and end a transaction just to open one object.

With the transaction manager, a library function that opens
an object can use the top transaction (if there is one) to do
it without having to incurr the overhead of starting/ending a
transaction

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

wrote in message news:4880032@discussion.autodesk.com...
thanks albert.
Since there is no difference between TransactionManager and Transaction. in
the following two methods ,Could you point out which
one is better.

//method 1

public static void MainApp ( )
{
Database db = HostApplicationServices.WorkingDatabase;
TransactionManager tm = db.TransactionManager;
Transaction trans = tm.StartTransaction();
try
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "S_Cols_Pat1";
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 251);
AddLayer(ltr, trans);
trans.Commit();
}
finally { trans.Dispose(); }
}
public static ObjectId AddLayer( LayerTableRecord ltr , Transaction
trans)
{
ObjectId retId = ObjectId.Null;
LayerTable lt = trans.GetObject(Db.LayerTableId,
OpenMode.ForWrite, false) as LayerTable;
if (!lt.Has(ltr.Name))
{
retId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
}
else
{
retId = lt[ltr.Name];
}
return retId;
}



//method 2

public static void MainApp()
{
Database db = HostApplicationServices.WorkingDatabase;
TransactionManager tm = db.TransactionManager;
Transaction trans = tm.StartTransaction();
try
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "S_Cols_Pat1";
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 251);
AddLayer(ltr);
trans.Commit();
}
finally { trans.Dispose(); }
}
public static ObjectId AddLayer( LayerTableRecord ltr )
{
Database db = HostApplicationServices.WorkingDatabase;
TransactionManager tm = db.TransactionManager;
ObjectId retId = ObjectId.Null;
LayerTable lt = tm.GetObject(Db.LayerTableId, OpenMode.ForWrite,
false) as LayerTable;
if (!lt.Has(ltr.Name))
{
retId = lt.Add(ltr);
tm.AddNewlyCreatedDBObject(ltr, true);
}
else
{
retId = lt[ltr.Name];
}
return retId;
}

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost