TimesTen 应用层数据库缓存学习:14. 用户自定义(User Managed)缓存

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

在TimesTen中,read-only, AWT, SWT都属于系统管理的缓存组,而User managed cache group则是用户自定义的缓存组。

When TimesTen manages operations for user managed cache groups, it connects to the Oracle database using the current user’s credentials as the user name and the OraclePwd connection attribute as the Oracle password. TimesTen does not connect to the Oracle database with the cache administration user name and password set with the ttCacheUidPwdSet built-in procedure for user managed cache group operations.

可以定义的属性为:
- READONLY,缺省为可写
- 同步PROPAGATE ,缺省为不同步到Oracle
- AUTOREFERSH
- LOAD CACHE GROUP, REFRESH CACHE GROUP, and FLUSH CACHE GROUP

User Managed缓存组仍然需要主键。
补充一点,User Managerd缓存组的Propagate行为和SWT是一致的,都是先在Oracle中提交,然后在TimesTen中提交。
如果Oracle中失败,则TimesTen端需要手工rollback;如果Oracle中成功而TimesTen中失败,需要手工将TimesTen中的数据补齐,例如用ttCachePropagateFlagSet。
描述如下:

Since the operations in the transaction are applied to tables in both the TimesTen and Oracle databases, the process for committing is as follows:
After the operations are propagated to the Oracle database, the commit is first attempted in the Oracle database.

If an error occurs when applying the operations on the tables in the Oracle database, then all operations are rolled back on the tables on the Oracle database. If the commit fails in the Oracle database, the commit is not attempted in the TimesTen database and the application must roll back the TimesTen transaction. If the user tries to execute another statement, an error displays informing them of the need for a rollback. As a result, the Oracle database never misses updates committed in TimesTen.

If the commit succeeds in the Oracle database, the commit is attempted in the TimesTen database.

If the transaction successfully commits on the Oracle database, the user’s transaction is committed on TimesTen (indicated by the commit log record in the transaction log) and notifies the application. If the application ends abruptly before TimesTen informs it of the success of the local commit, TimesTen is still able to finalize the transaction commit on TimesTen based on what is saved in the transaction log.
If the transaction successfully commits on the Oracle database and a failure occurs before returning the status of the commit on TimesTen, then no record of the successful commit is written into the transaction log and the transaction is rolled back.

If the commit fails in TimesTen, an error message is returned from TimesTen indicating the cause of the failure. You then need to manually resynchronize the cache tables with the Oracle Database tables.

了解User Manager缓存组的结果可以借助于SQL Developer,见下图:

其中,all tables readonly类似于只读缓存组,Propagate all tables类似于AWT,而varies from table to table则完全是自定义了。

all tables readonly

和read only缓存组不同的一点是,如果缓存组涉及多表,则可以单独指定某些表为readonly。

SQL>
CREATE TABLE active_customer
(custid NUMBER(6) NOT NULL PRIMARY KEY,
name VARCHAR2(50),
addr VARCHAR2(100),
zip VARCHAR2(12),
region VARCHAR2(12) DEFAULT 'Unknown');
SQL> grant select on active_customer to cacheadm;Grant succeeded.
insert into active_customer values(1, 'A', 'Beijing', '100036', 'CHINA');
insert into active_customer values(2, 'B', 'Shanghai', '122222', 'CHINA');
commit;cacheadm>
CREATE DYNAMIC USERMANAGED CACHE GROUP "UM_DRO" FROM"TTHR"."ACTIVE_CUSTOMER" ("CUSTID" NUMBER(6)          NOT NULL,"NAME"   VARCHAR2(50 BYTE) ,"ADDR"   VARCHAR2(100 BYTE),"ZIP"    VARCHAR2(12 BYTE) ,"REGION" VARCHAR2(12 BYTE) ,PRIMARY KEY("CUSTID"), READONLY)AGING LRU ON
cacheadm>cachegroupsCache Group CACHEADM.UM_DRO:Cache Group Type: User Managed (Dynamic)Autorefresh: NoAging: LRU onRoot Table: TTHR.ACTIVE_CUSTOMERTable Type: Read Only1 cache group found.tthr>select * from active_customer;
0 rows found.
tthr>select * from active_customer where custid = 1; <- 满足dynamic load
< 1, A, Beijing, 100036, CHINA >
1 row found.tthr>insert into active_customer values(3, 'C', 'Guangzhou', '133333', 'CHINA');8225: Table ACTIVE_CUSTOMER is read only
The command failed.

