me.danwi.sqlex.spring.SpringExceptionTranslator Maven / Gradle / Ivy
package me.danwi.sqlex.spring;
import me.danwi.sqlex.core.ExceptionTranslator;
import me.danwi.sqlex.core.exception.SqlExUndeclaredException;
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
import org.springframework.jdbc.support.SQLExceptionTranslator;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
* 由spring框架支持都异常翻译
*
* {@link SQLException}将通过转换成spring标准的{@link org.springframework.dao.DataAccessException}
*
*
{@link RuntimeException}分类异常保持不变
*
*
其他的Checked异常,将全部转换成{@link SqlExUndeclaredException}
*/
public class SpringExceptionTranslator implements ExceptionTranslator {
private final SQLExceptionTranslator springTranslator;
SpringExceptionTranslator(DataSource dataSource) {
springTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource);
}
@Override
public RuntimeException translate(Exception ex) {
if (ex instanceof SQLException)
return springTranslator.translate("SqlEx", "", (SQLException) ex);
else if (ex instanceof RuntimeException)
return (RuntimeException) ex;
else
return new SqlExUndeclaredException(ex);
}
}