正常情况下,一个事务只能跑在一个数据库上,也就是type-1 connect 。 但DB2支持type-2 connect,也就是一个事务中可以同时连接两个数据库。本文就是type-2连接的测试。
1.)一些说明
1.1 在被连接的两个数据库中,只能有一个是active状态,另一个是dormant状态。
1.2 一些关键字的解释与说明
- The CONNECT TO <dbname> or CONNECT TO <dbname> USER userid USING password statements establish a connection.
- The SET CONNECTION <dbname> statement changes the state of a database connection from dormant to current. It makes the specified location the current server.
- The RELEASE statement places one or more connections in the release-pending state.Note that, even though a database has been placed in release pending state, you may still CONNECT TO or SET CONNECTION to the database before the COMMIT
- The DISCONNECT statement destroys one or more connections when there is no active unit of work (after COMMIT or ROLLBACK WORK).
2.)测试如下,有两个数据库DB1和DB2,先连接到DB1,再连接到DB2,再切换到DB1,最后release并提交
$ db2set DB2OPTIONS=+c
$ db2stop
$ db2start
$ db2 "set client syncpoint twophase connect 2 sqlrules STD"
$ db2 "connect to DB1"
数据库连接信息
数据库服务器 = DB2/NT 10.5.1
SQL 授权标识 = QINGSONG
本地数据库别名 = DB1
$ db2 "select * from t1"
ID NAME
----------- ----------
1 AA
2 bb
2 条记录已选择。
$ db2 "update t1 set name='aa' where ID=1"
$ db2 "connect to DB2"
数据库连接信息
数据库服务器 = DB2/NT 10.5.1
SQL 授权标识 = QINGSONG
本地数据库别名 = DB2
$ db2 "select * from employee"
AGE
-----------
68
72
26
100
4 条记录已选择。
$ db2 "insert into employee values(300)"
$ db2 "set connection db1"
数据库连接信息
数据库服务器 = DB2/NT 10.5.1
SQL 授权标识 = QINGSONG
本地数据库别名 = DB1
$ db2 "select * from t1"
ID NAME
----------- ----------
1 aa
2 bb
2 条记录已选择。
$ db2 "select * from employee"
SQL0204N "QINGSONG.EMPLOYEE" 是一个未定义的名称。 SQLSTATE=42704
$ db2 "release all"
DB20000I SQL RELEASE 命令成功完成。
$ db2 "commit"
DB20000I SQL 命令成功完成。
$ db2 "list applications"
SQL1611W “数据库系统监视器”没有返回任何数据。
如果在测试过程查看db2 list application的输出,可以看到 在第一次connect to DB1之后,有一个应用。当connect to DB2之后,变成了两个应用。在发出release all之后,仍然是两个应用,在发出commit命令之后,所有应用断开(断开的原因是之前发出过release all的命令)