body
{
font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif;
font-size: 10.5pt;
line-height: 1.5;
}
html, body
{

}
h1 {
font-size:1.5em;
font-weight:bold;
}
h2 {
font-size:1.4em;
font-weight:bold;
}
h3 {
font-size:1.3em;
font-weight:bold;
}
h4 {
font-size:1.2em;
font-weight:bold;
}
h5 {
font-size:1.1em;
font-weight:bold;
}
h6 {
font-size:1.0em;
font-weight:bold;
}
img {
border:0;
max-width: 100%;
}
blockquote {
margin-top:0px;
margin-bottom:0px;
}
table {
border-collapse:collapse;
border:1px solid #bbbbbb;
}
td {
border-collapse:collapse;
border:1px solid #bbbbbb;
}

IBATIS事务处理 - - 博客频道 - CSDN.NET

        iBATIS事务处理是和Dao紧密相联的。 

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

        UserDao.insertUser (user); // Starts transaction 

        user.setName("wh"); 

        UserDao.updateUser (user); // Starts a new transaction

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

        iBatis是通过DaoManager类来统管Dao子类的事务,

        众Dao子类由DaoManager产生,如下:

        Reader reader =Resources.getResourceAsReader("dao.xml"); 

        DaoManager  daoManager =DaoManagerBuilder.buildDaoManager(reader);  

        UserDao userDao = (UserDao) daoManager.getDao(UserDao.class);

        UserDao是用户自己定义的接口,获得的其实是在dao.xml中指定的相对应的 SqlMapDao实现类,从而实现了松藕合。在良好的分层设计中,

 

        但是一般的事务需要放到业务层,因为一个业务需要具有原子性,事务放到Dao层是不能达到业务一致的效果的,那么如果想要把事务放到业务层,就需要在业务层使用显示事务进行声明处理.

 

        显式地声明事务处理语句,如下:

        try { 

                daoManager.startTransaction(); 

                UserDao.insertUser (user);  

                user.setName("wh"); 

                UserDao.updateUser(user);  

                otherDao.doSomething(other); 

                 ... 

                daoManager.commitTransaction(); 

       } finally { 

                daoManager.endTransaction(); 

       }

       这样就保持了原子性,整体为一个事务,要么全部执行成功,否则回滚。

 

       现在唯一的问题就是,dao层的事务是否已经放弃,否则产生事务嵌套问题对性能会有影响. 

       当然,iBatis 完全可以这么做:建一个声明式接口:IService,再使用动态代理,将用户自己的Serivce子类通过动态代理自动包上事务处理的代码,默认每一个业务方法为一个事务。 

       大师的心如果能轻易揣测,就是不大师了:),估计大师认为这样属于过度设计,他认为把这种灵活性交给用户是合适的,相当多的service 方法只调用一个Dao方法,例如CRUD操作。 

       再补充一下,iBatis中对事务的处理是可配置的,最常用的Type是"JDBC",也可以声明为"JTA"或"EXTERNAL".

 

       项目里常用Spring与Ibatis配合使用,这样可以在Spring里配置事务管理,可以省去业务层的显示事务代码.

        spring的配置文件的基本写法为:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3.    xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"   
  4.    xmlns:context="http://www.springframework.org/schema/context"   
  5.    xsi:schemaLocation="    
  6.           http://www.springframework.org/schema/beans     
  7.           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.           http://www.springframework.org/schema/aop     
  9.           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd    
  10.           http://www.springframework.org/schema/tx     
  11.           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  12.            
  13.            
  14. <bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource" >  
  15.    <property name="driverClassName"value="com.mysql.jdbc.Driver" />  
  16.    <property name="url"value="jdbc:mysql://10.11.0.145:3306/carrefour?characterEncoding=gb2312"/>  
  17.    <property name="username" value="dev01" />  
  18.    <property name="password" value="123456"/>  
  19. </bean>  
  20. <bean id="sqlClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  21.    <property name="dataSource">  
  22.       <ref local="dataSource" />  
  23.    </property>  
  24.    <property name="configLocation">  
  25.       <value>classpath:sqlmaps.xml</value>  
  26.    </property>  
  27. </bean>  
  28. <!--配置事务管理器-->  
  29. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  30.         <propertynamepropertyname="dataSource" ref="dataSource"></property>  
  31. </bean>  
  32. <!--配置哪些方法,什么情况下需要回滚-->  
  33. <tx:advice id="serviceAdvice"transaction-manager="transactionManager">   
  34.     <tx:attributes>    
  35.         <!--当代理的service层中的方法抛出异常的时候才回滚,必须加rollback-for参数-->  
  36.         <tx:methodnametx:methodname="insert*" propagation="REQUIRED"rollback-for="Throwable"/>  
  37.         <tx:methodnametx:methodname="del*" propagation="REQUIRED"rollback-for="Throwable"/>   
  38.         <tx:methodnametx:methodname="update*" propagation="REQUIRED"rollback-for="Throwable"/>   
  39.         <!--除了上面标识的方法,其他方法全是只读方法-->  
  40.         <tx:methodnametx:methodname="*" read-only="true"/>   
  41.     </tx:attributes>   
  42. </tx:advice>   
  43. <!-- 配置哪些类的方法需要进行事务管理 -->   
  44. <aop:config proxy-target-class="true">   
  45. <aop:pointcut id="servicePointcut"expression="execution(* com.wh.service.*.*(..))"/>   
  46. <aop:advisor pointcut-ref="servicePointcut"advice-ref="serviceAdvice"/>   
  47. </aop:config>  
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName"value="com.mysql.jdbc.Driver" />
<property name="url"value="jdbc:mysql://10.11.0.145:3306/carrefour?characterEncoding=gb2312"/>
<property name="username" value="dev01" />
<property name="password" value="123456"/>
</bean>
<bean id="sqlClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="configLocation">
<value>classpath:sqlmaps.xml</value>
</property>
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource" ref="dataSource"></property>
</bean>
<!--配置哪些方法,什么情况下需要回滚-->
<tx:advice id="serviceAdvice"transaction-manager="transactionManager">
<tx:attributes>
<!--当代理的service层中的方法抛出异常的时候才回滚,必须加rollback-for参数-->
<tx:methodname="insert*" propagation="REQUIRED"rollback-for="Throwable"/>
<tx:methodname="del*" propagation="REQUIRED"rollback-for="Throwable"/>
<tx:methodname="update*" propagation="REQUIRED"rollback-for="Throwable"/>
<!--除了上面标识的方法,其他方法全是只读方法-->
<tx:methodname="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置哪些类的方法需要进行事务管理 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="servicePointcut"expression="execution(* com.wh.service.*.*(..))"/>
<aop:advisor pointcut-ref="servicePointcut"advice-ref="serviceAdvice"/>
</aop:config>

         这是在spring+Ibatis的情况下, ,通过aop控制需要事务的包和具体方法,将事务控制在service层,来达到事务在业务层提交和回滚.保持业务的原子性.

