Solved

How to explicitly lock a table on DB2

Posted on 2007-12-06
1
3,844 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
ID: 20419101
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Read about achieving the basic levels of HRIS security in the workplace.
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…

863 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

25 Experts available now in Live!

Get 1:1 Help Now