簡體   English   中英

在Transactional方法中捕獲異常后超過了鎖定等待超時

[英]Lock wait timeout exceeded after catch of exception inside Transactional method

嗨,我有一個查詢異常后的恢復過程的問題。 這是代碼(它的主要部分)

@Override
@Async
@Transactional(rollbackFor=Exception.class)
public void modifyFleet(User currentUser, FleetForm fleetForm) throws Exception {
    //Keep the progress status on DB
    fleetServices.setEditingProgress(fleetForm.getIdFleet(), "Start editing fleet");
    Fleet oldFleet = fleetServices.findById(fleetForm.getIdFleet());
    //some INSTRUCTIONS
    if (!(backupFolderFile.mkdirs()))
        throw new FileSystemException("Error making the folder backup");
    try{
        //Keep the progress status on DB
        fleetServices.setEditingProgress(fleetForm.getIdFleet(), "Backup...");
        FileUtils.copyDirectory(fleetFile, new File(backupFolder));

        //Create fleet with new value
        Fleet newFleet = newFleetConstructor(fleetForm, oldFleet);
        //Change operation for all cars of the application
        int i = 0;
        for (Car car:oldFleet.getCars()){
            //some INSTRUCTIONS

            //Keep the progress status on DB
            fleetServices.setEditingProgress(fleetForm.getIdFleet(), "Work on car "+ car.getCarType().getIdCarType() + car.getId()+ " (" + i +" of " + oldFleet.getCars().size() +"): Editing acquisitions file"  );

        }

        fleetServices.setEditingProgress(oldFleet.getIdFleet(), "Updating file system fleet path");
        utils.unSetEditingFleet(oldFleet.getIdFleet());
//          throw new Exception("fleet has been restored Exception");
    }catch(Exception e){
        //Keep the progress status on DB
        fleetServices.setEditingProgress(oldFleet.getIdFleet(), "Sorry an error occured during the procedure, wait until restore is ended!");
        //Restore the file system procedure
        restoreProcedure(oldFleet.getIdFleet(), fleetFile, backupFolderFile);       
        //Keep the progress status on DB
        fleetServices.setEditingProgress(oldFleet.getIdFleet(), "");
        utils.unSetEditingFleet(oldFleet.getIdFleet());
        //Even with this exception set the fleet as not in editing
        throw new EditingException("fleet has been restored!");
    }
}

為了嘗試在unSetEditingFleet期間exception發生時發生的情況,我拋出了一個新異常:

@Transactional
public void unSetEditingFleet(Integer idFleet) throws QueryException {
    try {
        fleetServices.setEditingFleet(idFleet, false);  
        throw new Exception();
//          for(Car car : carServices.findByFleetIdFleet(idFleet)){
//              carServices.setEditingCar(car.getIdCar(), false);   //Unset cars associated with the fleet 
//          }    
    }catch(Exception e){
        throw new QueryException(e);
    }
}

異常捕獲后的指令fleetServices.setEditingProgress會生成異常:

Caused by: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2113)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2049)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2034)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
    ... 54 more 

setEditinProgress的代碼如下:

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW) //necessary to set immediately the text into the database inside a transactional method. This annotation create a new transaction
public void setEditingProgress(Integer idFleet, String editingProgress) {
    fleetRepository.setEditingProgress(idFleet, editingProgress);   
}

這會將操作的進度狀態設置到數據庫中,以便用戶可以知道進度狀態。 為什么僅在捕獲后才收到此異常? 你知道我該怎么解決嗎?

從我發現的信息可以看出,您在數據庫上的事務已超時,因為另一個線程將某個記錄的記錄鎖定時間過長。 檢查哪些其他線程同時在該表上執行操作。 有不同的方式來弄清楚什么塊事務如圖所示這里

如果您正在運行繁忙的數據庫,請使用:

SET GLOBAL innodb_lock_wait_timeout = 5000; 

然后:

SET innodb_lock_wait_timeout = 5000; 

可以作為一種解決方法,但是您的查詢需要進行優化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM