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

2024-02-04 13:48

本文主要是介绍TimesTen 应用层数据库缓存学习:7. 同步读写缓存,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

同步读写缓存(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 ONcachaadm> cachegroups;Cache Group CACHEADM.D_SWT:Cache Group Type: Synchronous Writethrough (Dynamic)Autorefresh: NoAging: LRU onRoot Table: TTHR.DEPARTMENTSTable Type: Propagate1 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 = -15055: 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 WritethroughAutorefresh: NoAging: LRU onRoot Table: TTHR.DEPARTMENTSTable Type: Propagate1 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就行了,因为他用不到

这篇关于TimesTen 应用层数据库缓存学习:7. 同步读写缓存的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/677708

相关文章

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

数据库面试必备之MySQL中的乐观锁与悲观锁

《数据库面试必备之MySQL中的乐观锁与悲观锁》:本文主要介绍数据库面试必备之MySQL中乐观锁与悲观锁的相关资料,乐观锁适用于读多写少的场景,通过版本号检查避免冲突,而悲观锁适用于写多读少且对数... 目录一、引言二、乐观锁(一)原理(二)应用场景(三)示例代码三、悲观锁(一)原理(二)应用场景(三)示例

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

Ubuntu中远程连接Mysql数据库的详细图文教程

《Ubuntu中远程连接Mysql数据库的详细图文教程》Ubuntu是一个以桌面应用为主的Linux发行版操作系统,这篇文章主要为大家详细介绍了Ubuntu中远程连接Mysql数据库的详细图文教程,有... 目录1、版本2、检查有没有mysql2.1 查询是否安装了Mysql包2.2 查看Mysql版本2.

Oracle数据库常见字段类型大全以及超详细解析

《Oracle数据库常见字段类型大全以及超详细解析》在Oracle数据库中查询特定表的字段个数通常需要使用SQL语句来完成,:本文主要介绍Oracle数据库常见字段类型大全以及超详细解析,文中通过... 目录前言一、字符类型(Character)1、CHAR:定长字符数据类型2、VARCHAR2:变长字符数

Win11安装PostgreSQL数据库的两种方式详细步骤

《Win11安装PostgreSQL数据库的两种方式详细步骤》PostgreSQL是备受业界青睐的关系型数据库,尤其是在地理空间和移动领域,:本文主要介绍Win11安装PostgreSQL数据库的... 目录一、exe文件安装 (推荐)下载安装包1. 选择操作系统2. 跳转到EDB(PostgreSQL 的

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处

mysql数据库重置表主键id的实现

《mysql数据库重置表主键id的实现》在我们的开发过程中,难免在做测试的时候会生成一些杂乱无章的SQL主键数据,本文主要介绍了mysql数据库重置表主键id的实现,具有一定的参考价值,感兴趣的可以了... 目录关键语法演示案例在我们的开发过程中,难免在做测试的时候会生成一些杂乱无章的SQL主键数据,当我们