最初由 gclizh 发布
[B]SERIALIZABLE是很好资源的[/B]
我说的这句话是错误的,在oracle中SERIALIZABLE 并不比read committed 多耗多少资源,但是会有ORA-08177错误:
SQL>set transaction isolation level serializable;
事务处理集。
SQL>select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ---------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 2000 20
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7902 FORD ANALYST 7566 03-12月-81 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择14行。
SQL>declare
2 pragma autonomous_transaction;
3 begin
4 update emp set sal=900 where empno=7369;
5 commit;
6 end;
7 /
PL/SQL 过程已成功完成。
SQL>update emp set sal=800 where empno=7369;
update emp set sal=800 where empno=7369
*
ERROR 位于第 1 行:
ORA-08177: 无法连续访问此事务处理
所以:
(从oracle 文档(b10501_01)中copy的)
Oracle's serializable isolation is suitable for environments where there is a relatively low chance that two concurrent transactions will modify the same rows and the long-running transactions are primarily read-only. It is most suitable for environments with large databases and short transactions that update only a few rows.
另外 如果使用serializable事务隔离级别 可以预防丢失更新的问题,这不同于select ... for update nowait(后者是通过加锁的物理上的串行)
应该仔细考虑是否应该使用serializable事务隔离级别。
对于使用编程缓存结果并不总是有效的做法,我举的那个例子不能用缓存,对于多句sql 分别访问不同的数据行(比如我那个例子中如果分多句sql分段读取数据)或者多次访问了不同范围或使用不同的算法计算的情况下 缓存的数据并不能有效的被使用,如果有读取一致性的要求的话使用数据库提供的serializable 事务隔离级别是最有效的做法(代码少,性能高)