iBatis 学习 之二 事务处理

Ibatis 事务处理 NULL 博文链接:https://kevin-zhang1983.iteye.com/blog/1111689 立即下载

这部分是和Dao紧密相联的。

我们在使用Dao时,如以下代码,先插入新记录,再进行更新:

  personDao.insertPerson (person); // Starts transaction
 person.setLastName("Begin");
 personDao.updatePerson (person); // Starts a new transaction

因为没有显式地启动事务,iBatis会认为这是两次事务,分别从连接池中取两次Connectio。
我们所写的Dao子类(继承自com.ibatis.dao.client.template.SqlMapDaoTemplate)的每一个Dao方法已经默认为一个事务(通过动态代理)。

而在业务层,应该有一个类来统管Dao子类的事务,iBatis是通过DaoManager类来作这件事的,如下:

DaoManager provides access to all DAOs it manages and also allows transactions to be committed and ended (possibly rolled back)

众Dao子类由DaoManager产生,如:

  DaoManager daoManager = DaoManagerBuilder.buildDaoManager(reader);
  UserDao userDao = (UserDao) daoManager.getDao(UserDao.class);
 
UserDao是用户自己定义的接口,获得的其实是在dao.xml中指定的相对应的 SqlMapDao实现类,从而实现了松藕合。在良好的分层设计中,
业务层(service包)只需要知道Dao接口,而不去关心其具体怎么实现。
如果显式地声明事务处理语句,如下:
  try {
    daoManager.startTransaction();
    personDao.insertPerson (person);
    person.setLastName("Begin");
    personDao.updatePerson(person);    
    otherDao.doSomething(other);
    ...
    daoManager.commitTransaction();
  } finally {
    daoManager.endTransaction();
  }

 
  这样就保持了原子性,整体为一个事务,要么全部执行成功,否则回滚。
  现在唯一的问题就是,dao层的事务是否已经放弃,否则产生事务嵌套问题对性能会有影响(杞人忧天?)
 
  当然,iBatis 完全可以这么做:建一个声明式接口:IService,再使用动态代理,将用户自己的Serivce子类通过动态代理自动包上事务处理的代码,默 认每一个业务方法为一个事务。
  大师的心如果能轻易揣测,就是不大师了:),估计大师认为这样属于过度设计,他认为把这种灵活性交给用户是合适的,相当多的service 方法只调用一个Dao方法,例如CRUD操作。
 
  再补充一下,iBatis中对事务的处理是可配置的,最常用的Type是"JDBC",也可以声明为"JTA"或"EXTERNAL".

CATIA V5新手必看:从零开始的界面导航与基础操作指南(附常见问题解决) 本文为CATIA V5新手提供全面的界面导航与基础操作指南,涵盖核心工作区解析、文件管理规范、视图操作秘籍及常见问题解决方案。通过详细的步骤说明和实用技巧,帮助用户快速掌握这一工业设计标杆软件,特别适合从SolidWorks、AutoCAD等软件过渡的用户。 阅读详情

相关推荐

DRAM知识整理系列(三):部分时序参数整理