Propagate all tables

和SWT缓存组很类似,无需rep agent,在propagate时,直接利用schema用户访问Oracle,因此也无需再Oracle中赋予更新的权限给cache admin用户。

CREATE USERMANAGED CACHE GROUP "UM_AWT" FROM"TTHR"."ACTIVE_CUSTOMER" ("CUSTID" NUMBER(6)          NOT NULL,"NAME"   VARCHAR2(50 BYTE) ,"ADDR"   VARCHAR2(100 BYTE),"ZIP"    VARCHAR2(12 BYTE) ,"REGION" VARCHAR2(12 BYTE)  DEFAULT 'Unknown',PRIMARY KEY("CUSTID"), PROPAGATE)
cacheadm>cachegroups;Cache Group CACHEADM.UM_AWT:Cache Group Type: User ManagedAutorefresh: NoAging: No aging definedRoot Table: TTHR.ACTIVE_CUSTOMERTable Type: Propagate1 cache group found.
cacheadm>cacheadm>load cache group UM_AWT where custid = 1 commit every 256 rows;
1 cache instance affected.
cacheadm>load cache group UM_AWT commit every 256 rows;
1 cache instance affected.tthr>select * from active_customer;
< 1, A, Beijing, 100036, CHINA >
< 2, B, Shanghai, 122222, CHINA >
2 rows found.
tthr>insert into active_customer values(3, 'C', 'Guangzhou', '133333', 'CHINA');
1 row inserted.SQL> set linesize 200;
SQL> select * from active_customer;CUSTID NAME                                               ADDR                                                                                                 ZIP REGION
---------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- ------------ ------------3 C                                                  Guangzhou                                                                                            133333       CHINA1 A                                                  Beijing                                                                                              100036       CHINA2 B                                                  Shanghai                                                                                             122222       CHINASQL> insert into active_customer values(4, 'D', 'Nanjing', '144444', 'CHINA');1 row created.SQL> commit;Commit complete.
tthr>select * from active_customer where custid = 4;
0 rows found.
# 需要手工load,而不能自动refresh(因为没有定义auto refresh,见下例)
cacheadm>load cache group UM_AWT commit every 256 rows;
1 cache instance affected.tthr>select * from active_customer where custid = 4;
< 4, D, Nanjing, 144444, CHINA >
1 row found.

 varies from table to table 

完全自定义,最常见的组合是Propaget+autorefresh,以及手工flush(no propagate)+ autorefresh

Propaget+autorefresh

SQL>
delete from active_customer;
insert into active_customer values(1, 'A', 'Beijing', '100036', 'CHINA');
insert into active_customer values(2, 'B', 'Shanghai', '122222', 'CHINA');
commit;CREATE USERMANAGED CACHE GROUP "UM_FLUSH_AR" 
AUTOREFRESH MODE INCREMENTAL INTERVAL 5 SECONDS
STATE ONFROM"TTHR"."ACTIVE_CUSTOMER" ("CUSTID" NUMBER(6)          NOT NULL,"NAME"   VARCHAR2(50 BYTE) ,"ADDR"   VARCHAR2(100 BYTE),"ZIP"    VARCHAR2(12 BYTE) ,"REGION" VARCHAR2(12 BYTE)  DEFAULT 'Unknown',PRIMARY KEY("CUSTID"), PROPAGATE)cacheadm>cachegroups;Cache Group CACHEADM.UM_AWT:Cache Group Type: User ManagedAutorefresh: YesAutorefresh Mode: IncrementalAutorefresh State: OnAutorefresh Interval: 5 SecondsAutorefresh Status: okAging: No aging definedRoot Table: TTHR.ACTIVE_CUSTOMERTable Type: Propagate1 cache group found.# autorefresh生效,autorefresh state 如果设为off,则看不到数据,初始化时必须手工refresh或load
tthr>select * from active_customer;
< 1, A, Beijing, 100036, CHINA >
< 2, B, Shanghai, 122222, CHINA >
2 rows found.
# propagate生效
tthr>insert into active_customer values(3, 'C', 'Guangzhou', '133333', 'CHINA');
1 row inserted.SQL> select * from active_customer;CUSTID NAME                                               ADDR                                                                                                 ZIP REGION
---------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- ------------ ------------3 C                                                  Guangzhou                                                                                            133333       CHINA1 A                                                  Beijing                                                                                              100036       CHINA2 B                                                  Shanghai                                                                                             122222       CHINA# autorefresh生效
SQL> insert into active_customer values(4, 'D', 'Nanjing', '144444', 'CHINA');1 row created.SQL> commit;Commit complete.
tthr>select * from active_customer where custid = 4;
< 4, D, Nanjing, 144444, CHINA >
1 row found.

