Question

How to find database table lock and send alerts

Asked by: prasen

I want to check al the Experts, if there is a way to send alerts to a group of mail ids, say AAA
if there is Database table lock, for more than 10 mins
If so, where to put the query, like will it be a trigger or something else, and also there would be 100s of tables, if for each table, we need to set an alert
I'm using Oracle 10g and 9i databases

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-06-23 at 10:54:24ID24515576
Topics

Oracle Database

,

Oracle 10.x

,

Oracle 9.x

Participating Experts
2
Points
200
Comments
17

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. alert
    i want to put a value of a form field in the alert message .. (not the regular message) ..
  2. attaching an alert..
    i want to attach an alert to my menu toolbar.. when i tried to attach the alert to a button on the form it works..however, when attaching it to the menu toolbar.. i am not getting the alert.. so what can be the problem..and how can i solve it....... thanks..
  3. Creating Alerts in Oracle Apps 11i
    I want to create an update/insert/delete alert on a certain column of a table in Oracle Apps without using triggers... I know that Oracle Apps 11i has a facility which allows me to create an alert on the whole table... but my requirement is only for a particular column not t...
  4. Messages and Alerts
    Hello everyone, When i click on my commit button, i want a message or an alert dispalying my 'serial_no' which is a sequence number that is dispalyed only after i commit my changes with the message saying :'your request number is ('serial_no'). This serial_no should be pick...
  5. Trigger - Alert
    Hi, I'm trying to devise a trigger that will catch any updates made to a particular field in a table, I have built the trigger and I get no errors from the syntax, but when I go in and deliberately change the field I get no email alert. Although I know the senmail works as I...
  6. lock on dbms_alert
    Part of our installation process for a database includes this statemen grant to the new user: GRANT EXECUTE ON SYS.DBMS_ALERT TO "NEW_USER" About 2 out of 5 attempts at installation result in this timeout. ERROR at line 1: ORA-04021: timeout occurred while wa...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: schwertnerPosted on 2009-06-23 at 11:08:47ID: 24694339

It is possible if a Mail server is available and you can use SMTP to send mails via it.
In 10g it is easiely to send mails:

PROCEDURE DBMS_MAIL.SEND
   (from_str IN VARCHAR2, to_str IN VARCHAR2,
    cc IN VARCHAR2, bcc IN VARCHAR2,
    subject IN VARCHAR2, reply_to IN VARCHAR2,
    body IN VARCHAR2);

 

by: sdstuberPosted on 2009-06-23 at 11:18:14ID: 24694433

in 9i and 10g you should use utl_smtp  (if you can do 10g only, then I agree dbms_mail is easier)

try something like this...

modify the query to return whatever lock information you want and format however you want

then put it in a dbms_job to check every 5 or 10 minutes.

Note,  the more frequently the job runs, the more timely your notifications
but it also means flooding the recipients with email until the lock is cleared

CREATE OR REPLACE PROCEDURE lock_monitor
IS
    v_connection UTL_SMTP.connection;
BEGIN
    v_connection := UTL_SMTP.open_connection('your.email.server.com');
    UTL_SMTP.helo(v_connection, 'your.domain.com');
    UTL_SMTP.mail(v_connection, 'your_return_address@your.domain.com');
    UTL_SMTP.rcpt(v_connection, 'your_recipient@your.domain.com');
    UTL_SMTP.open_data(v_connection);
    UTL_SMTP.write_data(v_connection, 'From: Lock Monitor' || UTL_TCP.crlf);
    UTL_SMTP.write_data(v_connection, 'To: AAA group' || UTL_TCP.crlf);
    UTL_SMTP.write_data(v_connection, 'Subject: Long lock held' || UTL_TCP.crlf);
 
    UTL_SMTP.write_data(v_connection, UTL_TCP.crlf);
    UTL_SMTP.write_data(v_connection, 'Locks found' || UTL_TCP.crlf);
 
    FOR x
    IN (SELECT lk.sid,
               se.username,
               se.osuser,
               se.machine,
               DECODE(lk.TYPE, 'TX', 'Transaction', 'TM', 'DML', 'UL', 'PL/SQL User Lock', lk.TYPE)
                   lock_type,
               DECODE(
                   lk.lmode,
                   0, 'None',
                   1, 'Null',
                   2, 'Row-S (SS)',
                   3, 'Row-X (SX)',
                   4, 'Share',
                   5, 'S/Row-X (SSX)',
                   6, 'Exclusive',
                   TO_CHAR(lk.lmode)
               )
                   mode_held,
               DECODE(
                   lk.request,
                   0, 'None',
                   1, 'Null',
                   2, 'Row-S (SS)',
                   3, 'Row-X (SX)',
                   4, 'Share',
                   5, 'S/Row-X (SSX)',
                   6, 'Exclusive',
                   TO_CHAR(lk.request)
               )
                   mode_requested,
               TO_CHAR(lk.id1) lock_id1,
               TO_CHAR(lk.id2) lock_id2,
               ob.owner,
               ob.object_type,
               ob.object_name,
               DECODE(
                   lk.block,
                   0,
                   'No',
                   1,
                   'Yes',
                   2,
                   'Global'
               )
                   block,
               se.lockwait,
               lk.ctime held_for_seconds
        FROM   v$lock lk, dba_objects ob, v$session se
        WHERE  lk.TYPE IN ('TM', 'UL')
           AND lk.sid = se.sid
           AND lk.id1 = ob.object_id
           AND lk.ctime > 600)
    LOOP
        UTL_SMTP.write_data(
            v_connection,
               x.sid
            || ' '
            || x.username
            || ' '
            || x.osuser
            || ' '
            || x.machine
            || ' '
            || x.lock_type
            || ' '
            || x.mode_held
            || ' '
            || x.mode_requested
            || ' '
            || x.lock_id1
            || ' '
            || x.lock_id2
            || ' '
            || x.owner
            || ' '
            || x.object_type
            || ' '
            || x.object_name
            || ' '
            || x.block
            || ' '
            || x.lockwait
            || ' '
            || x.held_for_seconds
            || UTL_TCP.crlf
        );
    END LOOP;
 
 
    UTL_SMTP.close_data(v_connection);
    UTL_SMTP.quit(v_connection);
