当前位置:编程学习 > JAVA >>

spring管理hibernate的事务后异常怎莫捕获不到?

现在遇到的问题,在ssh中,spring管理hibernate的事务。现在业务层有个方法
try {
System.out.println("1---->");
auserDao.save(auser);
} catch (Exception e) {
System.out.println("添加用户出现异常");
throw new AuctionException("添加用户出现异常");
}
System.out.println("2---->");
return auser;

这里想捕获一下异常,然后又抛到action中。AuctionException是继承Exception的一个自定义类。

public String addAuser() throws AuctionException {
   System.out.println(auserName);
   System.out.println(auserPass);
   auser = manager.addAuser(auserName, auserPass, 3);
   if (auser != null) {
ActionContext.getContext().getSession().put("auser", auser);
return SUCCESS;
  } else {
return ERROR;
  }
}

然后struts.xml配置全局异常

<global-results> 
  <result name="exception">/error.jsp</result>  
</global-results>  

<global-exception-mappings>
  <exception-mapping name="exception" exception="per.elec.utils.AuctionException"  
     result="exception"></exception-mapping>
  <exception-mapping name="exception" exception="java.lang.Exception"  
     result="exception"></exception-mapping>     
</global-exception-mappings> 

添加用户中auserName不能为空,但运行时让auserName为空,按理说业务层应该会出现异常,并且能捕获到异常,然后抛到action中,通过struts2的配置文件跳到指定的页面。
可是运行的时候统统不起作用。

null
null
1---->
2---->
2013-5-16 20:06:57 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
严重: Exception occurred during processing request: Could not execute JDBC batch update; SQL [insert into elec.auser (auser_name, auser_pass, auser_money, limit_id, photo_id, auser_id) values (?, ?, ?, ?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; SQL [insert into elec.auser (auser_name, auser_pass, auser_money, limit_id, photo_id, auser_id) values (?, ?, ?, ?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:643)
at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:394)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy5.addAuser(Unknown Source)
at per.elec.action.UpdateAuserAction.addAuser(UpdateAuserAction.java:145)

.......



页面也是出现一大堆异常信息,没有跳到指定页

HTTP Status 500 - Could not execute JDBC batch update; SQL [insert into elec.auser (auser_name, auser_pass, auser_money, limit_id, photo_id, auser_id) values (?, ?, ?, ?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

--------------------------------------------------------------------------------

type Exception report

message Could not execute JDBC batch update; SQL [insert into elec.auser (auser_name, auser_pass, auser_money, limit_id, photo_id, auser_id) values (?, ?, ?, ?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

description The server encountered an internal error that prevented it from fulfilling this request.

exception 
org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; SQL [insert into elec.auser (auser_name, auser_pass, auser_money, limit_id, photo_id, auser_id) values (?, ?, ?, ?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:643)




上面输出结果的两个null是指auserName与auserPass为null,程序肯定会报异常,但为什么在业务层的那个try..catch不起作用?业务层的事务是不是都在退出方法以后才提交,也就是方法都退出了才出现异常,所以才捕获不到?像auserDao.save(auser)之类的操作都还没有提交事务?

业务层的异常没有捕获到,在action中也是直接抛异常,但为什么struts.xml里的全局异常不起作用?


这个问题比较急,希望能有人帮忙回答,谢谢!




补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,