您的位置:首页 > 数据库

TimesTen 应用层数据库缓存学习:7. 同步读写缓存

2016-04-15 19:07 375 查看
同步读写缓存(SWT)比较少用到,它性能肯定是不如AWT,但数据一致性强于AWT。

数据先在Oracle提交,然后才在TimesTen中提交,因此TimesTen的约束可以比Oracle弱。



和AWT一样,SWT的定义不能出现where条件

和AWT一样,虽然Oracle端可以修改,但是不建议。

定义dynamic SWT缓存

CREATE DYNAMIC SYNCHRONOUS WRITETHROUGH CACHE GROUP "D_SWT"
FROM
"TTHR"."DEPARTMENTS" (
"DEPARTMENT_ID"   NUMBER(4)         NOT NULL,
"DEPARTMENT_NAME" VARCHAR2(30 BYTE) NOT NULL,
"MANAGER_ID"      NUMBER(6)        ,
"LOCATION_ID"     NUMBER(4)        ,
PRIMARY KEY("DEPARTMENT_ID")
)
AGING LRU ON

cachaadm> cachegroups;

Cache Group CACHEADM.D_SWT:

Cache Group Type: Synchronous Writethrough (Dynamic)
Autorefresh: No
Aging: LRU on

Root Table: TTHR.DEPARTMENTS
Table Type: Propagate

1 cache group found.

cacheadm> call ttrepstart;
8191: This store (CACHEDB1_1122 on TIMESTEN-HOL) is not involved in a replication scheme
The command failed.


SWT缓存无需cache agent,因为其是在Oracle中先提交,所以AWT是复制,而SWT是两阶段提交。

上例中ttrepstart失败也说明了这一点。

tthr> select * from departments;
0 rows found.
tthr> select * from departments where department_id = 80;
< 80, Sales, 145, 2500 > <-dynamic load
1 row found.
Command> load cache group d_swt where department_id < 100 commit every 256 rows;
8 cache instances affected.
tthr>select * from departments;
< 10, Administration, 200, 1700 >
< 20, Marketing, 201, 1800 >
< 30, Purchasing, 114, 1700 >
< 40, Human Resources, 203, 2400 >
< 50, Shipping, 121, 1500 >
< 60, IT, 103, 1400 >
< 70, Public Relations, 204, 2700 >
< 80, Sales, 145, 2500 >
< 90, Executive, 100, 1700 >
9 rows found.
tthr>update departments set department_name = 'SALES' where department_id = 80;
1 row updated.
SQL> select * from departments where department_id = 80;

DEPARTMENT_ID DEPARTMENT_NAME                MANAGER_ID LOCATION_ID
------------- ------------------------------ ---------- -----------
80 SALES                                 145        2500


由于不建议在Oracle端修改数据,因此refresh操作通常意义不大,不过我们还是做了一个测试,来说明refresh的行为特征:

SQL> update departments set department_name = 'Sales' where department_id = 80;

1 row updated.

SQL> commit;

Commit complete.
cacheadm>refresh cache group d_swt where department_id = 80 commit every 256 rows;
3022: Refresh cache group with a where clause is only allowed only if the cache group is not dynamic
The command failed.
tthr>select * from departments where department_id = 80;
< 80, SALES, 145, 2500 >
1 row found.

cacheadm>refresh cache group d_swt commit every 256 rows;
9 cache instances affected.
tthr>select * from departments where department_id = 80;
< 80, Sales, 145, 2500 >
1 row found.


错误处理

在Oralce中失败,在TimesTen中成功,这时需要在TimesTen端rollback交易

If the transaction fails to commit in the Oracle database, the application must roll back the transaction in TimesTen.

tthr>set autocommit off
tthr>call ttCachePropagateFlagSet(0); <- 设置timesten中的更改不传递到Oracle
tthr>delete from departments where department_id = 80;
1 row deleted.
tthr>commit;
tthr>select * from departments;
< 10, Administration, 200, 1700 >
< 20, Marketing, 201, 1800 >
< 30, Purchasing, 114, 1700 >
< 40, Human Resources, 203, 2400 >
< 50, Shipping, 121, 1500 >
< 60, IT, 103, 1400 >
< 70, Public Relations, 204, 2700 >
< 90, Executive, 100, 1700 >
8 rows found.
tthr>call ttCachePropagateFlagSet(1);
# TimesTen中数据已删,而Oracle数据仍在
SQL> select * from departments where department_id = 80;

DEPARTMENT_ID DEPARTMENT_NAME                MANAGER_ID LOCATION_ID
------------- ------------------------------ ---------- -----------
80 Sales                                 145        2500

