Solved

How to explicitly lock a table on DB2

Posted on 2007-12-06
1
3,836 Views
Last Modified: 2012-06-21
I have a DB2-database. Most of the tables have a primary key that is filled by selecting and updating a row from another table. (to mimic the Oracle Sequence-function).
"Select PrimKey from tPrimKeys where table="MyTable"; Update tPrimKeys set PrimKey=PrimKey+1 where table="MyTable""
When 2 users simultaniously try to insert a record into MyTable, they both select the same Primkey from the primkeys table, and they both try to insert a record into myTable with the same primary key. Which obviously results into a duplicate key-error.
So what can I do ? Is there any way of explicitly locking the table (and unlocking) to make sure that no-one gets a duplicate number ?
0
Comment
Question by:mvw
1 Comment
 
LVL 45

Accepted Solution

by:
Kdo earned 250 total points
Comment Utility
Hi mvw,

Use the LOCK TABLE command.

  LOCK TABLE {table name} IN EXCLUSIVE MODE;

Note that the lock is held until a COMMIT or ROLLBACK occurs.  Also, the lock doesn't actually occur until all other processes have released every lock on any object in the table so depending on usage, the LOCK TABLE could suspend for quite a while.


That said, there are better ways to generate unique keys.  If you're wanting an incrementing, integer key, use an IDENTITY.  DB2 will automatically generate a unique value 1 higher than the previous high key (or allow you to generate your own key, depending on how you define the column.  Note that an IDENTITY column becomes the primary key of the table.

CREATE TABLE sample
(
  ID       INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
-- rest of table definitions
) ;

CREATE TABLE sample
(
  ID      INTEGER NOT NULL GENERATE BY DEFAULT AS IDENTITY,
-- rest of table definitions
);


If you need to generate this key so that it can be stored in any table and no duplicate exists in any of the tables, consider using a SEQUENCE.



Good Luck,
Kent
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now