EXCEPTION
    WHEN OTHERS
    THEN
        DBMS_OUTPUT.put_line(DBMS_UTILITY.format_error_stack);
END;

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:

Select allOpen in new window

 

by: prasenPosted on 2009-06-23 at 11:21:15ID: 24694462

whenever there is lock for > 10 mins, the alert should get fired,
Also, how to identify if a particular table got a lock, and if for all the tables in the DB, do we need to set specific alerts or one alert can take care of any table lock

 

by: sdstuberPosted on 2009-06-23 at 11:22:09ID: 24694470

AND lk.ctime > 600  --  600 seconds = 10 minutes

all locks held on all objects wil be found and reported

 

by: sdstuberPosted on 2009-06-23 at 11:25:26ID: 24694503

if your email system has smtp groups that will route appropriately you can simply send to that

if not, you can add multiple RCPT lines, one for each recipient

UTL_SMTP.rcpt(v_connection, 'your_recipient@your.domain.com');

 

by: schwertnerPosted on 2009-06-23 at 12:13:56ID: 24695014

The script will create statements to kill  the sessions which are holding locks.
Modifying this script and having a table that joins the users and their email addresses it will be possible to send emails

REM    The report generated by this script gives information on sessions 
REM    which are holding locks and gives the information needed to kill 
REM    using the ALTER SYSTEM KILL SESSION command. 
REM ------------------------------------------------------------------------ 
REM Main text of script follows: 
 
set linesize 180 pagesize 66 
break on Kill on username on terminal 
column Kill heading 'Kill String' format a13 
column res heading 'Resource Type' format 999 
column id1 format 9999990 
column id2 format 9999990 
column lmode heading 'Lock Held' format a20 
column request heading 'Lock Requested' format a20 
column serial# format 99999 
column username  format a10  heading "Username" 
column terminal heading Term format a10
column OSuser heading OSuser format a15
column machine heading Machine format a10 
column tab format a35 heading "Table Name" 
column owner format a9 
column Address format a18 
select	nvl(S.USERNAME,'Internal') username, 
	nvl(S.TERMINAL,'None') terminal,
        nvl(S.MACHINE,'None') machine,
        nvl(S.OSUSER,'None') OSuser,    
	L.SID||','||S.SERIAL# Kill, 
	U1.NAME||'.'||substr(T1.NAME,1,20) tab, 
	decode(L.LMODE,1,'No Lock', 
		2,'Row Share', 
		3,'Row Exclusive', 
		4,'Share', 
		5,'Share Row Exclusive', 
		6,'Exclusive',null) lmode, 
	decode(L.REQUEST,1,'No Lock', 
		2,'Row Share', 
		3,'Row Exclusive', 
		4,'Share', 
		5,'Share Row Exclusive', 
		6,'Exclusive',null) request 
from	V$LOCK L,  
	V$SESSION S, 
	SYS.USER$ U1, 
	SYS.OBJ$ T1 
where	L.SID = S.SID  
and	T1.OBJ# = decode(L.ID2,0,L.ID1,L.ID2)  
and	U1.USER# = T1.OWNER# 
and	S.TYPE != 'BACKGROUND' 
order by   username, terminal, machine, lmode
--order by 1,2,6 
/ 
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:

Select allOpen in new window

 

by: sdstuberPosted on 2009-06-23 at 12:19:28ID: 24695082

