Re: sequential numbering across more instances
Re: sequential numbering across more instances
- Subject: Re: sequential numbering across more instances
- From: Chuck Hill <email@hidden>
- Date: Fri, 15 Aug 2008 10:13:18 -0700
On Aug 15, 2008, at 8:03 AM, Ondřej Čada wrote:
Florijan,
On Aug 15, 2008, at 4:23 PM, Florijan Stamenkovic wrote:
I've just bumped into a need to maintain a sequential numbering of
database rows, which are created by more concurrent WO application
instances.
If you just need a unique identifier for a row, you could use the
primary key value easily,
I regret to say I have a very strong feeling against using PK's for
anything but PK and relationships, ever.
so I am guessing that is not what you need. So, could you elaborate
on this a bit?
Anyway, even if I wanted to use them, it would not do, for there are
items which have no numbers... Ha, thanks! Seems you just have led
me to one reason I haven't realised originally why UNIQUE would not
work quite well :) (Unless put into a table of its own, dedicated
for this task, which I at the moment have alas completely no idea
whether it's worth that or not... I think rather not?)
What kind of a sequence is it? Do the users need to provide values,
or can they be auto generated? In short, what are you trying to do?
There's an entity, say, Order, one of whose attributes is an integer
orderNumber.
Some of created orders have no number (test ones, etc.). Those which
have a number need to be numbered in a global sequence. In pseudo-
code, I need a method
class Order:EOGenericRecord { ... ... ...
void assignOrderNumber() {
// precondition: orderNumber()==null
...
...
// postcondition: orderNumber() is unique, so that all numbered
orders make a sequence 1,2,3,4,....
}
so that this method works properly from any number of application
instances.
You will have fun with this one.
How about this: have an entity OrderNumber with a PK (of course), and
a lastOrderNumber attribute? There will only ever be one row.
in Order:
void assignOrderNumber() {
OrderNumber orderNumber = OrderNumber.getOneAndOnly(editingContext());
setOrderNumber(orderNumber.nextOrderNumber():
}
In OrderNumber:
public Integer nextOrderNumber() {
setLastOrderNumber(new Integer(lastOrderNumber().intValue() + 1));
return lastOrderNumber();
}
NOW, the hard part! For every save you are going to have to trap
OptimisiticLocking exceptions and check if it failed on OrderNumer.
If it did, you are going to need to call order.assignOrderNumber()
again and repeat until the save succeeds. This will be problematic if
you have multiple Orders being saved in one transaction.
Hmm, and there is one other rather serious problem: If another order
saves first, it will update the snapshot for OrderNumber and you will
not get the needed OptimisiticLocking exception. You would need to
catch the Object Changed In Store notification and based on that
updating OrderNumber, invalidate and re-assign the number you had
previously generated.
Have Fun!
Chuck
--
Chuck Hill Senior Consultant / VP Development
Practical WebObjects - for developers who want to increase their
overall knowledge of WebObjects or who are trying to solve specific
problems.
http://www.global-village.net/products/practical_webobjects
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden