Solved

How to explicitly lock a table on DB2

Posted on 2007-12-06
1
3,935 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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…

688 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