首页 > 数据库 > Oracle > Oracle悲观封锁和乐观封锁在比较
create or replace package ttx_temp_pkg is
g_ok varchar2(2) := 'OK';
g_changed varchar2(21) :='CHANGED BY OTHER USER';
g_no_row varchar2(14) := 'NO ROW UPDATED';
procedure lock_row
(
x_message out nocopy varchar2,
p_id in number,
p_owner in varchar2,
p_object_name in varchar2,
p_subobject_name in varchar2,
p_object_id in number,
p_data_object_id in number,
p_object_type in varchar2,
p_created in date,
p_last_ddl_time in date,
p_timestamp in varchar2,
p_status in varchar2,
p_temporary in varchar2,
p_generated in varchar2,
p_secondary in varchar2
);
procedure update_row
(
x_message out nocopy varchar2,
p_id in number,
p_owner in varchar2,
p_object_name in varchar2,
p_subobject_name in varchar2,
p_object_id in number,
p_data_object_id in number,
p_object_type in varchar2,
p_created in date,
p_last_ddl_time in date,
p_timestamp in varchar2,
p_status in varchar2,
p_temporary in varchar2,
p_generated in varchar2,
p_secondary in varchar2
);
procedure delete_row
(
p_id in number,
x_message out nocopy varchar2
);
end ttx_temp_pkg;
/
create or replace package body ttx_temp_pkg is
procedure lock_row
(
x_message out nocopy varchar2,
p_id in number,
p_owner in varchar2,
p_object_name in varchar2,
p_subobject_name in varchar2,
p_object_id in number,
p_data_object_id in number,
p_object_type in varchar2,
p_created in date,
p_last_ddl_time in date,
p_timestamp in varchar2,
p_status in varchar2,
p_temporary in varchar2,
p_generated in varchar2,
p_secondary in varchar2
) is
cursor cur_ttx_temp is
select
id,
owner,
object_name,
subobject_name,
object_id,
data_object_id,
object_type,
created,
last_ddl_time,
timestamp,
status,
temporary,
generated,
secondary
from ttx_temp
where id = p_id
for update nowait;
ctt cur_ttx_temp%rowtype;
begin
open cur_ttx_temp;
fetch cur_ttx_temp into ctt;
if ((ctt.owner=p_owner) or
(ctt.owner is null and p_owner is null))
and ((ctt.object_name=p_object_name) or
(ctt.object_name is null and p_object_name is null))
and ((ctt.subobject_name=p_subobject_name) or
(ctt.subobject_name is null and p_subobject_name is null))
and ((ctt.object_id=p_object_id) or
(ctt.object_id is null and p_object_id is null))
and ((ctt.data_object_id=p_data_object_id) or
(ctt.data_object_id is null and p_data_object_id is null))
and ((ctt.object_type=p_object_type) or
(ctt.object_type is null and p_object_type is null))
and ((ctt.created=p_created) or
(ctt.created is null and p_created is null))
and ((ctt.last_ddl_time=p_last_ddl_time) or
(ctt.last_ddl_time is null and p_last_ddl_time is null))
and ((ctt.timestamp=p_timestamp) or
(ctt.timestamp is null and p_timestamp is null))
and ((ctt.status=p_status) or
(ctt.status is null and p_status is null))
and ((ctt.temporary=p_temporary) or
(ctt.temporary is null and p_temporary is null))
and ((ctt.generated=p_generated) or
(ctt.generated is null and p_generated is null))
and ((ctt.secondary=p_secondary) or
(ctt.secondary is null and p_secondary is null)) then
x_message := g_ok;
else
x_message := g_changed;
end if;
close cur_ttx_temp;
exception
when others then
x_message := substrb(sqlcode||'/'||sqlerrm,1,200);
if cur_ttx_temp%isopen then
close cur_ttx_temp;
end if;
end;
procedure update_row
(
x_message out nocopy varchar2,
p_id in number,
p_owner in varchar2,
p_object_name in varchar2,
p_subobject_name in varchar2,
p_object_id in number,
p_data_object_id in number,
p_object_type in varchar2,
p_created in date,
p_last_ddl_time in date,
p_timestamp in varchar2,
p_status in varchar2,
p_temporary in varchar2,
p_generated in varchar2,
p_secondary in varchar2
) is
begin
update ttx_temp
set
owner = p_owner,
object_name = p_object_name,
subobject_name = p_subobject_name,
object_id = p_object_id,
data_object_id = p_data_object_id,
object_type = p_object_type,
created = p_created,
last_ddl_time = p_last_ddl_time,
timestamp = p_timestamp,
status = p_status,
temporary = p_temporary,
generated = p_generated,
secondary = p_secondary
where id=p_id;
if sql%rowcount > 0 then
x_message := g_ok;
else
x_message := g_no_row;
end if;
exception
when others then
x_message := substrb(sqlcode||'/'||sqlerrm,1,200);
end;
procedure delete_row
(
p_id in number,
x_message out nocopy varchar2
) is
begin
delete ttx_temp
where id=p_id;
if sql%rowcount > 0 then
x_message := g_ok;
else
raise no_data_found;
end if;
exception
when others then
x_message := substrb(sqlcode||'/'||sqlerrm,1,200);
end;
end ttx_temp_pkg;
/
begin
--old.xxx 在调用时用具体的初始值替代,在Oracle Form和Delphi中都支持Old和New的模式,
--在JAVA中我不太清楚,应该是有办法的
ttx_temp_pkg.lock_row
(
x_message => v_message,
p_id => id,
p_owner => old.owner,
p_object_name => old.object_name,
p_subobject_name => old.subobject_name,
p_object_id => old.object_id,
p_data_object_id => old.data_object_id,
p_object_type => old.object_type,
p_created => old.created,
p_last_ddl_time => old.last_ddl_time,
p_timestamp => old.timestamp,
p_status => old.status,
p_temporary => old.temporary,
p_generated => old.generated,
p_secondary => old.secondary
);
if v_message = 'OK' then
ttx_temp_pkg.update_row
(
x_message => v_message,
p_id => id,--用具体的值代替
p_owner => new.owner,
p_object_name => new.object_name,
p_subobject_name => new.subobject_name,
p_object_id => new.object_id,
p_data_object_id => new.data_object_id,
p_object_type => new.object_type,
p_created => new.created,
p_last_ddl_time => new.last_ddl_time,
p_timestamp => new.timestamp,
p_status => new.status,
p_temporary => new.temporary,
p_generated => new.generated,
p_secondary => new.secondary
);
if v_message = 'OK' then
--数据更新成功
else
--数据更新失败
end if;
else
--锁定行出错,具体信息为:v_message
end if;
end;
update ttx_temp t
set t.subobject_name=new.subobject_name
where t.rowid=_rowid--用具体的值代替
and t.id=old.id
and nvl(t.owner,'"$!')=nvl(old.owner,'"$!')
and nvl(t.object_name,'"$!')=nvl(old.object_name,'"$!')
and nvl(t.object_id,'"$!')=nvl(old.object_id,'"$!')
and nvl(t.data_object_id,'"$!')=nvl(old.data_object_id,'"$!')
and nvl(t.object_type,'"$!')=nvl(old.object_type,'"$!')
and nvl(t.created,'"$!')=nvl(old.created,'"$!')
and nvl(t.last_ddl_time,'"$!')=nvl(old.last_ddl_time,'"$!')
and nvl(t.timestamp,'"$!')=nvl(old.timestamp,'"$!')
and nvl(t.status,'"$!')=nvl(old.status,'"$!')
and nvl(t.temporary,'"$!')=nvl(old.temporary,'"$!')
and nvl(t.generated,'"$!')=nvl(old.generated,'"$!')
and nvl(t.secondary,'"$!')=nvl(old.secondary,'"$!');来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/621535/viewspace-911777/ ,如需转载,请注明出处,否则将追究法律责任。