本人也很想弄清楚spring是如何對Service進行事務管理的
既然源代碼看不懂
為引起不必要的爭論
開發環境
OS
Web Server: jakarta
DataBase Server: MS SQL Server
IDE: Eclipse
測試案例系統結構
web層<
web層使用struts
數據庫中有兩張表
student
student
id name address
student
測試情形一
web層捕獲異常並處理
Service層接口
public interface StudentManagerService {
public void bus_method();
}
DAO層接口
public interface StudentDAO {
public void deleteStudent
public void insertStudent
}
StudentDAO接口的實現
public class StudentDAOImp extends JdbcDaoSupport implements StudentDAO{
//刪除student
public void deleteStudent
JdbcTemplate jt=this
jt
}
//將student
//id字段設置為自增長的
public void insertStudent
JdbcTemplate jt=this
String arg[]=new String[
arg[
arg[
arg[
jt
}
}
StudentManagerService 接口的實現
public class StudentManagerServiceImp implements StudentManagerService{
private StudentDAO stdDAO;
public void setStdDAO(StudentDAO stdDAO){
this
}
//此方法為事務型的
//如果insertStudent
public void bus_method(){
this
this
}
}
web層:
三個jsp
index
chenggong
shibai
action實現
public class StudentManagerAction extends Action{
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
try{
WebApplicationContext appContext=WebApplicationContextUtils
getWebApplicationContext(this
StudentManagerService stdm=(StudentManagerService)appContext
getBean(
stdm
return mapping
}
catch(DataAccessException e){
System
return mapping
}
}
}
配置文件
web
<?xml version=
<web
<context
<param
<param
</context
<context
<param
<param
</context
<listener>
<listener
</listener>
<listener>
<listener
</listener>
<servlet>
<servlet
<servlet
<init
<param
<param
</init
<init
<param
<param
</init
<init
<param
<param
</init
<load
</servlet>
<servlet
<servlet
<url
</servlet
</web
sturts
<struts
<action
<action input=
<forward name=
<forward name=
</action>
</action
<message
</struts
applicationContext
<?xml version=
<!DOCTYPE beans PUBLIC
<beans>
<bean id=
class=
<property name=
<property name=
<property name=
<property name=
</bean>
<bean id=
<property name=
</bean>
<bean id=
<property name=
<ref bean=
</property>
<property name=
<props>
<prop key=
</props>
</property>
</bean>
<bean id=
<property name=
<bean class=
<property name=
<ref bean=
</property>
</bean>
</property>
</bean>
<bean id=
<property name=
</bean>
</beans>
運行程序
查看控制台
查看數據庫
小結
測試情形二
web層捕獲異常並處理
修改StudentManagerServiceImp類
public class StudentManagerServiceImp implements StudentManagerService{
private StudentDAO stdDAO;
public void setStdDAO(StudentDAO stdDAO){
this
}
//此方法為事務型的
//如果insertStudent
public void bus_method(){
try{
this
this
}
catch(DataAccessException de)
System
}
}
}
運行程序
查看控制台
查看數據庫
小結
測試情形(還原表中的數據)三
web層捕獲異常
修改StudentDAOImp類代碼
public class StudentDAOImp extends JdbcDaoSupport implements StudentDAO{
//刪除student
public void deleteStudent
try{
JdbcTemplate jt=this
jt
}
catch(DataAccessException e){
System
}
}
//將student
//id字段設置為自增長的
public void insertStudent
try{
JdbcTemplate jt=this
String arg[]=new String[
arg[
arg[
arg[
jt
}
catch(DataAccessException e){
System
}
}
}
運行程序
查看控制台
查看數據庫
小結如果DAO的每一個方法自己捕獲異常並處理而不向外拋出
測試情形四
還原數據庫中的數據
還原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
}
//此方法為事務型的
//如果insertStudent
public void bus_method() throws StudentManagerException{
try{
this
this
}
catch(DataAccessException de)
System
throw new StudentManagerException();//StudentManagerException類繼承DataAcce //ssException異常
}
}
}
修改StudentManagerAction
public class StudentManagerAction extends Action{
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
try{
WebApplicationContext appContext=WebApplicationContextUtils
getWebApplicationContext(this
StudentManagerService stdm=(StudentManagerService)appContext
getBean(
stdm
return mapping
}
catch(StudentManagerException e){
System
return mapping
}
}
}
運行程序
查看控制台
action execute service exception!
查看數據庫
小結如果DAO的每一個方法不捕獲異常
結合源碼總結
的子類.
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28430.html