copy from http://www.goldenbug.net/2009/06/03/proper-ibatis-transaction-code-pattern/
Hi everyone,
Reading through the original thread, there is more than one person
confused by sessions and transactions, so here's some clarification.
There are four levels of control you can have over iBATIS
transactions. They are:
#1 First is single statement, automatic transaction. In this case,
you execute one statement that gets committed (or rolled back)
automatically by iBATIS. This behaves like JDBC "auto-commit", but it
definitely doesn't use it. iBATIS always explicitly calls
commit/rollback. An example of this single statement approach is:
sqlMapper.insert("insertPerson",somePerson);
You don't need a try/finally block or anything. It's all self
contained. This is one transaction.
#2 The second is multi-statement manual transaction mode. In this
case you have multiple statements you want to succeed or fail as a
group. If one statement fails, they all get rolled back. This is the
most common case for update statements, but can also improve the
performance and consistency of multiple queries in some cases. The
example is:
try {
sqlMapper.startTransaction();
sqlMapper.insert("insertPerson",somePerson);
sqlMapper.update("updatePerson",someOtherPerson);
sqlMapper.delete("deletePerson",anotherPerson);
sqlMapper.commitTransaction();
} finally {
sqlMapper.endTransaction();
}
There's no explicit call to rollback(). iBATIS knows if you've called
commit() indicating a successful transaction, otherwise it calls
rollback. This allows you to use try/finally semantics instead of
try/catch (which can get messy). You must ensure that this is always
in a try/finally block as above, otherwise you risk connection leaks.
#3 The third approach is to manage sessions manually. In the last two
cases sessions were managed automatically. Sometimes you need to do
this for greater control of the broader iBATIS usage scope, but you
might also use it to pass your own connection to iBATIS (openSession()
can take a connection as a parameter). Here's an example:
SqlMapSession session = sqlMap.openSession()
try {
session.startTransaction()
session.insert("insertPerson",somePerson);
session.update("updatePerson",someOtherPerson);
session.delete("deletePerson",anotherPerson);
session.commitTransaction();
} finally {
try {
session.endTransaction();
} finally {
session.close();
}
// Generally your session scope would be in a wider context and therefore the
// ugly nested finally block above would not be there. Realize that sessions
// MUST be closed if explicitly opened (via openSession()).
}
As you can see, there's definitely more work and more code involved
with managing sessions manually...it's therefore pretty rare. If you
do, you'll usually hide it in some abstract class or perhaps even in a
separate layer of the application. As the comment above says, doing
so will also avoid the need for a nested try/finally block (in C#
using blocks make this a lot cleaner too!).
#4 Finally, there's 3rd Party session/transaction management. This is
the case if you're using Spring DAO, iBATIS DAO, or some other higher
level persistence framework that has an iBATIS "plug-in". I won't
bother with an example, as you're better off looking them up. The one
we generally recommend is Spring DAO.
I hope that helps. This is all documented both in the JavaDocs and
the user guide (this is almost a cut and paste from the javadoc). Let
us know if and how we can make it more clear.
Cheers,
Clinton
分享到:
相关推荐
解决 The connected J-Link is defective,Proper operation cannot be guaranteed keil5版本比较高时debug会出现以上错误,替换安装文件下的segger 可以解决
### Spring与iBATIS集成应用详解 #### 一、目的 本文旨在介绍如何在Spring框架中集成使用iBATIS数据库层。数据库编程涉及到数据库连接、连接池管理、SQL语句执行、输入输出处理以及事务管理等多个方面。Java的JDBC...
FCKeditor the sever didn't send back a proper XML response,Please contact your system administrator.XML request error:OK(200)
对于模式串P,部分匹配表LPS(Longest Proper Prefix which is also Suffix)记录了每一个位置i之前形成的最长公共前后缀的长度。例如,模式串"ABABCAB"的部分匹配表为[0, 0, 1, 2, 0, 3, 0]。在实际匹配过程中,...
Moments as projections of an image’s intensity onto a proper polynomial basis can be applied to many different aspects of image processing. These include invariant pattern recognition, image ...
POD(Proper Orthogonal Decomposition,正交分解)是一种在工程领域得到广泛应用的强大且优雅的数据分析方法。POD能够在高维过程中获得低维的近似描述,特别是为了从实验或数值模拟过程中获得的数据集提供模态分解...
PROPER函数.xls
一份描述网口Bob Smith的电路,对于了解此电路的功能有很大的帮助
SPOD() is a Matlab implementation of the frequency domain form of proper orthogonal decomposition (POD, also known as principle component analysis or Karhunen-Loève decomposition) called spectral ...
AVG_Anti_Spyware_v75_PROPER_Patcher(转载) 看韩剧www.pigkrtv.com
《PROPER:概率程序终止性与正确性分析工具》 概率程序是一种结合了概率推理模型与图灵完备编程语言的编程范式,它能够有效地处理包含不确定性的复杂计算问题。在软件工程领域,对概率程序进行分析至关重要,因为...
标题 "PyPI 官网下载 | use-proper-hosting-1.0.1.tar.gz" 指示了这是一个从Python Package Index (PyPI) 官方网站获取的软件包,名为 "use-proper-hosting",其版本号为1.0.1,打包格式为tar.gz。PyPI是Python社区...
[U, S, V] = POD(X) returns the proper orthogonal decomposition
the proper number_work.c
**Proper:命题化框架与Java的不解之缘** 标题中的"Proper-开源"揭示了一个基于Java的开源项目,名为Proper。这个框架专注于命题化的数据生成,特别是在关系数据库的背景下。它不仅提供了一种生成命题数据的方法,...
proper.xsd
These are the guiding principles of Python, but are open to interpretation. A sense of humor is required for their proper interpretation.
【标题】"Spectral Proper Orthogonal Decomposition (SPOD) in Matlab" Spectral Proper Orthogonal Decomposition(SPOD)是一种数据驱动的统计方法,主要用于处理时间序列或空间序列数据,尤其在流体动力学、...
`eslint-plugin-proper-ternary` 是一个针对 JavaScript 代码风格检查的 ESLint 插件,主要用于规范三元运算符的使用。ESLint 是一个广泛使用的静态代码分析工具,可以帮助开发者发现并修复代码中的潜在问题,提升...