主頁 > 後端開發 > Spring事務的那些坑,這里都給你總結好了!

Spring事務的那些坑,這里都給你總結好了!

2020-11-22 17:47:15 後端開發

Spring框架已是JAVA專案的標配,其中Spring事務管理也是最常用的一個功能,但如果不了解其實作原理,使用姿勢不對,一不小心就可能掉坑里,

為了更透徹的說明這些坑,本文分四部分展開闡述:第一部分簡單介紹下Spring事務集成的幾種方式;第二部分結合Spring源代碼說明Spring事務的實作原理;第三部分通過實際測驗代碼介紹關于Spring事務的坑;第四部分是對本文的總結,

一、Spring事務管理的幾種方式:

Spring事務在具體使用方式上可分為兩大類:

1.  宣告式

  • 基于 TransactionProxyFactoryBean的宣告式事務管理
  • 基于 <tx><aop> 命名空間的事務管理
  • 基于 @Transactional 的宣告式事務管理

2.  編程式

  • 基于事務管理器API 的編程式事務管理
  • 基于TransactionTemplate 的編程式事務管理

目前大部分專案使用的是宣告式的后兩種:

  • 基于 <tx><aop> 命名空間的宣告式事務管理可以充分利用切點運算式的強大支持,使得管理事務更加靈活,
  • 基于 @Transactional 的方式需要實施事務管理的方法或者類上使用 @Transactional 指定事務規則即可實作事務管理,在Spring Boot中通常也建議使用這種注解方式來標記事務,

二、Spring事務實作機制

接下來我們詳細看下Spring事務的源代碼,進而了解其作業原理,我們從<tx>標簽的決議類開始:

@Override
public void init() {
        registerBeanDefinitionParser("advice", new TxAdviceBeanDefinitionParser());
        registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenBeanDefinitionParser());
        registerBeanDefinitionParser("jta-transaction-manager", new JtaTransactionManagerBeanDefinitionParser());
    }
}
class TxAdviceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
    @Override
    protected Class<?> getBeanClass(Element element) {
        return TransactionInterceptor.class;
    }
}

由此可看到Spring事務的核心實作類TransactionInterceptor及其父類TransactionAspectSupport,其實作了事務的開啟、資料庫操作、事務提交、回滾等,我們平時在開發時如果想確定是否在事務中,也可以在該方法進行斷點除錯,

TransactionInterceptor:

public Object invoke(final MethodInvocation invocation) throws Throwable {
        Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);

        // Adapt to TransactionAspectSupport's invokeWithinTransaction...
        return invokeWithinTransaction(invocation.getMethod(), targetClass, new InvocationCallback() {
            @Override
            public Object proceedWithInvocation() throws Throwable {
                return invocation.proceed();
            }
        });
    }

TransactionAspectSupport

protected Object invokeWithinTransaction(Method method, Class<?> targetClass, final InvocationCallback invocation)
            throws Throwable {

        // If the transaction attribute is null, the method is non-transactional.
        final TransactionAttribute txAttr = getTransactionAttributeSource().getTransactionAttribute(method, targetClass);
        final PlatformTransactionManager tm = determineTransactionManager(txAttr);
        final String joinpointIdentification = methodIdentification(method, targetClass, txAttr);

        if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
            // Standard transaction demarcation with getTransaction and commit/rollback calls.
            TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);
            Object retVal = null;
            try {
                // This is an around advice: Invoke the next interceptor in the chain.
                // This will normally result in a target object being invoked.
                retVal = invocation.proceedWithInvocation();
            }
            catch (Throwable ex) {
                // target invocation exception
                completeTransactionAfterThrowing(txInfo, ex);
                throw ex;
            }
            finally {
                cleanupTransactionInfo(txInfo);
            }
            commitTransactionAfterReturning(txInfo);
            return retVal;
        }
}

至此我們了解事務的整個呼叫流程,但還有一個重要的機制沒分析到,那就是Spring 事務針對不同的傳播級別控制當前獲取的資料庫連接,接下來我們看下Spring獲取連接的工具類DataSourceUtils,JdbcTemplate、Mybatis-Spring也都是通過該類獲取Connection,

public abstract class DataSourceUtils {
…
public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException {
        try {
            return doGetConnection(dataSource);
        }
        catch (SQLException ex) {
            throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);
        }
    }