flush(人工 propagate + no autorefresh)

这种方式是全人工方式,双向的数据传递依靠手工load/refreh和flush
flush和propagate的区别是,flush不传递delete,只传递insert和update。
可是为何这么设计呢?
Oracle的Chris Jenkins是这么解释的:

User managed cache groups do not ‘track’ changes to data within TimesTen (unlike AWT for example) so TimesTen does not knwo what data has changed. When a FLUSH is done one or more large MERGE operatiosn are performed from TimesTen to Oracle in order to insert new rows and update existing rows that have changed. Since deleted rows no longer exist in TimesTen there is no way for us to ‘propagate’ those deletes to oracle. We could of cpourse examine every row in the Oracle tabel to see if it still exists in TimesTen and if not delete it; but imagine how long that could take if the Oracle table is at all large.

User Managed Cache groups are really a special usage ‘corner case’. For most purposes AWT is what you will want to use.

确实如此,数据都在TimesTen中删除了,也就没有必要传递到后端了。

CREATE USERMANAGED CACHE GROUP "UM_FLUSH" FROM"TTHR"."ACTIVE_CUSTOMER" ("CUSTID" NUMBER(6)          NOT NULL,"NAME"   VARCHAR2(50 BYTE) ,"ADDR"   VARCHAR2(100 BYTE),"ZIP"    VARCHAR2(12 BYTE) ,"REGION" VARCHAR2(12 BYTE)  DEFAULT 'Unknown',PRIMARY KEY("CUSTID"))
8265: To use AUTOREFRESH, all the tables in the cache group must be READONLY or all the tables in the cache group must be PROPAGATE
The command failed.
cacheadm>cachegroups;Cache Group CACHEADM.UM_FLUSH:Cache Group Type: User ManagedAutorefresh: NoAging: No aging definedRoot Table: TTHR.ACTIVE_CUSTOMERTable Type: Not Propagate1 cache group found.cacheadm>load cache group UM_FLUSH commit every 256 rows;
4 cache instances affected.
tthr>insert into active_customer values(5, 'E', 'Changsha', '410000', 'CHINA');
1 row inserted.
cacheadm>flush cache group UM_FLUSH;5227: Insufficient privileges error occurred while performing an Oracle operation in OCIStmtExecute(): ORA-01031: insufficient privileges 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.
# 问题来了,对于自动Propagete,由于其利用了schema user的权限,因此无需赋予cacheadm更新的权限,而这时用cacheadm执行flush操作则出现问题。
要么还是在Oracle中赋予cacheadm更新表active_customer的权限,要么赋予schema用户tthr缓存管理的权限。
tthr>flush cache group CACHEADM.UM_FLUSH;
5 cache instances affected.# 然后在Oracle中看到了数据
SQL> select count(*) from active_customer where custid = 5;COUNT(*)
----------1# 以下演示propagete不传递delete
tthr>delete from active_customer;
5 rows deleted.
tthr>flush cache group CACHEADM.UM_FLUSH;
0 cache instances affected.
SQL> select count(*) from active_customer;COUNT(*)
----------5

User Managed Cache Group 适用的场景

User Managed缓存组非常灵活,但其适用场景似乎比较特殊,毕竟常用的已经固化成了read-only,SWT,AWT这些system managed模式。
在 Using Oracle TimesTen Application-Tier Database Cache to Accelerate the Oracle Database 提到了一个场景:

6.7 Updatable User-Managed Cache

User-Managed Cache Groups with explicit Flush are best suited for applications with frequent updates, but infrequent business transactions.
Some applications need to execute multiple updates in the cache for best performance, but need to permanently record the final transaction in the Oracle Database. An example of such an application is an eCommerce application where the application might maintain a number of shopping carts for active users. The shopping carts will be updated repeatedly in the cache. These updates need not be propagated to the Oracle Database as they are of little value. However, once a user executes a purchase, the transaction needs to be permanently recorded in the Oracle Database.
The best configuration for such data is a User-Managed updatable Cache Group where the application issues explicit Flush requests whenever it needs to record a transaction in the Oracle Database. This configuration may be coupled with Usage-Based Aging so that abandoned shopping carts get deleted from the cache automatically.

