歡迎您光臨本站 註冊首頁

spring是如何對Service進行事務管理的

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

  本人也很想弄清楚spring是如何對Service進行事務管理的,並且還去看了一下spring框架關於事務管理幾個相關類的源碼,可惜由於本人功力有限,只看懂了皮毛.

  既然源代碼看不懂,那麼只有運用例子進行測試,雖然笨了點,不過管是白貓還是黑貓,能捉老鼠就是好貓.:)

  為引起不必要的爭論,本帖子只針對本案例的測試結果進行小結,並保證此測試代碼在本人的運行環境絕對正確.

  開發環境:

  OS:windows 2003 Server

  Web Server: jakarta-tomcat-5.0.28

  DataBase Server: MS SQL Server 2000 (打了SP3補丁)

  IDE: Eclipse 3.2.0 MyEclipse 5.0GA

  測試案例系統結構:

  web層<---->Service層<---->DAO層

  web層使用struts 1.1,DAO使用的spring的JDBC,spring版本1.2

  資料庫中有兩張表:

  student1和Student2,表結構相同:id,name,address.其中id為主鍵且為自增長型.

  student1表中有一條記錄:

  id name address

  1 xiaoming wuhan

  student2表中記錄為空

  測試情形一:

  web層捕獲異常並處理,DAO層不捕獲異常,Service也不捕獲異常.

  Service層介面:

  public interface StudentManagerService {

  public void bus_method();

  }

  DAO層介面

  public interface StudentDAO {

  public void deleteStudent1();

  public void insertStudent2();

  }

  StudentDAO介面的實現:

  public class StudentDAOImp extends JdbcDaoSupport implements StudentDAO{

  //刪除student1表中的id=1的記錄

  public void deleteStudent1(){

  JdbcTemplate jt=this.getJdbcTemplate();

  jt.update("delete from student1 where id=1");

  }

  //將student1表中刪除的記錄插入到student2中,但是此方法實現有錯,

  //id欄位設置為自增長的,在插入記錄時我們不能指定值

  public void insertStudent2(){

  JdbcTemplate jt=this.getJdbcTemplate();

  String arg[]=new String[3];

  arg[0]="1";

  arg[1]="xiaoming";

  arg[2]="wuhan";

  jt.update("insert student2(id,name,address) values(?,?,?)",arg);

  }

  }

  StudentManagerService 介面的實現:

  public class StudentManagerServiceImp implements StudentManagerService{

  private StudentDAO stdDAO;

  public void setStdDAO(StudentDAO stdDAO){

  this.stdDAO=stdDAO;

  }

  //此方法為事務型的:刪除student1中的記錄成功且插入student2的記錄也成功,

  //如果insertStudent2()方法執行失敗,那麼deleteStudent1()方法也應該會失敗

  public void bus_method(){

  this.stdDAO.deleteStudent1();

  this.stdDAO.insertStudent2();

  }

  }

  web層:

  三個jsp,一個action:

  index.jsp ==>首頁面.上面僅僅有一個超鏈接<a herf="test.do">執行</a>

  chenggong.jsp ==>Service執行成功後轉向的JSP頁面

  shibai.jsp ====>Service執行失敗後轉向的JSP頁面

  action實現:

  public class StudentManagerAction extends Action{

  public ActionForward execute(ActionMapping mapping, ActionForm form,

  HttpServletRequest request, HttpServletResponse response) {

  try{

  WebApplicationContext appContext=WebApplicationContextUtils.

  getWebApplicationContext(this.getServlet().getServletContext());

  StudentManagerService stdm=(StudentManagerService)appContext.

  getBean("stdServiceManager");

  stdm.bus_method();

  return mapping.findForward("chenggong");

  }

  catch(DataAccessException e){

  System.err.println("action execute service exception!");

  return mapping.findForward("shibai");

  }

  }

  }

  配置文件:

  web.xml

  <?xml version="1.0" encoding="UTF-8"?>

  <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <context-param>

  <param-name>log4jConfigLocation</param-name>

  <param-value>/WEB-INF/log4j.properties</param-value>

  </context-param>

  <context-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>/WEB-INF/applicationContext.xml</param-value>

  </context-param>

  <listener>

  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

  </listener>

  <listener>

  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  <servlet>

  <servlet-name>action</servlet-name>

  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

  <init-param>

  <param-name>config</param-name>

  <param-value>/WEB-INF/struts-config.xml</param-value>

  </init-param>

  <init-param>

  <param-name>debug</param-name>

  <param-value>3</param-value>

  </init-param>

  <init-param>

  <param-name>detail</param-name>

  <param-value>3</param-value>

  </init-param>

  <load-on-startup>0</load-on-startup>

  </servlet>

  <servlet-mapping>

  <servlet-name>action</servlet-name>

  <url-pattern>*.do</url-pattern>

  </servlet-mapping>

  </web-app>

  sturts-config.xml

  <struts-config>

  <action-mappings >

  <action input="/index.jsp" path="/test" type="test.StudentManagerAction >

  <forward name="chenggong" path="/chenggong.jsp" />

  <forward name="shibai" path="/shibai.jsp" />

  </action>

  </action-mappings>

  <message-resources parameter="test.ApplicationResources" />

  </struts-config>

  applicationContext.xml

  <?xml version="1.0" encoding="UTF-8"?>

  <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

  <beans>

  <bean id="dataSource"

  class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >

  <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"></property>

  <property name="url" value="jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=test"></property>

  <property name="username" value="sa"></property>

  <property name="password" value="sa"></property>

  </bean>

  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

  <property name="dataSource" ref="dataSource"/>

  </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="stdServiceManager" parent="baseTxProxy" >

  <property name="target">

  <bean class="test.StudentManagerServiceImp">

  <property name="stdDAO">

  <ref bean="stdDAO"/>

  </property>

  </bean>

  </property>

  </bean>

  <bean id="stdDAO" class="test.StudentDAOImp">

  <property name="dataSource" ref="dataSource"/>

  </bean>

  </beans>

  運行程序:啟動伺服器,並部署.進入index.jsp頁面,點擊"執行"超鏈接"---->頁面跳向shibai.jsp

  查看控制台:列印有:action execute service exception!

  查看資料庫: student1表中的[1 xiaoming wuhan] 記錄仍然存在,student2表仍然為空.

  小結:如果DAO層和Service不捕獲異常而在web層捕獲異常,web成功捕獲異常,spring事務管理成功!

  測試情形二:

  web層捕獲異常並處理,Service捕獲異常並處理,DAO層不捕獲異常.

  修改StudentManagerServiceImp類

  public class StudentManagerServiceImp implements StudentManagerService{

  private StudentDAO stdDAO;

  public void setStdDAO(StudentDAO stdDAO){

  this.stdDAO=stdDAO;

  }

  //此方法為事務型的,刪除student1中的記錄成功且插入student2的記錄也成功

  //如果insertStudent2()方法執行失敗,那麼deleteStudent1()也應該會失敗

  public void bus_method(){

  try{

  this.stdDAO.deleteStudent1();

  this.stdDAO.insertStudent2();

  }

  catch(DataAccessException de)

  System.err.println("service execute exception!");

  }

  }

  }

  運行程序:啟動伺服器,並部署.進入index.jsp頁面,點擊"執行"超鏈接"---->頁面跳向chenggong.jsp

  查看控制台:列印有:service execute exception!

  查看資料庫: student1表中的[1 xiaoming wuhan] 記錄不存在,student2表仍然為空.

  小結:如果Service捕獲異常並處理而不向外拋出,web層捕獲不到異常,spring事務管理失敗!

  測試情形(還原表中的數據)三:

  web層捕獲異常,Service捕獲異常,DAO層也捕獲異常.

  修改StudentDAOImp類代碼

  public class StudentDAOImp extends JdbcDaoSupport implements StudentDAO{

  //刪除student1表中的id=1的記錄

  public void deleteStudent1(){

  try{

  JdbcTemplate jt=this.getJdbcTemplate();

  jt.update("delete from student1 where id=1");

  }

  catch(DataAccessException e){

  System.err.println("dao deleteStudent1 execute exception!");

  }

  }

  //將student1表中刪除的記錄插入到student2中,但是此方法實現有錯,

  //id欄位設置為自增長的,在插入記錄時我們不能指定值

  public void insertStudent2(){

  try{

  JdbcTemplate jt=this.getJdbcTemplate();

  String arg[]=new String[3];

  arg[0]="1";

  arg[1]="xiaoming";

  arg[2]="wuhan";

  jt.update("insert student2(id,name,address) values(?,?,?)",arg);

  }

  catch(DataAccessException e){

  System.err.println("dao insertStudent2 execute exception!");

  }

  }

  }

  運行程序:啟動伺服器,並部署.進入index.jsp頁面,點擊"執行"超鏈接"---->頁面跳向chenggong.jsp

  查看控制台:列印有:dao insertStudent2 execute exception!

  查看資料庫: student1表中的 1,xiaoming,wuhan 記錄不存在,student2表仍然為空.

  小結如果DAO的每一個方法自己捕獲異常並處理而不向外拋出,Service層捕獲不到異常,Web層同樣捕獲不到異常,spring事務管理失敗!

  測試情形四:

  還原資料庫中的數據

  還原StudentDAOImp類中的方法為測試情形一中的實現

  web層捕獲異常Service拋出的自定義異常StudentManagerException

  Service捕獲DataAccessException並拋出StudentManagerException,

  StudentManagerException為DataAccessException的子類

  DAO層不捕獲異常

  修改StudentManagerServiceImp類的實現:

  public class StudentManagerServiceImp implements StudentManagerService{

  private StudentDAO stdDAO;

  public void setStdDAO(StudentDAO stdDAO){

  this.stdDAO=stdDAO;

  }

  //此方法為事務型的,刪除student1中的記錄成功且插入student2的記錄也成功

  //如果insertStudent2()方法執行失敗,那麼deleteStudent1()也應該會失敗

  public void bus_method() throws StudentManagerException{

  try{

  this.stdDAO.deleteStudent1();

  this.stdDAO.insertStudent2();

  }

  catch(DataAccessException de)

  System.err.println("service execute exception!");

  throw new StudentManagerException();//StudentManagerException類繼承DataAcce                          //ssException異常

  }

  }

  }

  修改StudentManagerAction

  public class StudentManagerAction extends Action{

  public ActionForward execute(ActionMapping mapping, ActionForm form,

  HttpServletRequest request, HttpServletResponse response) {

  try{

  WebApplicationContext appContext=WebApplicationContextUtils.

  getWebApplicationContext(this.getServlet().getServletContext());

  StudentManagerService stdm=(StudentManagerService)appContext.

  getBean("stdServiceManager");

  stdm.bus_method();

  return mapping.findForward("chenggong");

  }

  catch(StudentManagerException e){

  System.err.println("action execute service exception!");

  return mapping.findForward("shibai");

  }

  }

  }

  運行程序:啟動伺服器,並部署.進入index.jsp頁面,點擊"執行"超鏈接"---->頁面跳向shibai.jsp

  查看控制台:列印有:service execute exception!

  action execute service exception!

  查看資料庫: student1表中的 [1,xiaoming,wuhan] 記錄仍然存在,student2表仍然為空.

  小結如果DAO的每一個方法不捕獲異常,Service層捕獲DataAccessException異常並拋出自己定義異常(自定義異常為DataAccessException的子類),Web層可以捕獲到異常,spring事務管理成功!

  結合源碼總結:

  1.spring在進行聲明時事務管理時,通過捕獲Service層方法的DataAccessException來提交和回滾事務的,而Service層方法的DataAccessException又是來自調用DAO層方法所產生的異常.

  2.我們一般在寫DAO層代碼時,如果繼承JdbcDaoSupport 類,並使用此類所實現的JdbcTemplate來執行資料庫操作,此類會自動把低層的SQLException轉化成DataAccessException以及DataAccessException

  的子類.

  3.一般在Service層我們可以自己捕獲DAO方法所產成的DataAccessException,然後再拋出一個業務方法有意義的異常(ps:此異常最好繼承DataAccessException),然後在Web層捕獲,這樣我們就可以手動編碼的靈活實現通過業務方法執行的成功和失敗來向用戶轉發不同的頁面.


[火星人 ] spring是如何對Service進行事務管理的已經有798次圍觀

http://coctec.com/docs/java/show-post-60130.html