public static Connection doGetConnection(DataSource dataSource) throws SQLException {
        Assert.notNull(dataSource, "No DataSource specified");

        ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
        if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) {
            conHolder.requested();
            if (!conHolder.hasConnection()) {
                logger.debug("Fetching resumed JDBC Connection from DataSource");
                conHolder.setConnection(dataSource.getConnection());
            }
            return conHolder.getConnection();
        }
…
}

TransactionSynchronizationManager也是一個事務同步管理的核心類,它實作了事務同步管理的職能,包括記錄當前連接持有connection holder,

 

TransactionSynchronizationManager

private static final ThreadLocal<Map<Object, Object>> resources =
            new NamedThreadLocal<Map<Object, Object>>("Transactional resources");
…
public static Object getResource(Object key) {
        Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
        Object value = doGetResource(actualKey);
        if (value != null && logger.isTraceEnabled()) {
            logger.trace("Retrieved value [" + value + "] for key [" + actualKey + "] bound to thread [" +
                    Thread.currentThread().getName() + "]");
        }
        return value;
    }

    /**
     * Actually check the value of the resource that is bound for the given key.
     */
    private static Object doGetResource(Object actualKey) {
        Map<Object, Object> map = resources.get();
        if (map == null) {
            return null;
        }
        Object value = map.get(actualKey);
        // Transparently remove ResourceHolder that was marked as void...
        if (value instanceof ResourceHolder && ((ResourceHolder) value).isVoid()) {
            map.remove(actualKey);
            // Remove entire ThreadLocal if empty...
            if (map.isEmpty()) {
                resources.remove();
            }
            value = null;
        }
        return value;
    }

在事務管理器類AbstractPlatformTransactionManager中,getTransaction獲取事務時,會處理不同的事務傳播行為,例如當前存在事務,但呼叫方法事務傳播級別為REQUIRES_NEW、PROPAGATION_NOT_SUPPORTED時,對當前事務進行掛起、恢復等操作,以此保證了當前資料庫操作獲取正確的Connection,

具體是在子事務提交的最后會將掛起的事務恢復,恢復時重新呼叫TransactionSynchronizationManager. bindResource設定之前的connection holder,這樣再獲取的連接就是被恢復的資料庫連接, TransactionSynchronizationManager當前激活的連接只能是一個,

AbstractPlatformTransactionManager

   private TransactionStatus handleExistingTransaction(
            TransactionDefinition definition, Object transaction, boolean debugEnabled)
            throws TransactionException {
…
        if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW) {
            if (debugEnabled) {
                logger.debug("Suspending current transaction, creating new transaction with name [" +
                        definition.getName() + "]");
            }
            SuspendedResourcesHolder suspendedResources = suspend(transaction);
            try {
                boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
                DefaultTransactionStatus status = newTransactionStatus(
                        definition, transaction, true, newSynchronization, debugEnabled, suspendedResources);
                doBegin(transaction, definition);
                prepareSynchronization(status, definition);
                return status;
            }
            catch (RuntimeException beginEx) {
                resumeAfterBeginException(transaction, suspendedResources, beginEx);
                throw beginEx;
            }
            catch (Error beginErr) {
                resumeAfterBeginException(transaction, suspendedResources, beginErr);
                throw beginErr;
            }
        }
/**
     * Clean up after completion, clearing synchronization if necessary,
     * and invoking doCleanupAfterCompletion.
     * @param status object representing the transaction
     * @see #doCleanupAfterCompletion
     */
    private void cleanupAfterCompletion(DefaultTransactionStatus status) {
        status.setCompleted();
        if (status.isNewSynchronization()) {
            TransactionSynchronizationManager.clear();
        }
        if (status.isNewTransaction()) {
            doCleanupAfterCompletion(status.getTransaction());
        }
        if (status.getSuspendedResources() != null) {
            if (status.isDebug()) {
                logger.debug("Resuming suspended transaction after completion of inner transaction");
            }
            resume(status.getTransaction(), (SuspendedResourcesHolder) status.getSuspendedResources());
        }
    }

Spring的事務是通過AOP代理類中的一個Advice(TransactionInterceptor)進行生效的,而傳播級別定義了事務與子事務獲取連接、事務提交、回滾的具體方式,

AOP(Aspect Oriented Programming),即面向切面編程,Spring AOP技術實作上其實就是代理類,具體可分為靜態代理和動態代理兩大類,其中靜態代理是指使用 AOP 框架提供的命令進行編譯,從而在編譯階段就可生成 AOP 代理類,因此也稱為編譯時增強;(AspectJ);而動態代理則在運行時借助于 默寫類別庫在記憶體中“臨時”生成 AOP 動態代理類,因此也被稱為運行時增強,其中java是使用的動態代理模式 (JDK+CGLIB),

JDK動態代理 JDK動態代理主要涉及到java.lang.reflect包中的兩個類:Proxy和InvocationHandler,InvocationHandler是一個介面,通過實作該介面定義橫切邏輯,并通過反射機制呼叫目標類的代碼,動態將橫切邏輯和業務邏輯編制在一起,Proxy利用InvocationHandler動態創建一個符合某一介面的實體,生成目標類的代理物件,

CGLIB動態代理 CGLIB全稱為Code Generation Library,是一個強大的高性能,高質量的代碼生成類別庫,可以在運行期擴展Java類與實作Java介面,CGLIB封裝了asm,可以再運行期動態生成新的class,和JDK動態代理相比較:JDK創建代理有一個限制,就是只能為介面創建代理實體,而對于沒有通過介面定義業務方法的類,則可以通過CGLIB創建動態代理,

搜索Java知音公眾號,回復“后端面試”,送你一份Java面試題寶典.pdf

CGLIB 創建代理的速度比較慢,但創建代理后運行的速度卻非常快,而 JDK 動態代理正好相反,如果在運行的時候不斷地用 CGLIB 去創建代理,系統的性能會大打折扣,因此如果有介面,Spring默認使用JDK 動態代理,源代碼如下:

public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {

    @Override
    public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
        if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
            Class<?> targetClass = config.getTargetClass();
            if (targetClass == null) {
                throw new AopConfigException("TargetSource cannot determine target class: " +
                        "Either an interface or a target is required for proxy creation.");
            }
            if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
                return new JdkDynamicAopProxy(config);
            }
            return new ObjenesisCGLIBAopProxy(config);
        }   
        else {
            return new JdkDynamicAopProxy(config);
        }
    }
}

在了解Spring代理的兩種特點后,我們也就知道在做事務切面配置時的一些注意事項,例如JDK代理時方法必須是public,CGLIB代理時必須是public、protected,且類不能是final的;在依賴注入時,如果屬性型別定義為實作類,JDK代理時會報如下注入例外:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.wwb.test.TxTestAop': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'stockService' is expected to be of type 'com.wwb.service.StockProcessServiceImpl' but was actually of type 'com.sun.proxy.$Proxy14'

但如果修改為CGLIB代理時則會成功注入,所以如果有介面,建議注入時該類屬性都定義為介面,另外事務切點都配置在實作類和介面都可以生效,但建議加在實作類上,

官網關于Spring AOP的詳細介紹

https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html%23aop

三、Spring事務的那些坑

通過之前章節,相信您已經掌握了spring事務的使用方式與原理,不過還是要注意,因為一不小心就可能就掉坑,首先看第一個坑:

3.1 事務不生效

測驗代碼,事務AOP配置:

<tx:advice id="txAdvice" transaction-manager="myTxManager">
        <tx:attributes>
            <!-- 指定在連接點方法上應用的事務屬性 -->
            <tx:method name="openAccount" isolation="DEFAULT" propagation="REQUIRED"/>
            <tx:method name="openStock" isolation="DEFAULT" propagation="REQUIRED"/>
            <tx:method name="openStockInAnotherDb" isolation="DEFAULT" propagation="REQUIRES_NEW"/>
            <tx:method name="openTx" isolation="DEFAULT" propagation="REQUIRED"/>
            <tx:method name="openWithoutTx" isolation="DEFAULT" propagation="NEVER"/>
            <tx:method name="openWithMultiTx" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:advice>
public class StockProcessServiceImpl implements IStockProcessService{
@Autowired
     private IAccountDao accountDao;
    @Autowired
     private IStockDao stockDao;
    
    @Override
    public void openAccount(String aname, double money) {
        accountDao.insertAccount(aname, money);
    }

