Nov
19
Creating a new Key in WCS
Posted by | Posted in WebSphere Commerce | Posted on 19-11-2008
Tagged Under : WCS, WebSphere, WebSphere Commerce, WebSphere Commerce Server
Creating a new Key in WCS
In WebSphere Commerce, tables that need a unique primary key, the keys are not auto generated. They are coming from the KEYS table. The WCS table KEYS defines the range of key values for all tables that require a unique key. So always new key generated will be using values of KEYS table.
Any time if you need this value programmatically, you can get it using ECKeyManager class. So the code for getting the next keys table for a table is
com.ibm.commerce.key.ECKeyManager keyMgr = com.ibm.commerce.key.ECKeyManager.singleton(); Long key = keyMgr.getNextKey("tablename"); |
Where tablename is your table’s name. Table name is case sensitive. If your primary key is defined as long data type use getNextKeyAsLong instead of getNextKey method.
In order to work the above code, we need to have an entry in the KEYS table.



Thanks. I was looking for something like this.
Thanks.
Very interesting but just a question: I create the new id for my table and the COUNTER fileld in KEYS table is incremented. If I get an exception in inserting the new row in my table, is COUNTER value rollbacked too?
I tested this scenario and I found that the COUNTER value was not rolled back. It could be because of the way I tested. I used a controller command directly for fetching the data. Immediately after fetching I threw an exception.
I thnk that we’ll implement something outside WCS: we must (it’s an italian law) have a numeric id with no spaces. I mean: 1 -2 -3 it’s ok; 1 – 5 – 6 – 7 it’s ko.