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

相关文章

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

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

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

Mysql用户授权(GRANT)语法及示例解读

《Mysql用户授权(GRANT)语法及示例解读》:本文主要介绍Mysql用户授权(GRANT)语法及示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql用户授权(GRANT)语法授予用户权限语法GRANT语句中的<权限类型>的使用WITH GRANT

数据库面试必备之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 的