惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
阮一峰的网络日志
阮一峰的网络日志
T
The Blog of Author Tim Ferriss
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
宝玉的分享
宝玉的分享
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
博客园 - 司徒正美
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - Franky
B
Blog
V
V2EX
J
Java Code Geeks
D
Docker
博客园 - 叶小钗
The Cloudflare Blog
量子位
博客园_首页
MongoDB | Blog
MongoDB | Blog

博客园 - Squall

Spring2.5中基于注释的IoC struts2登录拦截器 Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AU hibernate.current_session_context_class 错误 - Squall - 博客园 Lucene中使用Paoding中文分词 ibatis VS hibernate 经典论述 超越传统软件开发技术 ASP 缩略图 (纠错版) persits.jpeg 破解版下载 Cant connect to SQL server with jtds 1.2.2 - Squall The hibernate core class ------ ExportToDB Struts+spring使用随机验证码 SSH 异常 解决 方案 总结 Struts-Logic 标签库 structs - 标签库(html) Could not open ServletContext resource [/WEB-INF/applicationContext.xml] myeclipse tomcat 部署错误 ASP 通过正则提取信息内容种的图片 div+css 绝对定位和相对定位
spring2.0 事务管理以及异常处理之体会
Squall · 2009-01-07 · via 博客园 - Squall

开发环境:
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表中有一条记录:

测试情形一:
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层捕获,这样我们就可以手动编码的灵活实现通过业务方法执行的成功和失败来向用户转发不同的页面