    @Override
    public void openStock(String sname, int amount) {
        stockDao.insertStock(sname, amount);
    }
    
    @Override
    public void openStockInAnotherDb(String sname, int amount) {
        stockDao.insertStock(sname, amount);
}
}
public void insertAccount(String aname, double money) {
        String sql = "insert into account(aname, balance) values(?,?)";
        this.getJdbcTemplate().update(sql, aname, money);
        DbUtils.printDBConnectionInfo("insertAccount",getDataSource());
} 

    public void insertStock(String sname, int amount) {
        String sql = "insert into stock(sname, count) values (?,?)";
        this.getJdbcTemplate().update(sql , sname, amount);
        DbUtils.printDBConnectionInfo("insertStock",getDataSource());
}

    public static void printDBConnectionInfo(String methodName,DataSource ds) {
        Connection connection = DataSourceUtils.getConnection(ds);
        System.out.println(methodName+" connection hashcode="+connection.hashCode());
    }
//呼叫同類方法,外圍配置事務
    public void openTx(String aname, double money) {
            openAccount(aname,money);
            openStock(aname,11);
    }

1.運行輸出:

insertAccount connection hashcode=319558327 insertStock connection hashcode=319558327

//呼叫同類方法,外圍未配置事務
    public void openWithoutTx(String aname, double money) {
        openAccount(aname,money);
            openStock(aname,11);
    }

2.運行輸出:

insertAccount connection hashcode=1333810223 insertStock connection hashcode=1623009085

//通過AopContext.currentProxy()方法獲取代理
@Override
public void openWithMultiTx(String aname, double money) {
openAccount(aname,money);  
openStockInAnotherDb(aname, 11);//傳播級別為REQUIRES_NEW
}

3.運行輸出:

insertAccount connection hashcode=303240439 insertStock connection hashcode=303240439

可以看到2、3測驗方法跟我們事務預期并一樣,結論:呼叫方法未配置事務、本類方法直接呼叫,事務都不生效!

究其原因,還是因為Spring的事務本質上是個代理類,而本類方法直接呼叫時其物件本身并不是織入事務的代理,所以事務切面并未生效,具體可以參見#Spring事務實作機制#章節,

Spring也提供了判斷是否為代理的方法:

public static void printProxyInfo(Object bean) {
        System.out.println("isAopProxy"+AopUtils.isAopProxy(bean));
        System.out.println("isCGLIBProxy="+AopUtils.isCGLIBProxy(bean));
        System.out.println("isJdkProxy="+AopUtils.isJdkDynamicProxy(bean));
    }

那如何修改為代理類呼叫呢?最直接的想法是注入自身,代碼如下:

    @Autowired
    private IStockProcessService stockProcessService;
//注入自身類,回圈依賴,親測可以 
    public void openTx(String aname, double money) {
            stockProcessService.openAccount(aname,money);
            stockProcessService.openStockInAnotherDb (aname,11);
    }

當然Spring提供了獲取當前代理的方法:代碼如下:

//通過AopContext.currentProxy()方法獲取代理
    @Override
    public void openWithMultiTx(String aname, double money) {
((IStockProcessService)AopContext.currentProxy()).openAccount(aname,money);

((IStockProcessService)AopContext.currentProxy()).openStockInAnotherDb(aname, 11);
    }

另外Spring是通過TransactionSynchronizationManager類中執行緒變數來獲取事務中資料庫連接,所以如果是多執行緒呼叫或者繞過Spring獲取資料庫連接,都會導致Spring事務配置失效,

最后Spring事務配置失效的場景:

  1. 事務切面未配置正確
  2. 本類方法呼叫
  3. 多執行緒呼叫
  4. 繞開Spring獲取資料庫連接

接下來我們看下Spring的事務的另外一個坑:

3.2 事務不回滾

測驗代碼:

<tx:advice id="txAdvice" transaction-manager="myTxManager">
        <tx:attributes>
            <!-- 指定在連接點方法上應用的事務屬性 -->
            <tx:method name="buyStock" isolation="DEFAULT" propagation="REQUIRED"/>
        </tx:attributes>
