473,564 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2745
For scenarios like these you're probably better off using a
ServicedCompone nt inside COM+ (nee EnterpriseServi ces). 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.c om> wrote in message
news:eU******** ******@TK2MSFTN GP09.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.WaitForPendi ngFinalizers() 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()
{
_realTransactio n = new RealTransaction ();

_realTransactio n.BeginTran();
}

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

public void Dispose()
{
if(_realTransac tion != null)
{
_realTransactio n.Rollback();
}
GC.SuppressFina lize(this);
}

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

_realTransactio n.Commit();
_realTransactio n = null;
}

private _realTransactio n;
}

and then you can use them like:

using(Transacti on transaction = someFactory.Cre ateTransaction( ))
{
// Do some stuff here

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

} // 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.c om> wrote in message
news:eU******** ******@TK2MSFTN GP09.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
1298
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 found out is to create a weak reference object and give it a callback. But the callback must be a Python function not C. That seems to be too...
11
1599
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> Saumya
822
29100
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 real-time applications. The majority of expert/people recommend Ada for safety critical real-time applications. I've many years of experience in C/C++...
1
274
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 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...
5
1317
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 properly (by constructor). Please look examples. Is that a normal ? .. or garbage collector take care of all ? Am I doing something wrong ?
7
2511
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 SomeObject();
2
5489
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 instance of class will be removed, temporary needed file should be deleted too. So how can I do it? It there destructor for VB.NET class where this...
0
2814
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 magic, I wrote the following module. It is mainly useful as a light weight tool to help programmers catch mistakes at definition time (e.g.,...
4
1628
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 default implementation does nothing and always will, and most types won't need to override it. It'd be nice if I could test for overrides, but the...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.