Solved

How to explicitly lock a table on DB2

Posted on 2007-12-06
1
3,901 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 45

Accepted Solution

by:
Kent Olsen 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…

733 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