对父类和子类都进行注释时,应用程序异常如何工作?

时间:2019-01-21 17:35:15

标签: java exception java-ee ejb ejb-3.1

在此问题中,我指的是 JSR 318:Enterprise JavaBeansTM,版本3.1

在第14章(异常处理)中。下面的例子在那里。而且我了解。

@ApplicationException(rollback=true)
public class ExceptionA extends RuntimeException

public class ExceptionB extends ExceptionA

@ApplicationException(inherited=false, rollback=false)
public class ExceptionC extends ExceptionB

public class ExceptionD extends ExceptionC

ExceptionA is an application exception with transaction rollback.

ExceptionB is an application exception with transaction rollback.

ExceptionC is an application exception without transaction rollback.

ExceptionD is not an application exception.

我的问题是,当对类进行如下注释并且抛出ExceptionB时会发生什么?交易会回滚吗?

@ApplicationException
public class ExceptionA extends RuntimeException

@ApplicationException(rollback=true)
public class ExceptionB extends ExceptionA

1 个答案:

答案 0 :(得分:0)

在您的示例(或问题)中,

@ApplicationException // rollback attribute defaults to false, so tx will no rollback
public class ExceptionA extends RuntimeException

@ApplicationException(rollback=true) // tx will be marked for rollback
public class ExceptionB extends ExceptionA

另一个要点是,默认情况下,RuntimeExceptions始终将事务标记为回滚!所以,您需要注意它们...让我解释一下:

// Because it is not annotated, applies default behaviour... this is,
// the exception will be wrapped in EJB Exception and the transaction
// will marked for rollback (because it is a RuntimeException)
public class ExceptionA extends RuntimeException {
}

// The exception will be thrown like it is (not wrapped)
// and the tx will no marked for roll back (even if it is a RuntimeException)
// rollback defaults to false and inherited attribute defaults to true
@ApplicationException
public class ExceptionB extends ExceptionA {
}