php培训哪个好:Spring 下事务管理使用 AOP XML 配置管理(iBatis 为例)

来源:百度文库 编辑:偶看新闻 时间:2024/04/28 00:52:12

Spring 下事务管理 - 使用 AOP XML 配置管理(iBatis 为例)

        Spring下由三种途径对事物进行管理:编程式事务管理、声明式事务管理和AOP事务管理。其中AOP事务管理又分AOP注解事务管理和AOP XML配置两种,这里记录下述其

中的AOP XML配置管理,这也是spring最推荐的方式。
        参照中的银行转账的例子。
        1.Spring的数据源设置

 

  1.  id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >    
  2.    name="driverClassName" value="com.mysql.jdbc.Driver"/>    
  3.    name="url" value="jdbc:mysql://localhost:3306/test"/>    
  4.    name="username" value="root"/>    
  5.    name="password" value="123456"/>    
  6.     



        2.Spring对iBATIS的支持
        Spring对ibatis主要提供org.springframework.orm.ibatis.SqlMapClientFactoryBean类来进行支持

 

  1.  id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">    
  2.    name="dataSource" ref="dataSource"/>    
  3.    name="configLocation" value="/config/sqlMapConfig.xml"/>    
  4.     



        3.Spring对iBATIS DAO的支持
        Spring提供org.springframework.orm.ibatis.support.SqlMapClientDaoSupport来对iBATIS DAO进行支持,通过调用该类的getSqlMapClientTemplate()方法来获得对

iBATIS的控制访问。

 

  1.  id="accountDao" class="com.hj.dao.AccountDaoImp">    
  2.     name="sqlMapClient" ref="sqlMapClient"/>    
  3.    


 

 

  1.  id="bankService" class="com.hj.bankOps.DefaultBankService">    
  2.     name="accountDao" ref="bankAccountDao"/>    
  3.    



        这里DefaultBankService类主要实现BankService接口(提供服务的方法定义),其内部引用一个BankAccountDao实例来对数据库进行访问。BankAccountDao类主要继承

SqlMapClientDaoSupport。
        4.Spring 配置事务

view plaincopy to clipboardprint?

  1.  id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
  2.       name="dataSource" ref="dataSource"/>    
  3.      



        5.AOP XML配置事务管理
        1).配置事务通知

view plaincopy to clipboardprint?

  1.  id="transactionManagerAdivice" transaction-manager="transactionManager">    
  2.        
  3.        name="*"      
  4.                 isolation="READ_COMMITTED"      
  5.                 propagation="REQUIRED"      
  6.                 rollback-for="java.lang.RuntionException" />    
  7.        
  8.     



        2).配置切入点和方面

 

  1.     
  2.     expression="execution(* com.hj.bankOps.DefaultBankService.*(..))" id="bankServicePc"/>    
  3.     advice-ref="transactionManagerAdivice" pointcut-ref="bankServicePc"/>       
  4.      



        上述execution(* com.hj.bankOps.DefaultBankService.*(..))表达式表示切入点为该类中的任何方法。所以当DefaultBankService类中方法调用时就会进行事务管理