that's essentially what I have in the procedure above,
except without the emailing and time checks.
but yes, it should work

 

by: prasenPosted on 2009-06-24 at 00:49:00ID: 24699002

Need one more clarification ---
when we implement the above all DB objects lock check, what will be the impact on the database performance , will it drastically slow down the system, if we have more than 200 DB objects in the systems -- DB is Oracle 10g

 

by: schwertnerPosted on 2009-06-24 at 05:32:12ID: 24700376

I do not understand entirelly what are you asking.
Looking at Mr. Stuber script and at mine they will work fast.
The problem iis not in the script, the problem is in the locked tables - they prevent
the user to work with them.

But I am curious how you get locked tables.
Normally Oracle locks only records - it uses the lowest degree of locking.
It is possible to lock tables, but in special cases:

1. Using foreign keys without having indexes on them
2. Explicitelly to issie LOCK TABLE command (a wrong approach!).

Have you investigated the nature of your table locking?

 

by: sdstuberPosted on 2009-06-24 at 05:39:20ID: 24700432

agreed,

the "finding" of locks is quick

also, are you sure you are getting "table locks"?

if I update one row of a table, you will see i have a lock on that object but that doesn't mean I have the entire table locked.

 

by: prasenPosted on 2009-06-24 at 12:16:32ID: 24704674

Yes agree, but in certain scenarios, when 100s of thousands of record in a table gets updated in a single Update stmt, it locks the entire table and brings DB down
In order to prevent it from hapenning, if we put a check if any table is locked for X mins, it should be notified
But, was wondering, what will be the DB scan cost for this alert, as there will be 200 DB tables

 

by: sdstuberPosted on 2009-06-24 at 12:30:48ID: 24704819

it's not a matter of the number of tables, it's the number of locks that will be main criteria and 200 tables isn't that many.

of course, if you're worried, just run the query from my procedure by itself and see how it performs for you

SELECT lk.sid,
               se.username,
               se.osuser,
               se.machine,
               DECODE(lk.TYPE, 'TX', 'Transaction', 'TM', 'DML', 'UL', 'PL/SQL User Lock', lk.TYPE)
                   lock_type,
               DECODE(
                   lk.lmode,
                   0, 'None',
                   1, 'Null',
                   2, 'Row-S (SS)',
                   3, 'Row-X (SX)',
                   4, 'Share',
                   5, 'S/Row-X (SSX)',
                   6, 'Exclusive',
                   TO_CHAR(lk.lmode)
               )
                   mode_held,
               DECODE(
                   lk.request,
                   0, 'None',
                   1, 'Null',
                   2, 'Row-S (SS)',
                   3, 'Row-X (SX)',
                   4, 'Share',
                   5, 'S/Row-X (SSX)',
                   6, 'Exclusive',
                   TO_CHAR(lk.request)
               )
                   mode_requested,
               TO_CHAR(lk.id1) lock_id1,
               TO_CHAR(lk.id2) lock_id2,
               ob.owner,
               ob.object_type,
               ob.object_name,
               DECODE(
                   lk.block,
                   0,
                   'No',
                   1,
                   'Yes',
                   2,
                   'Global'
               )
                   block,
               se.lockwait,
               lk.ctime held_for_seconds
        FROM   v$lock lk, dba_objects ob, v$session se
        WHERE  lk.TYPE IN ('TM', 'UL')
           AND lk.sid = se.sid
           AND lk.id1 = ob.object_id
           AND lk.ctime > 600
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:

Select allOpen in new window

 

by: sdstuberPosted on 2009-06-24 at 12:32:12ID: 24704838

same with schwertner's script.

test everything  before you adopt it.

don't run anything in your production system just because an "expert" says to.
test it yourself, make sure it does what you want and you understand it then install it.

 

by: schwertnerPosted on 2009-06-24 at 13:22:49ID: 24705374

The locks are registered in internal views in advance.
The select only looks in these views and retrieves the rows.
Thats all.
How fast?
Very fast, but depends on the load of the DB.
If the DB is heavy loaded -  then some small delay is expected.

In my script do not run immediatelly the created ALTER SYSTEM KILL SESSION commands.
The script is reliable, but there are different ways to do the job.

 

by: prasenPosted on 2009-07-03 at 10:43:19ID: 24773655

The query is pretty good, but there can be other database objects which are valid, and can hold locks -- like session Ids, etc
How to make sure only valid tables lock are being identified?

 

by: schwertnerPosted on 2009-07-06 at 04:15:41ID: 24784048

Locks are locks held by sessions.
I think both scripts display the ID of the session holding the lock.

 

by: sdstuberPosted on 2009-07-06 at 06:23:57ID: 24784883

what are you calling a "valid" table lock.

A lock is a lock.  Only you can determine if the lock is being held for valid reasons or not.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...