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:





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);