,并且当抛出RuntimeException时,自动进行回滚操作。
        6.遇到的问题
        在一书上,对AOP XML事务配置时,其通知部分并没有设置具体属性(缺少 rollback-for="java.lang.RuntionException")

 

  1.     
  2.        name="*"      
  3.                 isolation="READ_COMMITTED"      
  4.                 propagation="REQUIRED"      
  5.                />    
  6.       
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. 完整的application.xml

  16.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  17.  
       destroy-method="close">
      
       com.mysql.jdbc.Driver
      

      
       jdbc:mysql://127.0.0.1:3306/projectcost?autoReconnect=true
      

      
       root
      

      
       root
      

     
  18.  
     
      
      
     
  19.  
      
       
      

     

  20.  
     
      
       
      

     
  21.  
      
       
      

     
  22.  
      
      

     
  23.  
  24.    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      
     
  25.  
  26.  
         expression="execution(* com.intertid.services.impl.DAOServicesImpl.*(..))" />
      
     
  27.  
      
           rollback-for="java.lang.ArithmeticException" />
      

     

  28.  
  29.  
  30. action 中的写法:
  31. import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
  32. import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
  33. import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
  34. import base.action.BaseAction;
  35. import com.intertid.common.ConstFormat;
    import com.intertid.dto.Customer;
    import com.intertid.form.CustomerInputForm;
    import com.intertid.services.DAOServices;
    /***
     *
     * @author Administrator
     *
     */
    public class CustomerInputAction extends BaseAction {
     CustomerInputForm customerInputFormIns;
     @Override 
     public ActionForward logic(ActionMapping mapping, ActionForm form,
       HttpServletRequest request, HttpServletResponse response)
       throws Exception {
      customerInputFormIns=(CustomerInputForm) form;
    //  CustomerInputDAO customerInputDAO= (CustomerInputDAO) ctx.getBean("customnerInputDAO");
      DAOServices daoServicesIns= (DAOServices) ctx.getBean("baseDAOServices");
      Customer customerPar=new Customer();
      customerPar.setCustomerTypeID(getCustomerTypeID());
      customerPar.setAddress(customerInputFormIns.getAddress());
      customerPar.setCustomerName(customerInputFormIns.getCustomerName());
      customerPar.setFax(customerInputFormIns.getFax());
      customerPar.setPhoneNum(customerInputFormIns.getPhoneNum());
      customerPar.setRecordCreateDate(ConstFormat.DATE.format(new Date())
        .toString());
      customerPar.setRecordCreator(getLoginUser(request).getUserName());
      List customerlist = new ArrayList();
      customerlist.add(customerPar);
      customerlist.add(customerPar);
      customerlist.add(customerPar);
      Integer effectRows= daoServicesIns.insertCsutomer(customerlist);
      if(effectRows>0)
      {
       mapping.findForward("success");
      }
      else
      {
       mapping.findForward("fail");
      }
      return null;
     }
     /***
      * 客户类型取得
      * @return
      */ 
     private Integer getCustomerTypeID()
     {
      if(ConstFormat.CUSTOMER_BIG.equals(customerInputFormIns.getCustomerType()))
      {
       return 1;
      }
      else
      {
       return 2;
      }
     }
  36. }

        services dao 的写法:

  1. import java.util.List;
  2. import com.intertid.dto.Customer;
  3. public interface DAOServices {
    int insertCsutomer(List customerList);
  4. }
     
  5. services dao 实现类的写法:
  6. import java.util.List;
  7. import com.intertid.dao.CustomerInputDAO;
    import com.intertid.dto.Customer;
    import com.intertid.services.DAOServices;
  8. public class DAOServicesImpl implements DAOServices {
     private CustomerInputDAO customnerInputDAO;
     public CustomerInputDAO getCustomnerInputDAO() {
      return this.customnerInputDAO;
     }
  9.  public void setCustomnerInputDAO(CustomerInputDAO customnerInputDAO) {
      this.customnerInputDAO = customnerInputDAO;
     }
  10.  @Override
     public int insertCsutomer(List customerList) {
      for (int i = 0; i < customerList.size(); i++) {
       System.out.println(""+i);
       if(i==1){
        //spring的事物会让他们全部不插入,ACID
        
         int a=1/0;//抛出异常 系统捕捉到,则自动回滚,与
    //  
    //    //   rollback-for="java.lang.ArithmeticException" />
     // 
  11.    }
       getCustomnerInputDAO().saveCustomer(customerList.get(i));
      }
      return 0;
     }
    }
  12.  
  13. 数据库访问DAO的写法:
  14. public interface CustomerInputDAO {
    public Integer saveCustomer(Customer cus);
    }
  15. 数据库访问DAO实现类的写法:
  16. public class CustomerInputDAO extends SqlMapClientDaoSupport implements
      com.intertid.dao.CustomerInputDAO {
  17.  @Override
     public Integer saveCustomer(Customer cus) {
      Object ret= getSqlMapClientTemplate().insert("insertCustomer", cus);
      
      if(ret==null)
      {
       return 0;
      }
      else
      {
       
       return Integer.parseInt(ret.toString());
      }
     }
  18. }
  19.  
  20. 如果回滚不成功 执行以下操作:
  21. 1.

SHOW variables like "have_%"

显示结果中会有如下3种可能的结果:

o    have_innodb YES

o    have_innodb NO

o    have_innodb DISABLED

这3种结果分别对应:

o    已经开启InnoDB引擎

o    未安装InnoDB引擎

o    未启用InnoDB引擎

针对第二种未安装,只需要安装即可;针对第三种未启用,则打开mysql配置文件,找到 skip-innodb项,将其改成#skip-innodb,之后重启mysql服务即可。

2.查看你数据库中表的类型是否支持回滚 命令为:show table status;,我就发现在我的表类型为MYISAM,这种类型默认不支持回滚,而InnoDB支持
3.问题查出来了,现在怎么把类型改为InnoDB类型呢?在MySQL中输入命令:alter table tablename engine = InnoDB;