上面的例子是购物车的例子,平时都是在内存中更改,不同步到后端,最终需要持久化时才flush到后端数据库。

Flush是如何传递数据的

$ sqlplus tthr/oracle@ttorcl
create table lru_tab(a int primary key not null, b date not null);
insert into lru_tab values(1, sysdate);
grant select, insert, update on lru_tab to cacheadm;cacheadm>
CREATE USERMANAGED CACHE GROUP "UM_FLUSH" FROM"TTHR"."LRU_TAB" ("A" NUMBER(38) NOT NULL,"B" DATE       NOT NULL,PRIMARY KEY("A"))
cacheadm>cachegroups;Cache Group CACHEADM.UM_FLUSH:Cache Group Type: User ManagedAutorefresh: NoAging: No aging definedRoot Table: TTHR.LRU_TABTable Type: Not Propagate1 cache group found.在 cache group中插入4条数据,
tthr>select * from lru_tab;
< 1, 2016-04-18 22:50:20 >
< 2, 2016-04-18 23:01:23 >
< 3, 2016-04-18 23:01:33 >
< 4, 2016-04-18 23:01:43 >
4 rows found.tthr>flush cache group cacheadm.um_flush;
4 cache instances affected.在Oracle中可以看到4条数据  
SQL> select * from lru_tab;A B
---------- ---------1 18-APR-162 18-APR-163 18-APR-164 18-APR-16
SQL> delete from lru_tab;4 rows deleted.SQL> commit;Commit complete.tthr>flush cache group cacheadm.um_flush;
4 cache instances affected.在Oracle中仍可以看到这四条数据在Oracle中更改数据
SQL> update lru_tab set B='16-APR-16';4 rows updated.SQL> select * from lru_tab;A B
---------- ---------1 16-APR-162 16-APR-163 16-APR-164 16-APR-16SQL> commit;Commit complete.
tthr>flush cache group cacheadm.um_flush;
4 cache instances affected.
在oracle中的数据发生更改
SQL> select * from lru_tab;A B
---------- ---------1 18-APR-162 18-APR-163 18-APR-164 18-APR-16# 可以看到,由于没有依赖日志,已经传送的数据并非不会再次传送。在Flush时,会根据cache group中的数据与Oracle中的数据做merge,然后再决定是插入还是更新。

由于Flush操作是依赖于MERGE操作,以下给出一个merge的例子

tthr>select * from lru_tab;
< 1, 2016-04-18 22:50:20 >
< 2, 2016-04-18 23:01:23 >
< 3, 2016-04-18 23:01:33 >
< 4, 2016-04-18 23:01:43 >
4 rows found.tthr>create table a as select * from lru_tab;
4 rows inserted.
tthr>create table b as select * from lru_tab;
4 rows inserted.
tthr>merge into b using a on (a.a = b.a) > when matched then update set b.b = a.b> when not matched then insert values(a.a, a.b);
4 rows merged.
tthr>select * from b;
< 1, 2016-04-18 22:50:20 >
< 2, 2016-04-18 23:01:23 >
< 3, 2016-04-18 23:01:33 >
< 4, 2016-04-18 23:01:43 >
4 rows found.
tthr>delete from b;
4 rows deleted.
tthr>merge into b using a on (a.a = b.a)                                                                                                                                when matched then update set b.b = a.b                                                                                                                                  when not matched then insert values(a.a, a.b);
4 rows merged.
tthr>select * from b;
< 1, 2016-04-18 22:50:20 >
< 2, 2016-04-18 23:01:23 >
< 3, 2016-04-18 23:01:33 >
< 4, 2016-04-18 23:01:43 >
4 rows found.
tthr>update b set b.b = 0;2962: Inconsistent datatypes: expected DATE got NUMBER in expression (0)
The command failed.
tthr>update b set b.b = '2016-04-01';
4 rows updated.
tthr>merge into b using a on (a.a = b.a)                                                                                                                                when matched then update set b.b = a.b                                                                                                                                  when not matched then insert values(a.a, a.b);
4 rows merged.
tthr>select * from b;
< 1, 2016-04-18 22:50:20 >
< 2, 2016-04-18 23:01:23 >
< 3, 2016-04-18 23:01:33 >
< 4, 2016-04-18 23:01:43 >
4 rows found.
tthr>

这篇关于TimesTen 应用层数据库缓存学习:14. 用户自定义(User Managed)缓存的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

51单片机学习记录———定时器

文章目录 前言一、定时器介绍二、STC89C52定时器资源三、定时器框图四、定时器模式五、定时器相关寄存器六、定时器练习 前言 一个学习嵌入式的小白~ 有问题评论区或私信指出~ 提示:以下是本篇文章正文内容,下面案例可供参考 一、定时器介绍 定时器介绍:51单片机的定时器属于单片机的内部资源,其电路的连接和运转均在单片机内部完成。 定时器作用: 1.用于计数系统,可

问题:第一次世界大战的起止时间是 #其他#学习方法#微信

问题:第一次世界大战的起止时间是 A.1913 ~1918 年 B.1913 ~1918 年 C.1914 ~1918 年 D.1914 ~1919 年 参考答案如图所示

[word] word设置上标快捷键 #学习方法#其他#媒体

word设置上标快捷键 办公中,少不了使用word,这个是大家必备的软件,今天给大家分享word设置上标快捷键,希望在办公中能帮到您! 1、添加上标 在录入一些公式,或者是化学产品时,需要添加上标内容,按下快捷键Ctrl+shift++就能将需要的内容设置为上标符号。 word设置上标快捷键的方法就是以上内容了,需要的小伙伴都可以试一试呢!

AssetBundle学习笔记

AssetBundle是unity自定义的资源格式,通过调用引擎的资源打包接口对资源进行打包成.assetbundle格式的资源包。本文介绍了AssetBundle的生成,使用,加载,卸载以及Unity资源更新的一个基本步骤。 目录 1.定义: 2.AssetBundle的生成: 1)设置AssetBundle包的属性——通过编辑器界面 补充:分组策略 2)调用引擎接口API

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

大学湖北中医药大学法医学试题及答案,分享几个实用搜题和学习工具 #微信#学习方法#职场发展

今天分享拥有拍照搜题、文字搜题、语音搜题、多重搜题等搜题模式,可以快速查找问题解析,加深对题目答案的理解。 1.快练题 这是一个网站 找题的网站海量题库,在线搜题,快速刷题~为您提供百万优质题库,直接搜索题库名称,支持多种刷题模式:顺序练习、语音听题、本地搜题、顺序阅读、模拟考试、组卷考试、赶快下载吧! 2.彩虹搜题 这是个老公众号了 支持手写输入,截图搜题,详细步骤,解题必备

《offer来了》第二章学习笔记

1.集合 Java四种集合:List、Queue、Set和Map 1.1.List:可重复 有序的Collection ArrayList: 基于数组实现,增删慢,查询快,线程不安全 Vector: 基于数组实现,增删慢,查询快,线程安全 LinkedList: 基于双向链实现,增删快,查询慢,线程不安全 1.2.Queue:队列 ArrayBlockingQueue:

关于如何更好管理好数据库的一点思考

本文尝试从数据库设计理论、ER图简介、性能优化、避免过度设计及权限管理方面进行思考阐述。 一、数据库范式 以下通过详细的示例说明数据库范式的概念,将逐步规范化一个例子,逐级说明每个范式的要求和变换过程。 示例:学生课程登记系统 初始表格如下: 学生ID学生姓名课程ID课程名称教师教师办公室1张三101数学王老师101室2李四102英语李老师102室3王五101数学王老师101室4赵六103物理陈

数据库期末复习知识点

A卷 1. 选择题(30') 2. 判断范式(10') 判断到第三范式 3. 程序填空(20') 4. 分析填空(15') 5. 写SQL(25') 5'一题 恶性 B卷 1. 单选(30') 2. 填空 (20') 3. 程序填空(20') 4. 写SQL(30') 知识点 第一章 数据库管理系统(DBMS)  主要功能 数据定义功能 (DDL, 数据定义语

ROS话题通信流程自定义数据格式

ROS话题通信流程自定义数据格式 需求流程实现步骤定义msg文件编辑配置文件编译 在 ROS 通信协议中,数据载体是一个较为重要组成部分,ROS 中通过 std_msgs 封装了一些原生的数据类型,比如:String、Int32、Int64、Char、Bool、Empty… 但是,这些数据一般只包含一个 data 字段,结构的单一意味着功能上的局限性,当传输一些复杂的数据,比如: