zoukankan      html  css  js  c++  java
  • Checked Exception & Unchecked Exception

    查Spring事务管理时看到一句话:

    Spring使用声明式事务处理,默认情况下,如果被注解的数据库操作方法中发生了unchecked异常,所有的数据库操作将rollback;如果发生的异常是checked异常,默认情况下数据库操作还是会提交的。

    那么,什么是Checked Exception & Unchecked Exception ?

    Unchecked Exception:
    a. 指的是程序的瑕疵或逻辑错误,并且在运行时无法恢复。
    b. 包括Error与RuntimeException及其子类,如:OutOfMemoryError, UndeclaredThrowableException, IllegalArgumentException, IllegalMonitorStateException, NullPointerException, IllegalStateException, IndexOutOfBoundsException等。
    c. 语法上不需要声明抛出异常。
    
    Checked Exception:
    a. 代表程序不能直接控制的无效外界情况(如用户输入,数据库问题,网络异常,文件丢失等)
    b. 除了Error和RuntimeException及其子类之外,如:ClassNotFoundException, NamingException, ServletException, SQLException, IOException等。
    c. 需要try catch处理或throws声明抛出异常。 


    但是,为毛"Spring使用声明式事务处理,默认情况下,如果被注解的数据库操作方法中发生了unchecked异常,所有的数据库操作将rollback;如果发生的异常是checked异常,默认情况下数据库操作还是会提交的。"?

    另外,@Transactional()

    org.springframework.transaction.support.TransactionTemplate

        @Override
        public <T> T execute(TransactionCallback<T> action) throws TransactionException {
            if (this.transactionManager instanceof CallbackPreferringPlatformTransactionManager) {
                return ((CallbackPreferringPlatformTransactionManager) this.transactionManager).execute(this, action);
            }
            else {
                TransactionStatus status = this.transactionManager.getTransaction(this);
                T result;
                try {
                    result = action.doInTransaction(status);
                }
                catch (RuntimeException ex) {
                    // Transactional code threw application exception -> rollback
                    rollbackOnException(status, ex);
                    throw ex;
                }
                catch (Error err) {
                    // Transactional code threw error -> rollback
                    rollbackOnException(status, err);
                    throw err;
                }
                catch (Exception ex) {
                    // Transactional code threw unexpected exception -> rollback
                    rollbackOnException(status, ex);
                    throw new UndeclaredThrowableException(ex, "TransactionCallback threw undeclared checked exception");
                }
                this.transactionManager.commit(status);
                return result;
            }
        }

    如何改变默认规则:
      1, 让checked exception也回滚: @Transactional(rollbackFor=Exception.class)
      2, 让unchecked exception不回滚: @Transactional(notRollbackFor=RunTimeException.class)
      3, 不需要事务管理的(只查询的)方法: @Transactional(propagation=Propagation.NOT_SUPPORTED)

  • 相关阅读:
    第二周总结
    第一次结组作业概述
    Spring_Bean的自动装配
    Spring_依赖注入
    Spring_配置
    第一周总结
    1680. Concatenation of Consecutive Binary Numbers
    1631. Path With Minimum Effort
    1657. Determine if Two Strings Are Close
    1673. Find the Most Competitive Subsequence
  • 原文地址:https://www.cnblogs.com/larryzeal/p/5477116.html
Copyright © 2011-2022 走看看