473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How do I implement class finalization?

Hello everyone,
I have a abstract class called DalBase and in that class I have a couple of
methods. One of the method that I have is called BeginTran() and this class
initializes a private Transaction object. I also have a method called
CommitTran() that disposes the private Transaction object, thus setting it
to null. Now the issue is, if the user calls BeginTran, I have to make sure
he/she calls CommitTran() so that the private transaction object gets set to
null. The only way I can think of is using C# distructor.

So I have coded

Expand|Select|Wrap|Line Numbers
  1. ~DalBase()
  2. {
  3. if (this._transaction != null)
  4. {
  5. throw (new Exception("You have opened a transaction that needs to be
  6. closed."));
  7. }
  8. }
  9.  
Now I have checked when I run the application, and call BeginTran() method
and intentionally not call CommitTran(), the above code does not get
executed. But if I set a break point at the above code, then it gets
executed. This is probably something to do with garbage collection. But how
do I solve the problem I am having. A short example would be very helpful.
Thanx alot
Nov 16 '05 #1
2 2733
For scenarios like these you're probably better off using a
ServicedComponent inside COM+ (nee EnterpriseServices). You can rely on the
Activate and Deactivate methods being invoked by COM+ and you can then
release your resources.

The other way is to force your clients to call Dispose() on instances of
types derived from your base class.

--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/
"JollyK" <Jo****@email.com> wrote in message
news:eU**************@TK2MSFTNGP09.phx.gbl...
Hello everyone,
I have a abstract class called DalBase and in that class I have a couple of methods. One of the method that I have is called BeginTran() and this class initializes a private Transaction object. I also have a method called
CommitTran() that disposes the private Transaction object, thus setting it
to null. Now the issue is, if the user calls BeginTran, I have to make sure he/she calls CommitTran() so that the private transaction object gets set to null. The only way I can think of is using C# distructor.

So I have coded

Expand|Select|Wrap|Line Numbers
  1.  ~DalBase()
  2.  {
  3.   if (this._transaction != null)
  4.   {
  5.      throw (new Exception("You have opened a transaction that needs to be
  6.  closed."));
  7.    }
  8.  }
  9.  

Now I have checked when I run the application, and call BeginTran() method
and intentionally not call CommitTran(), the above code does not get
executed. But if I set a break point at the above code, then it gets
executed. This is probably something to do with garbage collection. But how do I solve the problem I am having. A short example would be very helpful.
Thanx alot

Nov 16 '05 #2
I have a vague memory that the finalizers aren't guaranteed to be run when a
program exits, but I'm willing to be corrected about that.

You could try putting GC.Collect() and GC.WaitForPendingFinalizers() near
the very end of your program. I wouldn't put them anywhere else in your
code, except perhaps for debugging, as they have obvious performance
penalties.

You might want to consider a slightly different pattern for your transaction
class:

class Transaction : IDisposable
{
protected Transaction()
{
_realTransaction = new RealTransaction();

_realTransaction.BeginTran();
}

~Transaction()
{
Debug.Assert("not disposed correctly");
}

public void Dispose()
{
if(_realTransaction != null)
{
_realTransaction.Rollback();
}
GC.SuppressFinalize(this);
}

public void Commit()
{
Debug.Assert(_realTransaction != null, "already commited or
disposed");

_realTransaction.Commit();
_realTransaction = null;
}

private _realTransaction;
}

and then you can use them like:

using(Transaction transaction = someFactory.CreateTransaction())
{
// Do some stuff here

// Do we want to commit
if(itsAllOK)
{
transaction.Commit();
}

} // The real transaction will either be rolled back or committed.

Of course there are several variants (eg not storing the real transaction
object but using abstract protected methods instead, with the actual
transaction processing carried out by the derived class).

It's a pattern I've found useful, hope it helps you too.

Stu
"JollyK" <Jo****@email.com> wrote in message
news:eU**************@TK2MSFTNGP09.phx.gbl...
Hello everyone,
I have a abstract class called DalBase and in that class I have a couple of methods. One of the method that I have is called BeginTran() and this class initializes a private Transaction object. I also have a method called
CommitTran() that disposes the private Transaction object, thus setting it
to null. Now the issue is, if the user calls BeginTran, I have to make sure he/she calls CommitTran() so that the private transaction object gets set to null. The only way I can think of is using C# distructor.

So I have coded

Expand|Select|Wrap|Line Numbers
  1.  ~DalBase()
  2.  {
  3.   if (this._transaction != null)
  4.   {
  5.      throw (new Exception("You have opened a transaction that needs to be
  6.  closed."));
  7.    }
  8.  }
  9.  

Now I have checked when I run the application, and call BeginTran() method
and intentionally not call CommitTran(), the above code does not get
executed. But if I set a break point at the above code, then it gets
executed. This is probably something to do with garbage collection. But how do I solve the problem I am having. A short example would be very helpful.
Thanx alot

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tobbi | last post by:
Hi, I have a question about Python object finalization. I would like to be notified when an Object is deallocated. The problem is I would like to do it in C. The only way to register a finalizer I...
11
by: Saumya | last post by:
Hi, I didn't know how else to express myself in the subject line, but what I want to know is this: <b>I have a class A, so what do I have to do to ensure that no one can derive from it?</b> ...
822
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical...
1
by: JollyK | last post by:
Hello everyone, I have a abstract class called DalBase and in that class I have a couple of methods. One of the method that I have is called BeginTran() and this class initializes a private...
5
by: pronto | last post by:
Hello All. I've got some unexpected (to me) behavior. In C++, destructor of the class that wasn't created is not called, in C# some how I've got destructor call for class that wasn't initialized...
7
by: Razzie | last post by:
Hello all, This may seem like a silly question but I suddenly wondered about this. Is there any difference between the following 2 situations? 1) class A { private SomeObject o = new...
2
by: Mika M | last post by:
Hi! My application uses self made Class. When I create an instance of it, class creates temporary file in constructor, and this file is in use as long as instance of class is in use. After...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
4
by: lord trousers | last post by:
I'm implementing a garbage collector in C++ for a fun new language (don't ask), and I've found that it spends a good amount of time calling "finalize" on objects that are freed/not relocated. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.