473,698 Members | 2,434 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 2751
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
1302
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 difficult. Is there any (undocumented) way to do PyObject_RegisterFinalizer(PyObject *o,...
11
1611
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
29433
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++ (and Delphi) but no Ada knowledge. May I ask if it is too difficult to move from C/C++ to Ada?...
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 the user calls BeginTran, I have to make sure he/she calls CommitTran() so that the private...
5
1320
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
2515
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
5491
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 file can be deleted or something like that? I'm quite newbie with VB.NET OOP programming, but I have...
0
2828
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., forgetting to implement a method required by the given interface). This is handy when unit tests or...
4
1634
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 obvious thing doesn't work: class Object { // ... virtual void finalize() { }
0
8675
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9029
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7729
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6521
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.