SpringBootオープントランザクション、手動トランザクション


記事は個人ブログ優先ですよhttps://www.xdx97.com/article/754707243290591232
一、@Transactional
1-1、使用
クラスまたはメソッドに@Transactional注釈を付けるだけで、トランザクションを開始できます.
1-2、@Transactionalのプロパティ
属性名
説明
name
コンフィギュレーション・ファイルに複数のTransactionManagerがある場合、このプロパティを使用して、どのトランザクション・マネージャを選択するかを指定します.
propagation
トランザクションの伝播動作、デフォルト値はREQUIRDです.REQUIRD:現在のトランザクションをサポートし、現在トランザクションがない場合は新規トランザクションを作成します.これは最も一般的な選択です.SUPPORTS:現在のトランザクションをサポートし、現在トランザクションがない場合は非トランザクションで実行します.MANDATORY:現在のトランザクションをサポートし、現在トランザクションがない場合は例外を放出します.REQUIRES_NEW:新規トランザクションを作成し、現在トランザクションが存在する場合は、現在のトランザクションを保留します.NOT_SUPPORTED:非トランザクションで実行し、現在トランザクションが存在する場合は、現在のトランザクションを保留します.NEVER:非トランザクションで実行され、現在トランザクションが存在する場合は例外が放出されます.NESTED:現在のトランザクションをサポートし、現在のトランザクションが存在する場合はネストされたトランザクションを実行し、現在トランザクションが存在しない場合は新規トランザクションを作成します.
isolation
トランザクションの独立性、デフォルトはDEFAULTです.READ_UNCOMMITTED:読み取りがコミットされていないため、汚れた読み取り、幻読み取り、繰り返し不可能READ_COMMITTED:読み取りがコミットされているため、幻読み、リピート不可REPEATABLE_READ:繰り返し読み可能、幻読みSERIALIZABLE:最高の隔離レベル、事務に厳格に従うACID
timeout
トランザクションのタイムアウト時間.デフォルトは-1です.この時間制限を超えてもトランザクションが完了していない場合は、トランザクションが自動的にロールバックされます.
readOnly
トランザクションが読み取り専用であるかどうかを指定し、デフォルトはfalseであり、trueの場合は読み取り専用です.
rollbackFor
ロールバックできる例外を指定します.デフォルトでは、実行時の例外またはそのサブクラスのみがサポートされます.
noRollbackFor
例外が発生したトランザクションをロールバックしないことを指定します.
1-3、注意事項
  • @Transactionalはpublicメソッド
  • にのみ使用できます.
  • 親メソッドにトランザクションを追加すると、呼び出しサブメソッドが例外を投げ出すようにロールバックします.たとえば、次のようなコード
  • です.
    @Transactional(rollbackFor = {
         Exception.class})
    public String exportExcel() throws IOException {
         
       testMapper.insertOne((int)(Math.random()*100));
       fun();
       return "dada";
    }
    public void fun(){
         
       testMapper.insertTwo((int)(Math.random()*100));
       fun1();
    }
    public void fun1(){
         
       testMapper.insertTwo((int)(Math.random()*100));
       int i = 1 / 0;
    }
    
  • サブメソッドにトランザクション注釈を付けると無効です.たとえば、次のような
  • です.
    public String exportExcel() throws IOException {
         
        testMapper.insertOne((int)(Math.random()*100));
        fun();
        return "dada";
    }
    @Transactional(rollbackFor = {
         Exception.class})
    public void fun(){
         
        testMapper.insertTwo((int)(Math.random()*100));
        fun1();
    }
    @Transactional(rollbackFor = {
         Exception.class})
    public void fun1(){
         
        testMapper.insertTwo((int)(Math.random()*100));
        int i = 1 / 0;
    }
    

    二、手動で取引をロールバックする
    コードをtry catchで処理する必要がある場合、またはトランザクションを手動で制御したい場合は、次のコードを使用してトランザクションを手動でロールバックできます.
    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    

    トランザクションをロールバックする前に、@Transactional注釈を使用してトランザクションを開きます.
    トランザクションを手動でロールバックする場合は、このトランザクション内でロールバックコードが実行されている限り、例外が発生していることを気にする必要はありません.注:トランザクションを開くには、親メソッドの中にある必要があります.
    次のコードのtry catchはいずれもロールバックできますが、トランザクションを開く注釈は親メソッドでのみ使用できます.
    @Transactional
    public String exportExcel() {
         
        testMapper.insertOne((int)(Math.random()*100));
        fun();
        try {
         
            throw new IOException("243");
        }catch (Exception e){
         
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "dada";
    }
    public void fun() {
         
        testMapper.insertTwo((int)(Math.random()*100));
        fun1();
        try {
         
            throw new IOException("243");
        }catch (Exception e){
         
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
    
    public void fun1() {
         
        testMapper.insertTwo((int)(Math.random()*100));
        try {
         
            throw new IOException("243");
        }catch (Exception e){
         
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }