?
Solved

How to explicitly lock a table on DB2

Posted on 2007-12-06
1
Medium Priority
?
3,993 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 46

Accepted Solution

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
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…

800 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