Solved

Preventing deadlock.

Posted on 2013-01-07
11
333 Views
Last Modified: 2013-01-13
I have two tasks, both invoke the same proc in package mypkg.

The proc inserts into a table. Thus we have two sessions inserting into same table through a proc in mypkg.

Thus, Task1 and Task2 tasks failed due to deadlock issue,

Logs:
25-MAY-12 04.51.56.680350000 AM "[do_stuff]{}
Stack:
ORA-00060: deadlock detected while waiting for resource
Backtrace:
ORA-06512: at "DEV.mypkg", line 123"


25-MAY-12 04.51.44.221277000 AM "[do_stuff]{}
Stack:
ORA-00060: deadlock detected while waiting for resource
Backtrace:
ORA-06512: at "DEV.mypkg", line 123"


both tasks finished successfully on rerun induvidually.

What can i do to prevent such deadlock issues when two sessions call the same proc in a package.
0
Comment
Question by:gram77
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38751505
Have a unique identifier on the table so each session generates unique id (like a SYS_GUID())
or
use a global temorary table for the inserts.  This is sort of why they were created.  Only the session that inserts into them can see the rows and they are automatically cleaned up when the session ends (or commits depending on how you create the table).
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 38751613
or, serialize the procedures.  Put in a call to dbms_lock as the first line of each so which ever comes in first will block the other.


remember to release the lock at the end of the procedure (including exceptions) so the other procedure won't wait forever
0
 

Author Comment

by:gram77
ID: 38751852
Locking seems to be a good idea,  however, if two sessions both get called,  both will try to obtain a lock on the table, out of which one will succeed,  what will happen to the other package - will it keep trying to obtain a lock until package 1 ends
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38751868
If you can explain what these inserts are for we can likley offer better advice.

If they are inserts into some sort of staging table, I would not suggest table locks.  Why make the other session wait when it really doesn't need to?  Unless this is the desired behavior.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 38751879
>>> - will it keep trying to obtain a lock until package 1 ends

you can choose what will happen.  It can either wait until the lock is released - this is what I assume you want to happen.  Or it can exit with an error.
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 38753720
deadlocks can happen in a few ways
it always depends on what the code actually does

> so what code are you using ?
0
 

Author Comment

by:gram77
ID: 38754581
My code insert into table as already mentioned
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38754589
>>My code insert into table as already mentioned

We get that.  What we would like to know is more about the process and what happens to this data after it is inserted.

We have provided several ways to get around the deadlock.  The more information you can provide about the process the better advice we can provide.
0
 

Author Comment

by:gram77
ID: 38755910
A good example i saw on locking is here:
http://jeffkemponoracle.com/2005/10/19/user-named-locks-with-dbms_lock/

Any other simple example?


It has a merge statement:

         merge into mytable a
         using      (select col1,
                            col2,
                            col3,
                     from      myview b
                            inner join
                               mytable c
                            on b.col1 = c.col1
                               and c.mydate = b.mydate
                     where  b.col4 is not null
                            and b.mydate = p_mydate) s
         on         (a.rowid = s.rid)
         when matched then
            update set a.col1         = s.col1,
                       a.col2       = 'ABC',
                       a.mydate           = s.mydate,
                       a.col3     = s.col3;
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38756018
So you have decided to go with manual locks?

I've never used them so will have to defer to the other Experts.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 38756205
going with the user locks, something like the original code in Jeff Kemp's article should work fine.  The followup stuff only applies to his specific scenario with triggers.

For your usage (at least as I understand it)  simply allocate a particular handle and request it.

The only thing I'd change is the release on commit, as noted in the article,  set that to FALSE.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

773 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