public void saveSomething(String param) throws Exception() {
try{
doSomething.......;
}catch(Exception e) {
e.printStackTrace();
}
}
2.問題分析
由於在service層中,使用了try ,catch來捕捉異常,導致sevice層出現的異常被 “截留”,無法拋出給事務管理器,這就給事務管理器造成一種假象,就像程勳在運行中,沒有產生任何問題,因此也就不會對出現 runtimeException進行回滾操作。
3. 正確做法
在service中不進行異常的 try 和catch操作, 只是在方法定義中 throws exception(spring的事務管理也就能看到異常了),這些異常將由 struts的action來進行捕捉,在客戶端給出友好的提示。
4 注意的問題
spring的事務管理器只對 unchecked exception進行異常回滾,Error和RuntimeException及其子類是unchecked exception.其他exception是checked exception.
舉例: runtimeException有 以下
AnnotationTypeMismatchException,
ArithmeticException,
ArrayStoreException,
BufferOverflowException,
BufferUnderflowException,
CannotRedoException,
CannotUndoException,
ClassCastException,
CMMException,
ConcurrentModificationException,
DOMException,
EmptyStackException,
EnumConstantNotPresentException,
EventException,
IllegalArgumentException,
IllegalMonitorStateException,
IllegalPathStateException,
IllegalStateException,
ImagingOpException,
IncompleteAnnotationException,
IndexOutOfBoundsException,
JMRuntimeException,
LSException,
MalformedParameterizedTypeException,
MissingResourceException,
NegativeArraySizeException,
NoSuchElementException,
NullPointerException,
ProfileDataException,
ProviderException,
RasterFormatException,
RejectedExecutionException,
SecurityException,
SystemException,
TypeNotPresentException,
UndeclaredThrowableException,
UnmodifiableSetException,
UnsupportedOperationException