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中将异步调用转为同步的五种实现方法

《Java中将异步调用转为同步的五种实现方法》本文介绍了将异步调用转为同步阻塞模式的五种方法:wait/notify、ReentrantLock+Condition、Future、CountDownL... 目录异步与同步的核心区别方法一:使用wait/notify + synchronized代码示例关键

MySQL8.0设置redo缓存大小的实现

《MySQL8.0设置redo缓存大小的实现》本文主要在MySQL8.0.30及之后版本中使用innodb_redo_log_capacity参数在线更改redo缓存文件大小,下面就来介绍一下,具有一... mysql 8.0.30及之后版本可以使用innodb_redo_log_capacity参数来更改

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

MySQL数据库函数之JSON_EXTRACT示例代码

《MySQL数据库函数之JSON_EXTRACT示例代码》:本文主要介绍MySQL数据库函数之JSON_EXTRACT的相关资料,JSON_EXTRACT()函数用于从JSON文档中提取值,支持对... 目录前言基本语法路径表达式示例示例 1: 提取简单值示例 2: 提取嵌套值示例 3: 提取数组中的值注意

查询SQL Server数据库服务器IP地址的多种有效方法

《查询SQLServer数据库服务器IP地址的多种有效方法》作为数据库管理员或开发人员,了解如何查询SQLServer数据库服务器的IP地址是一项重要技能,本文将介绍几种简单而有效的方法,帮助你轻松... 目录使用T-SQL查询方法1:使用系统函数方法2:使用系统视图使用SQL Server Configu

SQL Server数据库迁移到MySQL的完整指南

《SQLServer数据库迁移到MySQL的完整指南》在企业应用开发中,数据库迁移是一个常见的需求,随着业务的发展,企业可能会从SQLServer转向MySQL,原因可能是成本、性能、跨平台兼容性等... 目录一、迁移前的准备工作1.1 确定迁移范围1.2 评估兼容性1.3 备份数据二、迁移工具的选择2.1

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查