一、tCL-tRCD-tRP-tRAS(第一时序) 内存是根据行和列寻址的,当请求触发后,最初是tRAS(Active to precharge Delay),预充电后,内存才真正开始初始化RAS(Row Address Strobe)。一旦tRAS激活后,RAS开始进行需要数据的寻址。首先是行地址,然后初始化tRCD(RAS to CAS Delay),周期结束,接着通过CAS(Column Address Strobe)访问所需数据的十六进制地址。其中,从CAS开始到CAS结束,就是CAS延迟(即t

不吃鱼的猫的博客 7万+

ibatis-dao框架中相关实例域存在原因总结

有关ibatis-dao组件部分的框架剖析和业务流程在《iBATIS 框架源码剖析》一书中写的已经比较详细。具体内容就不在这里写出来了,今天就把自己看过之后的几条心得和分析跟大家分享一下。写的不是很全,但都是个人觉得对于整体理解有很大帮助的内容。 1. daoManager提供的需求:     ·根据接口来获得实例化对象的代理对象——getDao方法实现。理由显而易见,必须获得代理对象

sum_rain的专栏 1443

基于Mind+、MOTT实现人工智能

@基于Mind+、MOTT实现人工智能 人工智能实验 一、实验目的及要求 【一】实现人脸识别 【二】实现云数据库应用 【三】实现测温数据mqtt上传至物联网云平台 二、实验原理与内容 基于二哈实现多人人脸学习和识别 基于TinywebDB实现云数据库应用 基于EasyIoT实现mqtt数据上传。 三、实验软硬件环境 硬件:掌控板 软件:Mind+ 四、实验过程(实验步骤、记录、数据、分析) 1.基于二哈实现多人人脸学习和识别 ①在Mind+扩展上的传感器选项选择HUSKYLENS AI 摄像头 ②配置哈士

weixin_44606930的博客 2372

IBATIS事务处理

iBATIS事务处理是和Dao紧密相联的。          在使用Dao时,如以下代码,先插入新记录,再进行更新:          UserDao.insertUser (user); // Starts transaction          user.setName("wh");          UserDao.updateUser (user); // Starts a ne

在你最感兴趣的事物上,隐藏着你人生的秘密 3万+

避坑指南:STM32H7不同DMA控制器(MDMA/DMA2D/BDMA)到底该用哪个?

本文深入解析STM32H7系列芯片的DMA控制器选型策略,涵盖MDMA、DMA2D、BDMA等不同控制器的架构原理与性能对比。通过实际项目案例,提供高速TCM数据搬运、GUI加速、低功耗传输等场景的选型决策树,并分享性能优化与调试技巧,帮助工程师避开常见陷阱,提升系统效率。

weixin_26876073的博客 447

AMEsim不连续处理机制:仿真精度与效率的平衡艺术

本文深入解析AMEsim不连续处理机制在系统仿真中的应用,探讨如何平衡仿真精度与效率。通过液压系统仿真案例,详细介绍了事件检测算法、变步长求解器自适应策略及典型场景优化方法,帮助工程师有效应对仿真中的不连续性问题,提升仿真效率和准确性。

v4w5x6的博客 440

ibatis DAO 事务

ibatis DAO 框架提供了事务管理模块。而这个事务管理可以应用到很多场合,包括JDBC、Hibernate、JTA、SQLMAP等。下面以最简单的JDBC来分析一下其如何实现事务管理。首先来看一段代码: public class OrderService { private DaoManager daoManager; private OrderDao orderDa...

狂徒技术分享 195

AD23导出Gerber文件保姆级教程:从PCB到嘉立创下单,一步不落(附常见问题排查)

本文提供AD23导出Gerber文件的详细教程,涵盖从PCB设计检查到嘉立创下单的全流程,特别针对两层板设计场景,帮助新手工程师避免常见错误。内容包括Gerber文件生成步骤、钻孔文件设置、文件检查工具推荐以及嘉立创下单实操指南,确保PCB打样顺利。

weixin_30924239的博客 440

TransactionSynchronizationManager事物提交之后操作

使用TransactionSynchronizationManager在事务提交之后操作 import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.transaction.support.TransactionSynchronizatio...

罗马没假日 3887

深入浅出:拆解RK3588 DVP摄像头驱动框架,从DTS配置到V4L2子系统

本文深入解析RK3588 DVP摄像头驱动框架,从DTS配置到V4L2子系统的完整实现。详细介绍了瑞芯微RK3588的多媒体子系统架构、设备树配置逻辑、rkcif驱动与V4L2框架的交互机制,以及用户空间调试与性能优化技巧,帮助开发者解决DVP摄像头驱动中的常见问题。

weixin_42525632的博客 265

Bev算法在J5平台的部署

支持多种形式的Bev算法:ipmlss时序,以及基于transformer的gkt。

@bangbang的博客 846

小毛iBatis.net保姆系列课程索引

小毛iBatis.net保姆系列课程索引 接下来一段时我会竭力来完善对iBatis.net的支持,为此出粗略的设计了下面的一个专题的教程来和大家一起学习使用ibatis.net iBatis.net前世今生 目标:对.net下的持久层访问框架有一个全局的...

122

ibatisnet系列(一) 总览

学习和使用 Ibatisnet 已经有一段时间了,前段时间也有写过一些与 iBatis 相关的 Blog 。也答应过一些朋友要比较全面地介绍一下 iBatis ,分享自己的学习过程和使用经验,记录一些常见且容易出现的问题。但由于前段时间一直在准备考试,而且当前的项目时间进度也比较紧,所以一直迟迟未能开始,在这里表示歉意,希望现在开始不会晚。不过最近社区(博客园)好消息不断,我发现越来越多的人开始关...

weixin_33835690的博客 73

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

ibmDW:凤凰涅槃:从 iBatis 到 MyBatis

原文转载自:http://www.ibm.com/developerworks/cn/opensource/os-cn-mybatis/index.html?ca=drs-   从 iBatis 到 MyBatis,你准备好了吗? 对于从事 Java EE 的开发人员来说,iBatis 是一个再熟悉不过的持久层框架了,在 Hibernate、JPA 这样的一站式对象 / 关系映射(O/R M...

weixin_34346099的博客 88

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

ibatis学习(三)---ibatis与spring的整合

Spring通过DAO模式,提供了对iBATIS的良好支持。SqlMapClient对象是iBATIS中的主要对象,我们可以通过配置让spring来管理SqlMapClient对象的创建。 与hibernate类似,Spring提供了SqlMapClientDaoSupport对象,我们的DAO可以继承这个类,通过它所提供的SqlMapClientTemplate对象来操纵数据库。看...

diliemu7730的博客 102

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

iBatis 学习 之一 DAO部分

iBatis作者不喜欢在类前加详尽的说明,批评一下.首先看一下包 com.ibatis.dao.client 1 定义了一个声明式接口 Dao2 一个DaoManager接口,它有一个创建Dao实例的工厂方法:  public Dao getDao(Class type); 是为了将用户自己写的Dao接口与sqlMap实现类解藕,通过在dao.xml中指定二者的映射。   public Dao

青之六号 3163

ibatis学习(二)--ibatis使用介绍

SqlMapClient对象这个对象是iBatis操作数据库的接口(执行CRUD等操作),它也可以执行事务管理等操作。这个类是我们使用iBATIS的最主要的类。它是线程安全的。通常,将它定义为单例。(与hibernate中sessionFactory的定义类似)。如: import java.io.Reader; import com.ibatis.common.resources.Reso...

孤雁是天的寂寞 127

ibatis学习

Spring通过DAO模式,提供了对iBATIS的良好支持。SqlMapClient对象是iBATIS中的主要对象,我们可以通过配置让spring来管理SqlMapClient对象的创建。 与hibernate类似,Spring 提供了SqlMapClientDaoSupport对象,我们的DAO可以继承这个类,通过它所提供的SqlMapClientTemplate对象来操纵数据库。看起来这些

小糊涂蛋大糊涂神的专栏 741

ibatis的学习spring

引::: Spring通过DAO模式,提供了对iBATIS的良好支持。SqlMapClient对象是iBATIS中的主要对象,我们可以通过配置让spring来管理SqlMapClient对象的创建。 与hibernate类似,Spring 提供了SqlMapClientDaoSupport对象,我们的DAO可以继承这个类,通过它所提供的SqlMapClientTemplate对...

weixin_30511039的博客 57

ibatis学习小结

 sqlMap配置文件 PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> useStatementNamespaces="true" cacheModelsEnabled="true" enhancem

tiancityycf的专栏 679

ibatis学习---ibatis与spring的整合

Spring通过DAO模式,提供了对iBATIS的良好支持。SqlMapClient对象是iBATIS中的主要对象,我们可以通过配置让spring来管理SqlMapClient对象的创建。 与hibernate类似,Spring 提供了SqlMapClientDaoSupport对象,我们的DAO可以继承这个类,通过它所提供的SqlMapClientTemplate对象来操纵数据库。看起来这

特立独行的猿 589

iBATIS SQLMap配置Demo浅析

iBATIS SQLMap配置Demo浅析转载自:http://developer.51cto.com/art/200907/138627.htmiBATIS SQLMap配置Demo向你介绍iBATIS SQLMap的具体配置以及实例。 在Spring的jpestore中,数据持久层用到了iBATIS SQLMaps,这里写一个iBAT

qqzhuyuehe的专栏 1191

iBATIS tips

1、iBATIS的配置文件 2、对象映射文件 3、编写应用程序 4、自动生成主键 5、DB2保存数据空值问题 6、传入参数 7、Result Map 8、复杂类型属性 9、缓存Mapped Statement结果集 10、动态Mapped Statement 11、事务处理 12、批处理 13、分页查询 14、Ibator     ...

James Blog 168
上一篇: iBatis 学习 之一 DAO部分
下一篇: struts 单元测试
_chage
博客等级 码龄24年 2粉丝 51原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值