# TimesTen插入数据成功,但Oracle端重复索引故障
tthr>insert into departments values(80, 'Sales', 145, 2500);
1 row inserted.
tthr>commit;
5210: Oracle unique constraint violation error in OCIStmtExecute(): ORA-00001: unique constraint (TTHR.SYS_C0014410) violated rc = -1
5055: Cannot synchronize Oracle with TimesTen.  The TimesTen transaction must be rolled back.
5025: Commit failure in Oracle. Transaction must be rolled back in TimesTen.
The command failed.
tthr>select * from departments;
5025: Commit failure in Oracle. Transaction must be rolled back in TimesTen.
The command failed.
tthr>rollback; <-TimesTen端(应用端)必须执行回退操作
tthr>select * from departments;
< 10, Administration, 200, 1700 >
< 20, Marketing, 201, 1800 >
< 30, Purchasing, 114, 1700 >
< 40, Human Resources, 203, 2400 >
< 50, Shipping, 121, 1500 >
< 60, IT, 103, 1400 >
< 70, Public Relations, 204, 2700 >
< 90, Executive, 100, 1700 >
8 rows found.
这时数据已经不一致了,我们仍可以用ttCachePropagateFlagSet来将数据补齐
tthr>autocommit 0
tthr>show autocommit
autocommit = 0 (OFF)
tthr>call ttCachePropagateFlagSet(0);
tthr>insert into departments values(80, 'Sales', 145, 2500);
1 row inserted.
tthr>commit;
tthr>call ttCachePropagateFlagSet(1);
tthr>select count(*) from departments;
< 9 >
1 row found.
tthr>passthrough 3
tthr>select count(*) from departments;
< 27 >
1 row found.
tthr>select count(*) from departments where department_id < 100;
< 9 >
1 row found.
tthr>passthrough 0


如果Oracle端成功,而TimesTen端失败,那么两边的数据就不一致了,需要人工同步

If the Oracle Database transaction commits successfully but the TimesTen transaction fails to commit, the cache tables in the SWT cache group are no longer synchronized with the cached Oracle Database tables.

cacheadm>unload cache group d_swt;
9 cache instances affected.
cacheadm>load cache group d_swt where department_id < 100 commit every 256 rows;
9 cache instances affected.
# 在TimesTen端插入一条数据,并且不同步到Oracle
tthr>autocommit;
autocommit = 1 (ON)
tthr>call ttCachePropagateFlagSet(0);
tthr>select * from departments;
< 10, Administration, 200, 1700 >
< 20, Marketing, 201, 1800 >
< 30, Purchasing, 114, 1700 >
< 40, Human Resources, 203, 2400 >
< 50, Shipping, 121, 1500 >
< 60, IT, 103, 1400 >
< 70, Public Relations, 204, 2700 >
< 80, Sales, 145, 2500 >
< 90, Executive, 100, 1700 >
9 rows found.
tthr>insert into departments values(99, 'Consultant', 145, 2500);
1 row inserted.
tthr>commit;
tthr>call ttCachePropagateFlagSet(1);
tthr>autocommit 1;
tthr>insert into departments values(99, 'Consultant', 145, 2500);
907: Unique constraint (DEPARTMENTS on TTHR.DEPARTMENTS) violated at Rowid <BMUFVUAAABWAgAAFBd>
The command failed.
# Oracle端数据插入成功
SQL> select * from departments where department_id = 99;

DEPARTMENT_ID DEPARTMENT_NAME                MANAGER_ID LOCATION_ID
------------- ------------------------------ ---------- -----------
99 Consultant                            145        2500
# 由于TimesTen无法控制Oracle端,因此成功的交易也就无法回退了,这时数据不一致,需要人工补数据


Explicitly SWT的load和refresh

tthr>cachegroups;

Cache Group CACHEADM.SWT:

Cache Group Type: Synchronous Writethrough
Autorefresh: No
Aging: LRU on

Root Table: TTHR.DEPARTMENTS
Table Type: Propagate

1 cache group found.
tthr>select * from departments;
0 rows found.

# 简单的说,对于explicitly缓存组,refresh就等于unload+load,开销很大。load等于insert。
cacheadm>load cache group swt where department_id < 100 commit every 256 rows;
10 cache instances affected.
cacheadm>load cache group swt where department_id < 100 commit every 256 rows;
0 cache instances affected.
cacheadm>refresh cache group swt where department_id < 100 commit every 256 rows;
10 cache instances affected.
cacheadm>refresh cache group swt where department_id < 100 commit every 256 rows;
10 cache instances affected.
cacheadm>refresh cache group swt where department_id < 100 commit every 256 rows;
10 cache instances affected.
cacheadm>refresh cache group swt  commit every 256 rows;
28 cache instances affected.
cacheadm>load cache group swt where department_id < 100 commit every 256 rows;
0 cache instances affected.


清理cache group

就说一点,无需停rep agent就行了,因为他用不到
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: