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

相关文章

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 配置(可选)启动服务应用场景

TP-Link PDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务

《TP-LinkPDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务》近期,路由器制造巨头普联(TP-Link)在用户群体中引发了一系列重要变动,上个月,公司发出了一则通知,明确要求所... 路由器厂商普联(TP-Link)上个月发布公告要求所有用户必须完成实名认证后才能继续使用普联提供的 D

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为何移除查

Python中连接不同数据库的方法总结

《Python中连接不同数据库的方法总结》在数据驱动的现代应用开发中,Python凭借其丰富的库和强大的生态系统,成为连接各种数据库的理想编程语言,下面我们就来看看如何使用Python实现连接常用的几... 目录一、连接mysql数据库二、连接PostgreSQL数据库三、连接SQLite数据库四、连接Mo