DB2中的type-2 connect

正常情况下,一个事务只能跑在一个数据库上,也就是type-1 connect 。 但DB2支持type-2 connect,也就是一个事务中可以同时连接两个数据库。本文就是type-2连接的测试。


1.)一些说明

 1.1 在被连接的两个数据库中,只能有一个是active状态,另一个是dormant状态。

 1.2 一些关键字的解释与说明

  1.   The CONNECT TO <dbname> or CONNECT TO <dbname> USER userid USING password statements establish a connection.
  2.   The SET CONNECTION <dbname> statement changes the state of a database connection from dormant to current. It makes the specified location the current server.
  3.   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
  4.   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的命令)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值