File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Is this a good way of creating counter? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is this a good way of creating counter?" Watch "Is this a good way of creating counter?" New topic
Author

Is this a good way of creating counter?

Beksy Kurian
Ranch Hand

Joined: Jul 11, 2001
Posts: 254
Is using a database sequence(Oracle), a good way to create counter in a jsp page. So far it looks like it is working. any disadvantages?
Beksy
[This message has been edited by Beksy Kurian (edited October 16, 2001).]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Offhand, it's an expensive thing to obtain a connection, query the sequence, and disconnect, all for the sake of a page count.

*on the other hand*... how else are these things done, right?

One thing I noticed about Oracle sequences, which I hadn't realized until I had performed testing over several days time... If the sequence has a buffer, and it is not queried for some amount of time (which I haven't been able to figure out, or see where it is set)... the sequence looses the buffered numbers.

I used a sequence for 'next primary key' generation.. I was noticing the following.. one day when i did my tests, I'd have 100,101,102,103,etc...
Several hours later it would be 104,105,106,etc...

But on the next day, it would start at 120,121,122,123...
and the next day would be 140,141,142.

The sequence buffered 20 'next numbers'... but if they're not used in time, they're gone!

Now you *could* set it not to buffer any, but does anyone know how to tell Oracle to simply *not* dump its buffer. My books don't show me the magical 'timeout' setting.
Beksy Kurian
Ranch Hand

Joined: Jul 11, 2001
Posts: 254
If we set to 'no cache' will that help?
Beksy
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Now you *could* set it not to buffer any, but does anyone know how to tell Oracle to simply *not* dump its buffer. My books don't show me the magical 'timeout' setting.

Would setting the buffer size to one do it? No idea.
I agree with everything Mike had to say, but for a rough and ready age count you could use an insance variable in the servlet
as described in this thread
Dave.
Beksy Kurian
Ranch Hand

Joined: Jul 11, 2001
Posts: 254
thanks you David and Mike
Beksy
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Just as a final note:

the word i was looking for was 'CACHE'
so:
CREATE SEQUENCE seqName INCREMENT BY 1 START WITH 1000 CACHE 20;

will give me a single-increment sequence, starting at 1000.

BUT.. the CACHE is not persistent over time, and so if I don't grab the next one 'in time', my next number will be some number that is startNumber + (increment * n) where n > 1 (It flushes all the cached numbers that are not used, and gets the next block of 'increment' numbers).

I'm not sure of how to control this cache-flushing behaviour. I agree that if this is really bad for your app, simply specify NO-CACHE. But what if you want the cache for the performance increase, but don't want the thing to expire?

Edited to add this:
I've asked an Oracle guru here at work. He tells me that each new session with the database results in the sequence being cycled.
[This message has been edited by Mike Curwen (edited October 17, 2001).]
Syam Veerakumar
Ranch Hand

Joined: Sep 20, 2001
Posts: 49
Oracle Sequence is meant for producing Unique numbers rather serial numbers,coz this depends on the oracle user session, u'll get different numbers for a purticular sequence if u open multiple session for a same user.So better insert that sequence values to a column and get the row count which will equal to the number of hits.But to tell u this is quite costly.


A Quitter Never Wins<br />A Winner Never Quits
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is this a good way of creating counter?