Oracle TimesTen体验 – Cache Group刷新

回头看自己写的第一篇Timesten的文章,居然已经过去了1年半。事情有开始,应该就有个结束,完成它吧,即使今天碰到一万个挫折。

上回书说到,“1. 安装TimesTen 7.0 …”,现在最新版本已经是TimesTen Release 7.0.5.0.0了,这次重新安装了TimesTen,安装过程中我的手很欠,点上了本来不是默认选项的Enable Access Control,启用了用户存取权限控制,导致后面不得不查阅各个联机文档,费尽心机,才完成整个实验。当然手欠也有手欠的好处,否则谁会在刚开始实验一个新产品的时候去研究那些劳什子的安全设置呢。

2. 创建一个系统DSN,命名为ttdemo。
按照OTN上的How to Create TimesTen Databases,如果没有激活Access Control,应该一路下去很顺畅,我却卡在了第43页上,连接Datastore总是报错。

D:\Temp>ttIsql -connStr “DSN=ttdemo;UID=kamus”

Copyright (c) 1996-2008, Oracle. All rights reserved.
Type ? or “help” for help, type “exit” to quit ttIsql.
All commands must end with a semicolon character.

connect “DSN=ttdemo;UID=kamus”;
7001: User authentication failed
The command failed.
Done.

耗时良久,最终发现,是因为启用了Access Control,所以必须要在Timesten中也创建跟Oracle Database中相同的Internal User,并且赋予这个User相应的权限,才可以连接后台是Oracle数据库的Datastore。

如果执行下面的命令有同样的输出,那么就是启用了Access Control。

D:\Temp>ttVersion -m | grep access_control
access_control=1

可是如何创建这个Internal User呢?

D:\Temp>ttisql

Copyright (c) 1996-2008, Oracle. All rights reserved.
Type ? or “help” for help, type “exit” to quit ttIsql.
All commands must end with a semicolon character.

Command> create user kamus identified by oracle;
An existing connection is required to execute this command.
Try executing “connect [connection_string];” first.
The command failed.

直接在ttIsql中创建一个跟数据库用户kamus一样的TT用户,会要求先有一个连接,这个连接该是哪个呢?再去查文档。文档里面说的不清不楚,几经周折,终于发现在TT安装的过程中会自动创建一系列的DSN,其中名字是tt_tt70_32的为Instance data store,在这个datastore中可以进行TT用户的管理。

连接这个Instance data store,需要用instance administrator user用户登陆,可以通过下面的命令获得这个User的名字,在Windows系统中应该跟登陆操作系统的用户名一样。

D:\Temp>ttVersion -m | grep instance_admin
instance_admin=leyzhang

3. 创建TT User

D:\Temp>ttisql

Copyright (c) 1996-2008, Oracle. All rights reserved.
Type ? or “help” for help, type “exit” to quit ttIsql.
All commands must end with a semicolon character.

Command> connect “dsn=tt_tt70_32; UID=leyzhang”;
Connection successful: DSN=tt_tt70_32;UID=leyzhang;DataStore=C:\TimesTen\DEMODA~
1\TT_tt70_32;DatabaseCharacterSet=US7ASCII;ConnectionCharacterSet=US7ASCII;DRIVE
R=C:\TimesTen\tt70_32\bin\ttdv70.dll;Authenticate=0;PermSize=20;TypeMode=0;
(Default setting AutoCommit=1)
Command> create user kamus identified by ‘oracle’;
Command> grant ddl to kamus;
Command> connect “dsn=tt_tt70_32; UID=kamus”;
Enter password for ‘kamus’:
Connection successful: DSN=tt_tt70_32;UID=kamus;DataStore=C:\TimesTen\DEMODA~1\T
T_tt70_32;DatabaseCharacterSet=US7ASCII;ConnectionCharacterSet=US7ASCII;DRIVER=C
:\TimesTen\tt70_32\bin\ttdv70.dll;Authenticate=0;PermSize=20;TypeMode=0;
(Default setting AutoCommit=1)

4. 使用TT user连接第二步创建的Datastore

D:\Temp>ttIsql -connStr “DSN=ttdemo;UID=kamus”

Copyright (c) 1996-2008, Oracle. All rights reserved.
Type ? or “help” for help, type “exit” to quit ttIsql.
All commands must end with a semicolon character.

connect “DSN=ttdemo;UID=kamus”;
Connection successful: DSN=ttdemo;UID=kamus;DataStore=c:\TimesTen\DemoDataStores
\ttdemo;DatabaseCharacterSet=ZHS16GBK;ConnectionCharacterSet=ZHS16GBK;DRIVER=C:\
TimesTen\tt70_32\bin\ttdv70.dll;OracleId=orcl11g;LogDir=c:\TimesTen\DemoDataStor
es\log;PermSize=20;TempSize=20;TypeMode=0;
(Default setting AutoCommit=1)

我这里的测试没有要求输入kamus用户的密码,是因为在DSN中已经配置过了,加密的密码需要用ttUser -pwdcrypt命令创建。

5. 测试Passthrough。

所谓passthrough是指,TT中可以设定,当SQL语句检索的表在TT实例中不存在,那么将自动去后台的Oracle数据库中查询,并将查询的结果返回到TT中。这也是我们可以通过只读Cache Group来更新Oracle数据的方法。

Command> select * from v$version;
2206: Table KAMUS.V$VERSION not found
The command failed.
Command> autocommit 0;
Command> set passthrough 2;
Command> select * from v$version;
5220: Permanent Oracle connection failure error in OCIServerAttach(): ORA-12154
: TNS:could not resolve the connect identifier specified rc = -1
5131: Cannot connect to Oracle database: connect_string = “orcl11g”, uid = “KAM
US”, pwd is hidden
The command failed.

第一个2206错误是正常的,因为在创建DSN的时候,默认的passthrough值为0,表示不会将SQL发送到后台的Oracle数据库中,而TT实例中又没有v$version表,所以会报not found的错误。需要先将passthrough设置为1或者2或者3(每个数字的含义请自行查阅文档)。
第二个5220错误,就比较诡异了。在文档中一直描述DSN的OracleID值可以是后台Oracle数据库的SID或者是Service Name,但是实际上这个值却必须是tnsnames.ora文件中指向Oracle数据库的TNS名称。这也是为什么在TimesTen服务器中至少需要安装Oracle Client端的原因。

修改完DNS配置,再次执行,返回了数据库中的记录。

Command> select * from v$version;
< Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production >
< PL/SQL Release 11.1.0.6.0 - Production >
< CORE 11.1.0.6.0 Production >
< TNS for 32-bit Windows: Version 11.1.0.6.0 - Production >
< NLSRTL Version 11.1.0.6.0 - Production >
5 rows found.

6. 创建Readonly Cache Group

–设置cache administration user ID
Command> call ttCacheUidPwdSet(‘kamus’,’oracle’);

–启动cache agent
Command> call ttCacheStart;

–创建cache group,必须显示指定跟后台oracle数据库中表结构一样的TT表结构,Primary Key必须存在
Command> CREATE READONLY CACHE GROUP readcache
>AUTOREFRESH INTERVAL 5 SECONDS
>FROM dept
>(DEPTNO NUMBER(2,0) NOT NULL PRIMARY KEY, DNAME VARCHAR2(14), LOC VARCHAR2(13));

–将Oracle的表数据加载到TT表中
Command> LOAD CACHE GROUP readcache COMMIT EVERY 256 ROWS;
4 cache instances affected.

–检索TT表,确认数据已经加载完毕
Command> select * from dept;
< 10, ACCOUNTING, NEW YORK >
< 20, RESEARCH, DALLAS >
< 30, SALES, CHICAGO >
< 40, OPERATIONS, BOSTON >
4 rows found.

–检查cache group设置
Command> cachegroups;

Cache Group KAMUS.READCACHE:

Cache Group Type: Read Only
Autorefresh: Yes
Autorefresh Mode: Incremental
Autorefresh State: On
Autorefresh Interval: 5 Seconds

Root Table: KAMUS.DEPT
Table Type: Read Only

1 cache group found.

7. 测试Cache Group的功能

–在Oracle表中插入2条数据
SQL> insert into dept values(50,’CONSULTING’,’BEIJING’);
SQL> insert into dept values(60,’SUPPORT’,’SHANGHAI’);

–检查TT表,由于是5秒刷新间隔,几乎是立刻就可以看到TT表中已经存在这2条数据了
Command> select * from dept;
< 10, ACCOUNTING, NEW YORK >
< 20, RESEARCH, DALLAS >
< 30, SALES, CHICAGO >
< 40, OPERATIONS, BOSTON >
< 50, CONSULTING, BEIJING >
< 60, SUPPORT, SHANGHAI >
6 rows found.

–停止cache agent,此时cache group将不再会自动刷新
Command> call ttCacheStop;

–通过passthrough在TT中执行insert操作,可以看到成功插入一条记录,但是TT表中却仍然只有6条。此处passthrough必须设置为2,原因请自行查阅文档。
Command> autocommit 0;
Command> passthrough 2;
Command> insert into dept values(70,’APPS’,’NANJING’);
1 row inserted.
Command> select * from dept;
< 10, ACCOUNTING, NEW YORK >
< 20, RESEARCH, DALLAS >
< 30, SALES, CHICAGO >
< 40, OPERATIONS, BOSTON >
< 50, CONSULTING, BEIJING >
< 60, SUPPORT, SHANGHAI >
6 rows found.

–在oracle表中检索,发现记录数增加了。
SQL> select * from dept;

DEPTNO DNAME LOC
———- ————– ————-
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 CONSULTING BEIJING
60 SUPPORT SHANGHAI
70 APPS NANJING

7 rows selected.

–再次启动cache agent,重新检索TT表,发现这次记录数也增加了。
Command> call ttcachestart;
Command> select * from dept;
< 10, ACCOUNTING, NEW YORK >
< 20, RESEARCH, DALLAS >
< 30, SALES, CHICAGO >
< 40, OPERATIONS, BOSTON >
< 50, CONSULTING, BEIJING >
< 60, SUPPORT, SHANGHAI >
< 70, APPS, NANJING >
7 rows found.

最后,再给一个推荐SQL Developer的理由,还有哪个产品可以用图像化的界面管理Timesten呢?

2 thoughts on “Oracle TimesTen体验 – Cache Group刷新

Leave a Reply to bryanxu Cancel reply

Your email address will not be published. Required fields are marked *