IBATIS事务处理 - - 博客频道 - CSDN.NET的更多相关文章

  1. ibatis 学习笔记 3 - pfpfpfpfpf的专栏 - 博客频道 - CSDN.NET

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  2. Mybatis 示例之 Association - 偶尔记一下 - 博客频道 - CSDN.NET

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  3. 最牛B的编码套路 - 呦呦鹿鸣 - 博客频道 - CSDN.NET

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  4. java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET

    java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了!      社区福利快来领取免费参加MDCC大会机会哦    Tag功能介绍—我们 ...

  5. Notepad++前端开发常用插件介绍 - BorisHuai前端修炼 - 博客频道 - CSDN

    Notepad++前端开发常用插件介绍 - BorisHuai前端修炼 - 博客频道 - CSDN.NET http://blog.csdn.net/borishuai/article/details ...

  6. 帧与场 - djf_1985的专栏 - 博客频道 - CSDN.NET

    帧与场 - djf_1985的专栏 - 博客频道 - CSDN.NET 电视信号是通过摄像机对自然景物的扫描并经光电转换形成的.扫描方式分为“逐行扫描”和“隔行扫描”.“逐行扫描”指每幅图像均是由电子 ...

  7. 知道创宇爬虫题--代码持续更新中 - littlethunder的专栏 - 博客频道 - CSDN.NET

    知道创宇爬虫题--代码持续更新中 - littlethunder的专栏 - 博客频道 - CSDN.NET undefined 公司介绍 - 数人科技 undefined

  8. 采集爬虫中,解决网站限制IP的问题? - wendi_0506的专栏 - 博客频道 - CSDN.NET

    采集爬虫中,解决网站限制IP的问题? - wendi_0506的专栏 - 博客频道 - CSDN.NET undefined

  9. C# DataTable的詳細用法 - hcw_peter的专栏 - 博客频道 - CSDN

    C# DataTable的詳細用法 - hcw_peter的专栏 - 博客频道 - CSDN.NET 在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够 ...

随机推荐

  1. Oracle自增长序列

    create table user_info ( id number(6) primary key, username varchar2(30) not null, password varchar2 ...

  2. Notification使用笔记

    之前在项目中使用了Notification,现分享出来: checkNotification() function checkNotification(){ //判断是否支持Notification ...

  3. ligerUI调用$.ligerDialog.open弹出窗口,关闭后无法获取焦点问题

    1:调用父窗口某一个文件框,获取焦点,   parent.window.document.getElementByIdx_x("roleName").focus(); 2:关闭父窗 ...

  4. struts2中的值栈对象ValueStack

    ValueStack, 即值栈对象. 值栈对象: 是整个struts数据存储的核心,或者叫中转站. 用户每次访问struts的action,都会创建一个Action对象.值栈对象.ActionCont ...

  5. SqlServer 查询死锁,杀死死锁进程

    -- 查询死锁 select request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tableName from sy ...

  6. android ScrollView嵌套EditText

    editext.setOnTouchListener(new OnTouchListener() {                        @Override            publi ...

  7. js 基础对象一

    JavaScript 通常用于操作 HTML 元素. Document元素 每个载入浏览器的 HTML 文档都会成为 Document 对象. Document 对象使我们可以从脚本中对 HTML 页 ...

  8. Servlet与JSP的异同

    1.什么是Servlet A Java servlet is a Java programming language program that extends the capabilities of ...

  9. 使用DTM ( Dynamic Topic Models )进行主题演化实验

    最近想研究下Dynamic Topic Models(DTM),论文看了看,文科生的水平确实是看不懂,那就实验一下吧,正好Blei的主页上也提供了相应的C++工具, http://www.cs.pri ...

  10. Php和httpd.conf的配置

    http://www.cnblogs.com/homezzm/archive/2012/08/01/2618062.html http://book.51cto.com/art/201309/4096 ...