`
midstr
  • 浏览: 31974 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

理解 SET CHAINED command not allowed within multi-statement transaction. (zz)

阅读更多
在 Sybase ASE 实际应用中,特别是在 ASE + J2EE? 应用中,较容易出现 SET CHAINED command not allowed within multi-statement transaction.的异常(尽管到版本 15.0.1 为止,ASE 并不支持异常机制,但本文为了方便,统一采用“异常”一词)。有的开发人员认为这是 Sybase 数据库的问题;有的认为是多次调用 setAutoCommit() 方法的问题;有的开发人员则认为这是 jConnect 的问题,甚至从 jConnect 的代码上直接屏蔽此异常。
  然而,SET CHAINED 异常倒底是怎样产生的?
  一、数据库层
  首先,让我们看看 set chained。下面的文字片段摘自《ASE 12.5.2 Reference Manual: Commands》,Page 430:
chained
begins a transaction just before the first data retrieval or data modification
statement at the beginning of a session and after a transaction ends. In
chained mode, Adaptive Server implicitly executes a begin transaction
command before the following statements: delete, fetch, insert, lock table,
open, select, and update. You cannot execute set chained within a transaction.
  从此段文字可以得知,当 set chained on 后,delete、fetch、insert、lock table、open、select 以及 update 语句将自动启动一个事务,并要求显式的完成事务,即明确地调用 commit/rollback。同时,在事务中,不允许设置 chained 模式。
  下面的 sql 代码片断将说明在数据库层上 SET CHAINED 错误信息是如何产生的。
1> set chained on
2> go
1> set chained on
2> go
1> begin tran
2> go
1>
  似乎多次调用 set chained 并不会产生异常。接下来,
1> set chained on
2> go
Msg 226, Level 16, State 1:
Server 'FLYBEAN', Line 1:
SET CHAINED command not allowed within multi-statement transaction.
1> set chained off
2> go
Msg 226, Level 16, State 1:
Server 'FLYBEAN', Line 1:
SET CHAINED command not allowed within multi-statement transaction.
1>
  显然,处于事务环境下,调用 set chained 是会发生异常的,这一点手册上也非常明确的指出了。但为什么前面的片断中两次连续调用 set chained 却不会产生异常呢?请注意文档上这一句:Adaptive Server implicitly executes a begin transaction command before the following statements:
  重建一个数据库连接,从头开始:
1> set chained on
2> go
1> select 1
2> go
-----------
1
(1 row affected)
1> set chained on
2> go
Msg 226, Level 16, State 1:
Server 'FLYBEAN', Line 1:
SET CHAINED command not allowed within multi-statement transaction.
1> set chained off
2> go
Msg 226, Level 16, State 1:
Server 'FLYBEAN', Line 1:
SET CHAINED command not allowed within multi-statement transaction.
1>
  在执行 select 1 之前,数据库自动启动了一笔事务,因此不能再执行 set chained。接下来,完成隐式启动的事务:
1> rollback
2> go
1> set chained off
2> go
1>
  二、J2EE 层
  J2EE 应用中,一些轻量级的数据访问层实现采用 Connection 的setAutoCommit(false) + commit()/rollback() 的方式来管理事务。通过对 jConnect 的反编译以及对 spt_mda 数据的分析,可以得知 setAutoCommit(true) = SET CHAINED OFF;setAutoCommit(false) = SET CHAINED ON,下图以顺序图展示调用 setAutoCommit()  方法时,实际发生的交互。

理解 SET CHAINED command not allowed within multi-statement transaction.


  另一方面,J2EE 应用中大多采用了连接池。应用在调用 Connection.close() 方法时,实际上并没有真正地关闭连接,而是将连接回收到池中。假设连接的初态是 chained off。如果应用在取得连接后调用该连接的 setAutoCommit(false) 方法来启动事务,在未完成事务的情况下,通过 close() 方法回到池中,则当该连接下一次被取出并调用 setAutoCommit(false) 方法时就会抛出异常。见下图:

  通过上面的分析,理解了产生此异常的原因,就很容易避免此异常,即调用了 setAutoCommit(false) 后,必须显示地完成事务,即使只是执行了一个select语句。同时,关闭连接前,应显式地调用 setAutoCommit(true)。或许有的程序员会认为麻烦,但别忘记“完壁归赵”是资源借用者的义务。
分享到:
评论

相关推荐

    dpdk-18.02.2-stable

    they can request that the EAL instead uses preferred values provided by the PMD, falling back to defaults within the EAL if the PMD does not provide any. The provision of such tuned values now ...

    proxychains-ng-4.14.tar.xz

    * Proxy chain: user-defined list of proxies chained together. Usability : * Run any program through proxy server. * Access the Internet from behind a restrictive firewall. * Hide your IP * Run ...

    Ajax-chained-combobox.zip

    Ajax-chained-combobox.zip,带有laravel和vuejs的简单链式组合框,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建动态网页,其中网页的小部分在不重新加载...

    pandas_chained_logging-0.1.0-py3-none-any.whl

    Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、...

    前端项目-jquery-chained.zip

    前端项目-jquery-chained,简单的链接选择。您可以从两个不同的版本中进行选择。普通版本使用select选项的类名来决定子select的内容。它不进行任何外部Ajax(j)查询。远程版本进行外部查询,并从返回的JSON响应生成...

    工作流参考模型.pdf

    3.7.2 模型 1 —— 链锁式(Chained)............ ......... - 35 - 3.7.3. 模型 2 —— 子过程嵌套(Nested Subprocesses)................................ ............ - 36 - 3.7.4. 模型 3 —— P2P ...

    EurekaLog_7.5.0.0_Enterprise

    4)....Added "--el_injectjcl", "--el_createjcl", and "--el_createdbg" command-line options for ecc32/emake to inject JEDI/JCL debug info, create .jdbg file, and create .dbg file (Microsoft debug format...

    DPDK-18.08-rc03

    they can request that the EAL instead uses preferred values provided by the PMD, falling back to defaults within the EAL if the PMD does not provide any. The provision of such tuned values now ...

    Salient Object Detection with Chained Multi-Scale Fully Convolutional Network

    Salient Object Detection with Chained Multi-Scale Fully Convolutional Network

    Springer Press- High performance Packet Switching Architectures(2007)

    1.4 Multi-stage Switching ............................................................................. 19 1.4.1 Architecture Choice.......................................................................

    i18next-chained-backend:一个i18next后端,用于链接多个后端(添加后备,缓存等)

    $ npm install i18next-chained-backend 接线: import i18next from 'i18next' ; import Backend from 'i18next-chained-backend' ; i18next . use ( Backend ) . init ( i18nextOptions ) ; 与所有模块一样,...

    jquery.chained.remote

    jquery.chained.remote 使用于juqery的级联更新,

    intellij-idea-generate-chained-accessors:从 code.google.compintellij-idea-generate-chained-accessors 自动导出

    为当前类的每个字段创建 getter、setter 和 builder 方法,从谷歌代码自动导入。 会得到改善。 仅供个人使用。

    Daisy Chain AVB

    This document discuss how AVB daisy-chaining working,and show an example daisy-chained network.

    双向LSTM程序_BiLSTM.rar

    一个双向LSTM程序 Long Short Term... All RNNs have a chained form of repetitive neural network modules. In the standard RNN, this repetitive module has only a very simple structure, such as a tanh layer.)

    Multi-object-Tracking-paper-code-list

    Chained-Tracker:链接成对的注意力回归结果,以进行端到端联合多对象检测和跟踪 ECCV2020 -- 细分作为有效在线多对象跟踪和细分的点 ECCV2020口头 mots MAT:运动感知多对象跟踪 arXiv 2020.9.18 在一步式...

    janusgraph-images:JanusGraph Docker映像

    JanusGraph Docker映像 用于构建和发布映像的存储库。 建造 build-images.sh脚本将为版本化文件夹目录中的所有Dockerfile构建build-images.sh映像。 出版 push-images.sh脚本将为push-... 通常, chained.property.

    MOT-CTracker:ECCV 2020论文代码(聚光灯)“ Chained-Tracker

    CTracker(ECCV2020焦点) 链式跟踪器的PyTorch中的实现,如链式跟踪器中所述。 CTracker的介绍视频已上传到 。 该代码已通过PyTorch 0.4.1进行了测试。 它可能无法与其他版本一起运行。有关MOT挑战测试仪的视频演示...

    Deploying to OpenShift(True PDF)

    Author Graham Dumpleton provides the knowledge you need to make the best use of the OpenShift container platform to deploy not only your cloud-native applications, but also more traditional stateful ...

    fb_sys_fops.rar_Chained

    Chained IRQ handlers support.

Global site tag (gtag.js) - Google Analytics