How to lock a row in a db2/400

Hi all,
Could someone show me how to lock a row in the database DB2/400
The scenario:
DB.beginTransaction
User 1 gets a row with a column bill_number
"select bill_number from numbers where pk = xxx for update"
aux_bill_number = bill_number
USER 1 updated this row
"update numbers set bill_number = bill_number +1 where pk = xxx "
USER 1, like final step, insert aux_bill_number in other table as primary key
DB.commitTransaction
While doing so,
User 2 also gets a row with a bill_number. this user must take bill_number with value 2, but take the same value 1
This it causes key duplicated in the destiny table.
Please, help me.
How I must lock the row not to allow to read until finishing the previous transaction?

Hi all,
Could someone show me how to lock a row in the
database DB2/400Isn't SELECT FOR UPDATE standard?
>
The scenario:
DB.beginTransaction
er 1 gets a row with a column bill_number
"select bill_number from numbers where pk =
xxx for update"
aux_bill_number = bill_number
1 updated this row
"update numbers set bill_number = bill_number
+1 where pk = xxx "
USER 1, like final step, insert aux_bill_number in
other table as primary key
DB.commitTransactionSounds like you're trying to do something that a JOIN would do better.
While doing so,
User 2 also gets a row with a bill_number. this user
must take bill_number with value 2, but take the
same value 1
This it causes key duplicated in the destiny table.
Please, help me.
How I must lock the row not to allow to read until
finishing the previous transaction?Setting the isolation level to SERIALIZABLE might help.
%

