Spring控制多張表的提交事務操作


一.Spring配置文件如下:

<bean id="test" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
  <property name="url"   value="jdbc:oracle:thin:@192.168.1.192:1521:test" />
  <property name="username" value="test" />
  <property name="password" value="test" />
  <property name="initialSize" value="5" />
  <property name="maxActive" value="10" />
 </bean>

 <!--transactionManager -->
 <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="test" />
 </bean>

 <bean id="baseTxProxy"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
  lazy-init="true">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>

 <bean id="test_jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource">
   <ref bean="test" />
  </property>
 </bean>

 <!-- manage Dao -->
 <bean id="testImpDao"
  class="com.test.dao.TestImpDao">
  <property name="jdbcTemplate">
   <ref bean="test_jdbcTemplate" />
  </property>
 </bean>
 
 <!-- transaction services -->
 <bean id="testService" parent="baseTxProxy">
  <property name="target">
   <bean
    class=com.test.service.TestServiceImp">
    <property name="testImpDao">
     <ref bean="testImpDao" />
    </property>
   </bean>
  </property>
 </bean>

 

 

二.實現類簡單說明:

1.DAO的接口

    public interface ITestDAO {            

       public void   insertTable1();     

       public void  insertTable2();  

       public void  insertTable3();  

 }   

 

2.DAO的實現

 public class ImpTestDAO implements ITestDAO{           

       public void   insertTable1(){
          this.getJdbcTemplate().update(sb.toString(),paraObjectArray);  
       }    

       public void  insertTable2(){
          this.getJdbcTemplate().update(sb.toString(),paraObjectArray);  
       }     

       public void  insertTable3(){
          this.getJdbcTemplate().update(sb.toString(),paraObjectArray);  
       }     

 }

 

3.service的接口層:

public interface ITestService {
    public String saveOperate(String params);
}

 

4.service的實現層

public class TestServiceImp implements ITestService{   
  private TestDAO  testDAO;   
   
  public void setTestDAO(TestDAO   testDAO){   
     this.testDAO=testDAO;   
 }   
     
 
  public void  saveOperate(){   
    this.testDAO.insertTable1();   
    this.testDAO.insertTable2();   
    this.testDAO.insertTable3();   
 }       
}  

 

5.前台的調用,如:aciton層l

public String saveOperate(String params) {
  String returnStr = "";
  StringBuffer errorSb = new StringBuffer("[");
  try {
    WebApplicationContext appContext=WebApplicationContextUtils.    
                     getWebApplicationContext(this.getServlet().getServletContext());
   ITestService Service = (ITestService) appContext
     .getBean("testService");
   returnStr = Service.saveOperate(params);
  }// 如果執行失敗,把具體的異常信息輸出,並且回滾相應的事務;
  catch (Exception e) {
   errorSb.append("{result:error,");
   errorSb.append("info:"" + e.getMessage() + ""}");
   errorSb.append("]");
   returnStr = errorSb.toString();
  }

  return returnStr;
 }

 

 

碰到的問題:在DAO的實現層,把jdbc的操作異常拋到service的實現層--即:'TestServiceImp'類中時,

自己寫了try{}catch(){},在方法中把異常給處理了,出現事務無法回滾的錯誤。

備注: 經過調試,發現TestDaoImp中的JDBC操作時,當執行出錯時不需要對異常進行特殊處理,

而是應該把異常拋出到service的實現類中后,在service的實現類中也不需要對該異常出處理,

而是要把該異常拋出調用serviceImp的類中(如:相應的aciton),這樣 

<bean id="testService" parent="baseTxProxy">,這個代理類才能捕獲到JDBC拋出的異常,才能根據對應的異常進行判斷是否要進行事務的回滾操作.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM