一、DBMS_LOCK相关知识介绍
锁模式:
名字 |
描述 |
数据类型 |
值 |
nl_mode |
Null |
INTEGER |
1 |
ss_mode |
Sub Shared: used on an aggregate object to indicate that share locks are being acquired on subparts of the object (对象的子部分,加上了Share锁) |
INTEGER |
2 |
sx_mode |
Sub Exclusive: used on an aggregate object to indicate that exclusive locks are being acquired on subparts of the object (对象的子部分,加上了Exclusive锁) |
INTEGER |
3 |
s_mode |
Shared: indicates that the entire aggregate object has a share lock, but some of the sub-parts may additionally have exclusive locks (对象加上了Share锁) |
INTEGER |
4 |
ssx_mode |
Shared SubExclusive (对象加上了Share锁,其子部分加上了Exclusive锁) |
INTEGER |
5 |
x_mode |
Exclusive (对象加上了Exclusive锁) |
INTEGER |
6 |
锁兼容性:
|
NL |
SS |
SX |
S |
SSX |
X |
NL |
SUCC |
SUCC |
SUCC |
SUCC |
SUCC |
SUCC |
SS |
SUCC |
SUCC |
SUCC |
SUCC |
SUCC |
fail |
SX |
SUCC |
SUCC |
SUCC |
fail |
fail |
fail |
S |
SUCC |
SUCC |
fail |
SUCC |
fail |
fail |
SSX |
SUCC |
SUCC |
fail |
fail |
fail |
fail |
X |
SUCC |
fail |
fail |
fail |
fail |
fail |
二、DBMS_LOCK Procedure
1、ALLOCATE_UNIQUE
dbms_lock.allocate_unique(
lockname IN VARCHAR2,
lockhandle OUT VARCHAR2,
expiration_secs IN INTEGER DEFAULT 864000
)RETURN INTEGER;
说明:锁模式转换。
类型:Procedure
参数:
lockname :产生唯一的LockID。大小不超过128B,大小写敏感。不能以'ORA$'字符串开头,Oracle Products保留。如果lockname已分配LockID,则返回handle;否则,生成一个新的LockID,并返回handle。
Lockhandle :返回值,request,convert,release调用。
expiration_secs:执行'allocate_unique'后,Clean Up的时间间隔。
2、 CONVERT
dbms_lock.convert(lockhandle IN VARCHAR2,
lockmode IN INTEGER,
timeout IN NUMBER DEFAULT maxwait
) RETURN INTEGER;
说明:锁模式转换。
类型:Function
参数:Lockhandle:allocate_unique取得的handle。
Lockmode :转换的锁模式。
Timeout :转换等待时间。
返回值:
Return Values |
|
0 |
Success |
1 |
Timeout |
2 |
Deadlock |
3 |
Parameter error |
4 |
Don't own lock specified by id or lockhandle |
5 |
Illegal lock handle |
3、RELEASE
dbms_lock.release(lockhandle IN VARCHAR2) RETURN INTEGER;
说明:释放锁。
类型:Function
参数:Lockhandle:allocate_unique取得的handle。
返回值:
Return Values |
|
0 |
Success |
3 |
Parameter error |
4 |
Don't own lock specified by id or lockhandle |
5 |
Illegal lock handle |
4、REQUEST
dbms_lock.request(lockhandle IN VARCHAR2,
lockmode IN INTEGER DEFAULT x_mode,
timeout IN INTEGER DEFAULT maxwait,
release_on_commit IN BOOLEAN DEFAULT FALSE
) RETURN INTEGER;
说明:请求锁。
类型:Function
参数:Lockhandle:allocate_unique取得的handle。
Lockmode:加锁模式。
Timeout:转换等待时间。
release_on_commit:在commit或者rollback之后释放锁,默认False。
返回值:
Return Values |
|
0 |
Success |
1 |
Timeout |
2 |
Deadlock |
3 |
Parameter error |
4 |
Don't own lock specified by id or lockhandle |
5 |
Illegal lock handle |
5、SLEEP
dbms_lock.sleep(seconds IN NUMBER);
说明:Session睡眠。
类型:Procedure
参数:seconds:睡眠时间。
例子:
DECLARE
-- lock
l_lockname VARCHAR2(100);
l_lockhandle VARCHAR2(200);
l_lock_output NUMBER;
l_locked BOOLEAN := FALSE;
g_pkg_name VARCHAR2(240) := 'test_package';
g_org_id NUMBER := 125;
BEGIN
/* 定义按何种方式并发(此处为同一个OU不能同时执行)
* lockname类似于定义一个唯一的名字,当并发程序执行时就会去判断是否这个唯一标识已经存在*/
l_lockname := g_pkg_name || '_' || g_org_id;
--根据l_lockname获取唯一标识l_lockhandle
dbms_lock.allocate_unique(lockname => l_lockname,
lockhandle => l_lockhandle);
/*用l_lockhandle这个唯一标识去给当前请求加锁
-- Return value:
-- 0 - success
-- 1 - timeout
-- 2 - deadlock
-- 3 - parameter error
-- 4 - already own lock specified by 'id' or 'lockhandle'
-- 5 - illegal lockhandle*/
l_lock_output := dbms_lock.request(l_lockhandle,
6,
60,
FALSE);
IF l_lock_output <> 0 THEN
--Output lock failure message
RAISE apps.fnd_api.g_exc_error;
END IF;
--此处添加请求的业务逻辑
--dbms_lock.sleep(seconds => 50);
/*特别注意的是一定要将lockname释放掉 否则这个并发就永远别想再执行了*/
IF l_lock_output = 0 THEN
l_lock_output := dbms_lock.release(l_lockhandle);
END IF;
EXCEPTION
WHEN apps.fnd_api.g_exc_error THEN
IF l_lock_output = 0 THEN
l_lock_output := dbms_lock.release(l_lockhandle);
END IF;
WHEN OTHERS THEN
IF l_lock_output = 0 THEN
l_lock_output := dbms_lock.release(l_lockhandle);
END IF;
END;