Similar Messages

  • How to lock a row by using 'for update'?

    how to lock a row by using 'for update'?

    Hi,
    SELECT * FROM <TABLE> WHERE <PK_COLUMN>=<VALUE> FOR UPDATE NOWAITThis will help in locking the row with the primary key value that you provide
    cheers
    VT

  • How to lock the rows in program

    how to lock the rows in program

    Do not create a duplicate thread.
    many had already answered your query here.
    how to lock the rows in program
    Regards,
    Prazy

  • How to lock a row in a db

    Hi,
    Could someone share the code for how to lock the whole database, part of the database or just a row in the database?
    Thanks.

    Whoops... I am sorry about that.
    I am asking about MS Access (version 2002 if that matters)
    Thanks,
    Yaakov.

  • How to lock a row before update using UPDATE statement ?

    I want to lock a row before updating using the UPDATE statement! is there any "reserved word" that I can append to an update statement so, that the row get locked!, also, will the lock gets released automatically once the update is done or do I have to explicitly release the lock?
    how can I do this ?
    any help is greatly appreciated.
    Thanks,
    Srini.

    For detail information, see http://otn.oracle.com/doc/server.815/a67779/ch4l.htm#10900
    The lock will be released by "commit".
    FOR UPDATE Examples
    The following statement locks rows in the EMP table with clerks located in New York and locks rows in the DEPT table with departments in New York that have clerks:
    SELECT empno, sal, comm
    FROM emp, dept
    WHERE job = 'CLERK'
    AND emp.deptno = dept.deptno
    AND loc = 'NEW YORK'
    FOR UPDATE;
    The following statement locks only those rows in the EMP table with clerks located in New York. No rows are locked in the DEPT table:
    SELECT empno, sal, comm
    FROM emp, dept
    WHERE job = 'CLERK'
    AND emp.deptno = dept.deptno
    AND loc = 'NEW YORK'
    FOR UPDATE OF emp.sal;

  • How to lock a row and release lock

    Hello
    There are two thread, they read the same row, I want to lock this row when the first thread read the record, after this thread commit its data, release the lock, the second thread can lock this record, also can commit its data. I also want to know how to release the lock. And these operations are in different Classes, e.g. in Class A first thread lock the record, in Class B second lock the record, and in Class C release the lock.
    Note: these two thread will read this record at same time.
    Thanks in advance.

    TopLink provides to forms of locking, optimistic and pessimistic.
    <p>
    You are referring to pessimistic. You can use pessimistic locking in TopLink through the ReadObject/ReadAllQuery API acquireLocks(), or acquireLocksWithoutWaiting(), this will use a "Select ... for Update" in SQL. You must be in a transaction to lock a row in the database (UnitOfWork in TopLink), and you locks are released when you commit or rollback the transaction. If you are using JPA, TopLink supports a JPA query hint "toplink.pessimistic-lock"="Lock" to allow pessimistic locking.
    <p>
    You can also use optimistic locking. An optimistic lock does not prevent another user from reading the same row, but ensures that only one user will be able to commit changes to the row. Optimistic locking typically has the advantage of better concurrency, and does not have issues with deadlocks.
    <p>---
    <br>James Sutherland
    <br>Oracle TopLink, EclipseLink
    <br>Wiki: Java Persistence, EclipseLink

  • How to lock the rows in a transaction so others cant modify the rows

    Hi,
    I want few suggestion in the following scenario .
    I have a few records ( say 100 ) in a table which Im sending to the client over a network and are populated in a swing GUI...Now the client selects few of them ( say 3 ) and submits to the server. Now the server should assign a number to those rows and store them in the database. As it is a multi client environment While assigning the number it should check that no other client has already assigned a number to at lease to one of the row in the meantime. in that case it shoudl fail.
    In this case i want to lock those 3 records when the request arrives at the server assign number commit and release the lock.
    I want to avoid deadlocks.
    Can any one suggest me whoch locking policy should i use and how ?
    Thanx in advance...
    Mahi

    It depends on how acceptable it is for your commit to fail. By far the easiest and safest thing to do is use Optimistic Locking. This way if the server tries to commit changes to something that has already been committed, an Optmistic Lock exception will be thrown. It's up to your application to handle this, and depending on your domain and the likelyhood of this happeneing, you could just tell the user "try again". The other option is pessemistic locking, which is very, very, dangerous (in my opinion). The main issue is that it's resource intensive, and different databases have different semantics on how it works.
    IIWY, I'd look at Optimistic Locking in the docs and go from there.
    - Don

  • How To Lock Rows (in the DB)

    I need to lock a Row when a users enters some editing JSP until the user finished doing changes, while the other users (using VOs. created on the fly, and SQLJ) **DO NOT** have to get access to that Row.
    From a bean used in a JSP or in My AppModule How do I get a Lock over a Row? (The lock must be visible to all users of the D.B. not just the ones using the Framework.)
    Also setting the Transaction locking mode to NONE and doing manually the locks doesn't seems to be locking anything to the rest of users of the Framework.
    Thanks in advance.

    Or else you could issue the following select statement:
    select *
    from tab1, tab2, ....
    where <join clause>
    for update of <table to be updated>

  • How to find who has locked a row in a table?

    My DB is a Oracle Database 10g Enterprise Edition Release 10.2.0.4.0.
    My question is very simple: You try to update a row of a table but find out it is locked by somebody else.
    How do I find which user (or session) has locked that row?

    SELECT Decode(request, 0, 'Holder: ',
                           'Waiter: ')
           ||vl.sid sess,
           status,
           id1,
           id2,
           lmode,
           request,
           vl.TYPE
    FROM   v$lock vl,
           v$session vs
    WHERE  ( id1, id2, vl.TYPE ) IN (SELECT id1,
                                            id2,
                                            TYPE
                                     FROM   v$lock
                                     WHERE  request > 0)
           AND vl.sid = vs.sid
    ORDER  BY id1,
              request

  • How to lock a table when i insert a row

    hi, friends,
    how to lock a table when i insert a row, then unlock the table

    If you want to lock the whole table so that no-one else can do DML then you go LOCK TABLE <table> IN EXCLUSIVE MODE. This lock remains in force until you commit or rollback.
    There are less powerful locking modes available, but you can look those up in the OTN online documentation.
    APC

  • How can i explicitly lock a row of a Table

    Hi
    How can we implement optimistic locking , i am using Oracle as Database for my Application .
    Assume that i am working on a single row of a Table .
    Now please tell me how can i explicitly lock this row , so that other simultanoues users insert or update operations are not affected to this particular row .
    I am not using any ORM related technology
    Please help .

    raviprivate wrote:
    Hi
    How can we implement optimistic locking , i am using Oracle as Database for my Application .
    Assume that i am working on a single row of a Table .
    Now please tell me how can i explicitly lock this row , so that other simultanoues users insert or update operations are not affected to this particular row .Either you want optimistic locking which does not use (database) locks at all, or you want pessimistic locking which does use database locks.
    In optimistic locking everyone is allowed to read and to update without taking out a lock. Usually the optimistic lock is realised by including either a 'version' or 'last_modified' column which with every update is either increased (version) or set to the current_timestamp (last_modified). The update statement must be issued with a condition including that column (eg UPDATE ... WHERE id=<id> AND version=<version at time of read>). If the column was already changed by another user or process zero rows will have been updated signalling to the program that another user already changed the row and the user must redo his changes, or a more complex method of change comparison can be done.

  • How to get a row level share lock

    Is it possible to acquire a row-level share lock, which would do all of the following?
    1. Prevent others from updating that row.
    2. Allow others to read that row.
    3. Allow others to update other rows in the same table.
    I have the following scenario where two transactions need to lock eachother out:
    Set-up:
    Insert into TABLE_A(value_a) values ('ok');
    Insert into TABLE_B(value_b) values ('ok');
    Transaction A:
    Select value_b from TABLE_B
    If value_b = 'ok', update TABLE_A set value_a = 'not ok'
    Transaction B:
    Select value_a from TABLE_A
    If value_a = 'ok', update TABLE_B set value_b = 'not ok'
    If transaction A runs first then the end result is "not ok" only in TABLE_A.
    If transaction B runs first then the end result is "not ok" only in TABLE_B.
    If the two transactions run concurrently, it is possible to get "not ok" in both tables. This is what I would like to prevent.
    One way to get what I want is to use "select for update":
    Transaction A:
    Select value_a from TABLE_A for update
    Select value_b from TABLE_B for update
    If value_b = 'ok', update TABLE_A set value_a = 'not ok'
    Transaction B:
    Select value_a from TABLE_A for update
    Select value_b from TABLE_B for update
    If value_b = 'ok', update TABLE_B set value_a = 'not ok'
    This way both transactions won't perform their update unless they know that the result if their select will still be the same after they commit. However, by using "select for update" Transaction A has gained an exclusive lock on the TABLE_B row. If a Transaction C with the same contents as Transaction A happens concurrently, then the two will block eachother even though all they both want is to read data from the same table.
    Another way is to use "lock table", however using that would block out not only writes to a specific row, but writes to all rows in the table. (In my example there is only one row, but obviously that's just a simplified example.)
    I have looked at the "serializable" isolation level, but that doesn't seem to help because the queries and updates involve more than one table.
    I know that "reads don't block writes" is a fundamental part of the Oracle design that makes Oracle what it is, but is there any way I can explicitly make it happen anyway? Or can anyone see some other solution to what I'm trying to achieve?

    Let me give a more real-world example to show what I'm talking about.
    Imagine a simple bug-reporting OLTP application. Each bug has 3 fields:
    1. Description
    2. Resolution
    3. Status (open/closed)
    The application also has some rules:
    A. If the Status is "closed", Description and Resolution cannot be changed.
    B. In order to change the Status to "closed", both a Description and Resolution must be specified.
    Now as long as only one person at a time updates a bug, everything is fine. However, if one person switches the Status to "closed" while another concurrently blanks out the Resolution you end up with a closed bug that has no Resolution. This is why some locking is necessary.
    If Description, Resolution, and Status are all stored in the same table row, then it makes sense for each transaction to simply select the whole row FOR UPDATE, and then UPDATE it after it ensures all the rules are passed. However, in my fictional bug-reporting application they are all stored in separate tables. Now, the transaction that is making the change to Resolution could lock the row in the Status table FOR UPDATE (as everyone so far has suggested). Transactions making a similar change to the Description should then also lock the row in the Status table FOR UPDATE for the same reason. The end result of this is that Description changes and Resolution changes lock eachother out (can't happen concurrently, are serialized) when they don't need to be. Description changes and Resolution changes don't affect eachother, but because they both need to (exclusively) lock the same row, they are serialized.
    If there was a way to acquire a share lock on the row in the Status table, then Description and Resolution changes would use that and not affect eachother. Status changes would still acquire an exclusive lock on the row, and would be blocked by (and block any further) concurrent Description and Resolution changes. This would be ideal.

  • How can we split limit row counts for DB2?

    We want to mass data from DB2. but, we guess 200,000,000 count.
    so we try to split by 5,000 count and almost 5 MB.
    How can we use sql query for DB2? How can we make JDBC Sender?
    but, We don't use update query. because Customer did not allow ours requirement.
    How can we do?

    Well, there are ways, how to achieve success, but in all of them must customer do some changes in their database.
    a, create "interface table", run select into (load relevant data to it) and then run JDBC adapter select on this table using update statement to mark the processed lines
    b, there is for sure a table, which is "main" - every record is replicated and every one is repliacted only once.. Add new column to this table and use it as a "marker" for already processed records..
    ...for example...
    There may be of course more possibilites...
    If you don't want to process all records at once, there must be some flag to distinguish already processed records from not processed ones..
    Peter

  • How to distinguish individual rows in a view?

    As well known, we can use rowid to distinguish individual rows in a table. However, who know how to distinguish individual rows in a view? For there are not rowid in a view.

    I'm not sure I usnderstand your question, or at least I hope I don't.
    We do not use ROWID to distinguish rows in a table: we use unique iodentifiers, that is primary keys. A primary key is stable across the lifetime of a record: a rowid is guaranteed only for the lifetime of a transaction, and then only if we have locked that row.
    From this it follows, we should so design our views that each row in the view has a column, or combination of columns, that uniquely identify it.
    Cheers, APC

  • How to lock records in 8.1.7?

    Hi,
    How can I lock records in 8.1.7 so that only one user can select/update these records at one time? If possible, can it return an error mesg if one user try to select/update records locked by others?
    Thanks a lot.
    David

    Oracle automatically locks a row just before updating or deleting.
    You can lock rows explicitly before doing an update etc by selecting it for update:
    select * from emp where empno=21 for update;
    update emp
    set sal=sal*1.1
    where empno=21;
    commit;
    The lock will be released on the commit (or rollback).
    Please note that in Oracle, writers never block readers and readers never block writers. Which means, that although you have locked a row (or group of rows) for update, other users will still be able to select against those rows (in the pre-changed or consistant state).

Maybe you are looking for

  • Help! iPhoto library not available to other Apple applications

    My iPhoto library is not available for browsing in other Apple applications like iMovie, Mail or even Desktop and Screen Saver. I've rebuilt the iPhoto database, fixed permissions, tried to do over starting with a clean plate and importing the databa

  • Is this good practice?

    Hi, I am working on a project with some existing source code that someone else wrote. They made this file called Pair.java. This is all that is in there. I looked elsewhere and saw that the reason this person did this is because they need to store tw

  • How import raw files from canon550D into PSE9 ?

    hi' i've just purchased PSE9. I have downloaded Camperqa Raw 6.2 update and installed both...but still I cannot see/open my raw files from canon 550D - any suggestions...??

  • How can I use DxO 9 as an external editor in lightroom 5.3 ?

    I tried to follow the directions in help.  They were to: Open a photo and go to "photo" or "develop" tab in Lightroom.  Under  drop down,  find "edit in"  tab Then it lists programs but not DxO 9.  The help menu says "if not listed, just add it using

  • How to delete user defined swatches in adobe illustrator CS6?

    I'm trying to delete swatches that have been saved as User Defined but i'm unable to locate the local file to do it. Any help would be greatly appreciated.