【发布时间】:2017-08-16 06:26:29
【问题描述】:
Java 接口:
public interface IUserSettingManager {
UserSettingApi updateSetting(Long userId, UserSetting userSettingNew) throws FailUpdateUserSettingException;
}
Kotlin ejb:
@Stateless
@Local(IUserSettingManager::class)
open class UserSettingManager : DataManager(), IUserSettingManager {
private companion object {
private val LOG = LoggerFactory.getLogger(UserSettingManager::class.java)
}
@Throws(FailUpdateUserSettingException::class)
private fun validate(userSetting: UserSetting) {
if (userSetting.avatar?.length ?: 0 > DBConstant.UUID_VARCHAR_SIZE) {
throw FailUpdateUserSettingException("avatar length")
}
}
@Throws(FailUpdateUserSettingException::class)
override fun updateSetting(userId: Long, userSettingNew: UserSetting): UserSettingApi {
val logger = LOG.silentEnter("updateSetting")
try {
validate(userSettingNew)
.....
} catch (ex: Exception) {
val msg = "userId:$userId, user setting:$userSettingNew"
when (ex) {
is FailUpdateUserSettingException -> {
logger.debug("$msg, ex:$ex")
throw ex
}
else -> {
logger.error(msg, ex)
throw FailUpdateUserSettingException(ex.toString())
}
}
}
}
}
带有异常的 Java 类:
公共类 FailUpdateUserSettingException 扩展异常 {
public FailUpdateUserSettingException() {
this(error);
}
}
当尝试使用不正确的数据调用 ejb 时,得到异常 UndeclaredThrowableException,结果事务滚动
Caused by: java.lang.reflect.UndeclaredThrowableException
at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:34)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:52)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.component.interceptors.NonPooledEJBComponentInstanceAssociatingInterceptor.processInvocation(NonPooledEJBComponentInstanceAssociatingInterceptor.java:59) [wildfly-ejb3-10.0.0.Final.jar:10.0.0.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:254) [wildfly-ejb3-10.0.0.Final.jar:10.0.0.Final]
... 137 more
Caused by: com.pay.utils.shared.exception.user.FailUpdateUserSettingException: Fail update user setting. avatar length
at com.pay.manager.UserSettingManager.validate(UserSettingManager.kt:xx)
at com.pay.manager.UserSettingManager.updateSetting(UserSettingManager.kt:xx)
at com.pay.manager.UserSettingManager.updateSetting(UserSettingManager.kt:xx)
.....
结果
javax.ejb.EJBTransactionRolledbackException
【问题讨论】:
-
如何避免这个问题?如果函数是自己编写的,那么解决方案就简单多了。是的,声明该函数将抛出一个检查异常。例如:@Throws(ParseException::class) fun convertToDate(){/**/} 不适合我
-
为什么不适合你?
-
fun validate throw UndeclaredThrowableException,虽然我写了@Throws注解
-
你应该注意到
UndeclaredThrowableException是由jboss而不是你的代码抛出的,这意味着你的代码是好的。