• Welcome to KonaKart Community Forum. Please login or sign up.
 

KKCustomEng.finalize()

Started by unic1988, February 06, 2009, 12:01:21 pm

Previous topic - Next topic

unic1988

Hello everyone,

I need to do some things exactly when the users session expires, so I thought that should be easy via KKCustomEng.finalize(), but I can't get this method get called when the session expires.

What I have done in KKCustomEng.java:

protected void finalize() throws Throwable {

    mylog.debug("session closed");

    try {

//do something with some data that I've gathered during this session
    }
    finally {

super.finalize();
    }
}


I have also set konakart.session.durationMinutes=2, so I think the session should expire very fast (2 minutes).

So is there someone who can call me if I did something wrong, or if it won't work this way?

unic1988

After some testing it looks like KKCustomEng.finalize() never gets invoked. Is that right, because I thought the Garbage collector invokes that method once for every Object  ??? I am a bit confused.

julie

The KonaKart session information is stored in the database. In order to do something as soon as it has been detected that the session has expired, you should customize the checkSession() API call.

unic1988

First: Thank you for your fast answer,

let me explain you my problem a little more detailed:

Everytime the user gets a product displayed I want to save what product he looks at, the customers-id and the price of the product. I am saving this Information in an little class that I've written called ProductHistoryItem and save it as a Datalement in the KKCustomEng:

KKCustomEng.java
...

//Hashtable<Integer productId, ProductHistoryItem productInformation>

java.util.Hashtable<Integer, ProductHistoryItem> phItems = new Hashtable<Integer, ProductHistoryItem>();

...


Now everytime the Customer gets a product displayed I write that to this Hashtable

...
phItems.put(product.getId(), new ProductHistoryItem(... /* product information here */));
...


This all works fine.

Now everytime the customer orders a product I want to note that:

...
phItems.get(orderproduct.getProductId()).setOrdered(true);
...


(setOrdered is a method of class ProductHistoryItem)

Now all information that i need to save is in my Hashtable, but I want to write all this stuff into a custom table in the Database, but there are two problems:

1. befor I write something about a product to the database I have to wait if the customer orders this product, so I have to wait until the customer finished shopping, what is when the session expires. This is because I have many entrys per product, so I cant easily identify which of the entrys is the one that belongs to the actual ordered product.

2. It wouldn't be nice for performance if I do a SQL query everytime the user looks at a product, so thats another cause to do all the database stuff in one step at the moment the session expires.

Now it seemes, that the finalize() method is never called, why is this? I thought the finalize() method has to be called everytime the garbage collector destroyes an object.

So far what my problem is.


You say that I could use the checkSession() method, but I need to know when this method gets called? Does this method return a special value at the moment the session expires? I saw that it returns the customers Id, but that doesn't help me anyway, because I think, that this Id only is valid if the customer is logged in, but my Hashtable exists even if the customer is not logged in, what would lead me to a situation where I would save the products history data befor I really know if the user orders this product during this session or not.

unic1988

I tried it another way using javax.servlet.http.HttpSessionBindingListener.

My KKCustomEng now implements this interface (public void valueBound(HttpSessionBindingEvent e), public void valueUnbound(HttpSessionBindingEvent event) ) and I am adding the KKCustomEng explicitly as an Attribute to the session. I thought that should solve the problem, but it doesn't. valueBound() gets called correctly when the KKCustomEng is added to the session, but valueUnbound() doesn't and I have no Idea why. Is this a Bug in javax.servlet.http.HttpSessionBindingListener or is it a special behaviour of Konakart?

Thanks in advance.

unic1988

Edit: It works sometimes, but I have no Idea what it depends on.

trevor

QuoteIs this a Bug in javax.servlet.http.HttpSessionBindingListener or is it a special behaviour of Konakart?


It's not a special behavior of KonaKart. Maybe you aren't waiting long enough? Remember that this session duration is a configuration of the servlet engine and not of KonaKart.

unic1988

Well that could be the point. I have set Session duration in Konakart properties to 1 minute and after 1 minute I I was logged off automatically, so I thought that that is the moment the session expries. I am sorry that I have to ask such stupid questions, but I just don't know it better.

btw. I found out, that valueUnbound get called correctly if I restart the Server, so it seems you are right. I will take a look how long a session is stored in Tomcat 6.

Thank you.