.NET

.NET

Reply
Valued Contributor
netcai
Posts: 66
Registered: ‎07-22-2004
Message 1 of 6 (146 Views)

diference between TransactionManager and Transaction

146 Views, 5 Replies
06-19-2005 10:17 PM
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!
}
...
*Albert Szilvasy
Message 2 of 6 (146 Views)

Re: diference between TransactionManager and Transaction

06-20-2005 11:06 AM 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!
}
...
Valued Contributor
netcai
Posts: 66
Registered: ‎07-22-2004
Message 3 of 6 (146 Views)

Re: diference between TransactionManager and Transaction

06-20-2005 05:26 PM 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;
}
*Albert Szilvasy
Message 4 of 6 (146 Views)

Re: diference between TransactionManager and Transaction

06-20-2005 07:03 PM 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;
}
*Tony Tanzillo
Message 5 of 6 (146 Views)

Re: diference between TransactionManager and Transaction

06-20-2005 07:20 PM 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;
}
*J. Daniel Smith
Message 6 of 6 (146 Views)

Re: diference between TransactionManager and Transaction

06-20-2005 07:27 PM 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;
}
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.