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

相关文章

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

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

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

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

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

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

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

Spring Boot 整合 MyBatis 连接数据库及常见问题

《SpringBoot整合MyBatis连接数据库及常见问题》MyBatis是一个优秀的持久层框架,支持定制化SQL、存储过程以及高级映射,下面详细介绍如何在SpringBoot项目中整合My... 目录一、基本配置1. 添加依赖2. 配置数据库连接二、项目结构三、核心组件实现(示例)1. 实体类2. Ma

Linux修改pip和conda缓存路径的几种方法

《Linux修改pip和conda缓存路径的几种方法》在Python生态中,pip和conda是两种常见的软件包管理工具,它们在安装、更新和卸载软件包时都会使用缓存来提高效率,适当地修改它们的缓存路径... 目录一、pip 和 conda 的缓存机制1. pip 的缓存机制默认缓存路径2. conda 的缓

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、

Redis解决缓存击穿问题的两种方法

《Redis解决缓存击穿问题的两种方法》缓存击穿问题也叫热点Key问题,就是⼀个被高并发访问并且缓存重建业务较复杂的key突然失效了,无数的请求访问会在瞬间给数据库带来巨大的冲击,本文给大家介绍了Re... 目录引言解决办法互斥锁(强一致,性能差)逻辑过期(高可用,性能优)设计逻辑过期时间引言缓存击穿:给

查看Oracle数据库中UNDO表空间的使用情况(最新推荐)

《查看Oracle数据库中UNDO表空间的使用情况(最新推荐)》Oracle数据库中查看UNDO表空间使用情况的4种方法:DBA_TABLESPACES和DBA_DATA_FILES提供基本信息,V$... 目录1. 通过 DBjavascriptA_TABLESPACES 和 DBA_DATA_FILES

Java实现数据库图片上传与存储功能

《Java实现数据库图片上传与存储功能》在现代的Web开发中,上传图片并将其存储在数据库中是常见的需求之一,本文将介绍如何通过Java实现图片上传,存储到数据库的完整过程,希望对大家有所帮助... 目录1. 项目结构2. 数据库表设计3. 实现图片上传功能3.1 文件上传控制器3.2 图片上传服务4. 实现