</tx:advice>
public void buyStock(String aname, double money, String sname, int amount) throws StockException {
        boolean isBuy = true;
        accountDao.updateAccount(aname, money, isBuy);
        // 故意拋出例外
        if (true) {
            throw new StockException("購買股票例外");
        }
        stockDao.updateStock(sname, amount, isBuy);
    }
    @Test
    public void testBuyStock() {
        try {
            service.openAccount("dcbs", 10000);
            service.buyStock("dcbs", 2000, "dap", 5);
        } catch (StockException e) {
            e.printStackTrace();
        }
        double accountBalance = service.queryAccountBalance("dcbs");
        System.out.println("account balance is " + accountBalance);
    }

輸出結果:

insertAccount connection hashcode=656479172 updateAccount connection hashcode=517355658 account balance is 8000.0

應用拋出例外,但accountDao.updateAccount卻進行了提交,究其原因,直接看Spring源代碼:

TransactionAspectSupport

protected void completeTransactionAfterThrowing(TransactionInfo txInfo, Throwable ex) {
        if (txInfo != null && txInfo.hasTransaction()) {
            if (logger.isTraceEnabled()) {
                logger.trace("Completing transaction for [" + txInfo.getJoinpointIdentification() +
                        "] after exception: " + ex);
            }
            if (txInfo.transactionAttribute.rollbackOn(ex)) {
                try {
                    txInfo.getTransactionManager().rollback(txInfo.getTransactionStatus());
                }
                catch (TransactionSystemException ex2) {
                    logger.error("Application exception overridden by rollback exception", ex);
                    ex2.initApplicationException(ex);
                    throw ex2;
                }
                …
}

public class DefaultTransactionAttribute extends DefaultTransactionDefinition implements TransactionAttribute {
@Override
    public boolean rollbackOn(Throwable ex) {
        return (ex instanceof RuntimeException || ex instanceof Error);
    }
…
}

由代碼可見,Spring事務默認只對RuntimeException和Error進行回滾,如果應用需要對指定的例外類進行回滾,可配置rollback-for=屬性,例如:

    <!-- 注冊事務通知 -->
    <tx:advice id="txAdvice" transaction-manager="myTxManager">
        <tx:attributes>
            <!-- 指定在連接點方法上應用的事務屬性 -->
            <tx:method name="buyStock" isolation="DEFAULT" propagation="REQUIRED" rollback-for="StockException"/>
        </tx:attributes>
    </tx:advice>

事務不回滾的原因:

  1. 事務配置切面未生效
  2. 應用方法中將例外捕獲
  3. 拋出的例外不屬于運行時例外(例如IOException),
  4. rollback-for屬性配置不正確

接下來我們看下Spring事務的第三個坑:

3.3 事務超時不生效

測驗代碼:

<!-- 注冊事務通知 -->
    <tx:advice id="txAdvice" transaction-manager="myTxManager">
        <tx:attributes>
             <tx:method name="openAccountForLongTime" isolation="DEFAULT" propagation="REQUIRED" timeout="3"/>
        </tx:attributes>
    </tx:advice>
@Override
    public void openAccountForLongTime(String aname, double money) {
        accountDao.insertAccount(aname, money);
        try {
            Thread.sleep(5000L);//在資料庫操作之后超時
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void testTimeout() {
        service.openAccountForLongTime("dcbs", 10000);
    }

正常運行,事務超時未生效

public void openAccountForLongTime(String aname, double money) {
        try {
            Thread.sleep(5000L); //在資料庫操作之前超時
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        accountDao.insertAccount(aname, money);
    }

拋出事務超時例外,超時生效

org.springframework.transaction.TransactionTimedOutException: Transaction timed out: deadline was Fri Nov 23 17:03:02 CST 2018 at org.springframework.transaction.support.ResourceHolderSupport.checkTransactionTimeout(ResourceHolderSupport.java:141) …

通過原始碼看看Spring事務超時的判斷機制:

ResourceHolderSupport

/**
     * Return the time to live for this object in milliseconds.
     * @return number of millseconds until expiration
     * @throws TransactionTimedOutException if the deadline has already been reached
     */
    public long getTimeToLiveInMillis() throws TransactionTimedOutException{
        if (this.deadline == null) {
            throw new IllegalStateException("No timeout specified for this resource holder");
        }
        long timeToLive = this.deadline.getTime() - System.currentTimeMillis();
        checkTransactionTimeout(timeToLive <= 0);
        return timeToLive;
    }

    /**
     * Set the transaction rollback-only if the deadline has been reached,
     * and throw a TransactionTimedOutException.
     */
    private void checkTransactionTimeout(boolean deadlineReached) throws TransactionTimedOutException {
        if (deadlineReached) {
            setRollbackOnly();
            throw new TransactionTimedOutException("Transaction timed out: deadline was " + this.deadline);
        }
    }

通過查看getTimeToLiveInMillis方法的Call Hierarchy,可以看到被DataSourceUtils的applyTimeout所呼叫, 繼續看applyTimeout的Call Hierarchy,可以看到有兩處呼叫,一個是JdbcTemplate,一個是TransactionAwareInvocationHandler類,后者是只有TransactionAwareDataSourceProxy類呼叫,該類為DataSource的事務代理類,我們一般并不會用到,難道超時只能在這呼叫JdbcTemplate中生效?寫代碼親測:

    <!-- 注冊事務通知 -->
    <tx:advice id="txAdvice" transaction-manager="myTxManager">
        <tx:attributes>
            <tx:method name="openAccountForLongTimeWithoutJdbcTemplate" isolation="DEFAULT" propagation="REQUIRED" timeout="3"/>
        </tx:attributes>
    </tx:advice>
    public void openAccountForLongTimeWithoutJdbcTemplate(String aname, double money) {
        try {
            Thread.sleep(5000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        accountDao.queryAccountBalanceWithoutJdbcTemplate(aname);
    }
    public double queryAccountBalanceWithoutJdbcTemplate(String aname) {
           String sql = "select balance from account where aname = ?";
           PreparedStatement prepareStatement;
        try {
            prepareStatement = this.getConnection().prepareStatement(sql);
               prepareStatement.setString(1, aname);
               ResultSet executeQuery = prepareStatement.executeQuery();
               while(executeQuery.next()) {
                   return executeQuery.getDouble(1);
               }
        } catch (CannotGetJdbcConnectionException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }

運行正常,事務超時失效

由上可見:Spring事務超時判斷在通過JdbcTemplate的資料庫操作時,所以如果超時后未有JdbcTemplate方法呼叫,則無法準確判斷超時,另外也可以得知,如果通過Mybatis等操作資料庫,Spring的事務超時是無效的,鑒于此,Spring的事務超時謹慎使用,

搜索Java知音公眾號,回復“后端面試”,送你一份Java面試題寶典.pdf

四、 總結

JDBC規范中Connection 的setAutoCommit是原生控制手動事務的方法,但傳播行為、例外回滾、連接管理等很多技術問題都需要開發者自己處理,而Spring事務通過AOP方式非常優雅的屏蔽了這些技術復雜度,使得事務管理變的例外簡單,

但凡事有利弊,如果對實作機制理解不透徹,很容易掉坑里,最后總結下Spring事務的可能踩的坑:

1.  Spring事務未生效

  • 呼叫方法本身未正確配置事務
  • 本類方法直接呼叫
  • 資料庫操作未通過Spring的DataSourceUtils獲取Connection
  • 多執行緒呼叫

2.  Spring事務回滾失效

  • 未準確配置rollback-for屬性
  • 例外類不屬于RuntimeException與Error
  • 應用捕獲了例外未拋出

3.  Spring事務超時不準確或失效

  • 超時發生在最后一次JdbcTemplate操作之后
  • 通過非JdbcTemplate操作資料庫,例如Mybatis

Java 的知識面非常廣,面試問的涉及也非常廣泛,重點包括:Java 基礎、Java 并發,JVM、MySQL、資料結構、演算法、Spring、微服務、MQ 等等,涉及的知識點何其龐大,所以我們在復習的時候也往往無從下手,今天小編給大家帶來一套 Java 面試題,題庫非常全面,包括 Java 基礎、Java 集合、JVM、Java 并發、Spring全家桶、Redis、MySQL、Dubbo、Netty、MQ 等等,包含 Java 后端知識點 2000 +

資料獲取方式:關注公眾號:“程式員白楠楠”獲取上述資料

 

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/226402.html

標籤:Java

上一篇:Spring事務的那些坑,這里都給你總結好了!

下一篇:Redis原始碼剖析之跳表(skiplist)

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more