如果事务中只有一个远程数据库,则它不是分布式事务,因此它的行为与本地事务相同(与跨多个远程数据库的分布式事务相反)。以下示例显示了如何获取正确的行级锁并在单个工作单元中移动数据,并具有所需的数据一致性。
create table a_rem ( aid number, acontent varchar2(10) );
create table b_rem ( bid number, aid number, bcontent varchar(10) );
create table a_loc ( aid number, acontent varchar2(10) );
create table b_loc ( bid number, aid number, bcontent varchar(10) );
insert into a_rem values ( 1, 'A-One' );
insert into a_rem values ( 2, 'A-Two' );
insert into a_rem values ( 3, 'A-Three' );
insert into b_rem values ( 1, 1, 'B-One' );
insert into b_rem values ( 2, 1, 'B-Two' );
insert into b_rem values ( 3, 2, 'B-Three' );
insert into b_rem values ( 4, 3, 'B-Four' );
insert into b_rem values ( 5, 3, 'B-Five' );
commit;
-- look Ma, no data integrity! :(
-- let us pretend I want to move a_rem.aid = 2 information from both tables
declare
cursor row_level_locks is
select a.*, b.*
from a_rem a, b_rem b
where a.aid = b.aid and a.aid = 2
for update;
begin
open row_level_locks; -- begins transaction, obtains proper row level locking
insert into a_loc select * from a_rem where aid = 2;
insert into b_loc select * from b_rem where aid = 2;
delete a_rem where aid = 2;
delete b_rem where aid = 2;
commit;
close row_level_locks;
end;
最后说明:如果两个表之间的数据完整性很重要,请继续在 B_REM 引用 A_REM 上创建外键约束。如果您不能/不会,那么数据完整性并不重要。确保它不会被采取既重要又微不足道的步骤。