无法在JPA中的异常块中提交事务

时间:2017-02-10 00:27:44

标签: java spring jpa spring-boot spring-transactions

我有服务类,它作为单个事务执行两个db事务。如果发生异常,我想做一些其他数据库插入。我希望以上所有操作都是一次交易。

Follwing是服务类

package edu.unomaha.ist.bioinformatics.serviceimpl;

import org.apache.log4j.Logger;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;



@Service
public class DbOPerationsImpl implements DbOperations {

static Logger log = Logger.getLogger(SequenceValidation.class.getName());


@Autowired
private SequencesRestRepository seqrepo;

@Autowired
private SequenceNamesRepo seqnamerepo;


@Transactional(propagation = Propagation.REQUIRED,noRollbackFor = Exception.class)
public void insertsequence(int dbid, String active, String sequence, String hash, int i3bio, int regid, int version,String genename) {

    int seqid=0;
    SequencesRest seqobj=null;
    boolean isDuplicate=false;
    try {

        System.out.println("before insert");
        try{
            seqrepo.insertsequence(dbid,active,sequence , hash, i3bio, regid, version);
        }
        catch(DataIntegrityViolationException ex){
            isDuplicate=true;

            System.out.println("entered DataIntegrityViolationException blok");

            System.out.println("exception occured");
            //seqobj=seqrepo.findByData(sequence);
            seqobj=seqrepo.getSequenceRestData(sequence);

            seqid=seqobj.getSequencesId();
            seqnamerepo.insertsequencename(genename, version, seqid);



            System.out.println("After printing cause");
            log.error("Exception for the sequence " + sequence);





        }
        if(!isDuplicate){

            seqobj=seqrepo.findByData(sequence);
            seqid=seqobj.getSequencesId();

            seqnamerepo.insertsequencename(genename, version, seqid);
        }



    }
    catch(DataIntegrityViolationException ex){

        System.out.println("entered DataIntegrityViolationException blok");

        System.out.println("exception occured");
        seqobj=seqrepo.getSequenceRestData(sequence);

        seqid=seqobj.getSequencesId();
        seqnamerepo.insertsequencename(genename, version, seqid);



        System.out.println("After printing cause");
        log.error("Exception for the sequence " + sequence);




    }
    catch(ConstraintViolationException ex){

        System.out.println("entered ConstraintViolationException blok");


            System.out.println("exception occured");
            seqobj=seqrepo.findByData(sequence);

            seqobj=seqrepo.findByData(sequence);
            seqid=seqobj.getSequencesId();
            seqnamerepo.insertsequencename(genename, version, seqid);



        System.out.println("After printing cause");
        log.error("Exception for the sequence " + sequence);
        //log.error("Excpetion occured whle inserting into DB", ex);
        //  throw ex;




    }


    catch (Exception ex){



        System.out.println("After printing cause");
        log.error("Exception for the sequence " + sequence);
        //  log.error("Excpetion occured whle inserting into DB", ex);


    }


}



}

这是我的存储库类

package edu.unomaha.ist.bioinformatics.repository;



@Repository public interface SequenceNamesRepo extends JpaRepository<SequencesNamesRest,Serializable>{

@Modifying
@Query(nativeQuery=true, value=ApplicationQueries.INSERTSEQUENCENAMEREST)
public void insertsequencename(String name,int version,int id);

}

我的逻辑是,我在表中有一个可能抛出DataIntigrityException的唯一列。因此,当它与方法seqrepo.insertsequence一起发生时,我尝试在另一个表中获取序列和数据插入。但是插入另一张表并没有发生。当没有异常时,一切正常,数据被插入两个表中(请检查if(!isDuplicate)下的代码)。只有当我得到异常时,才会在第二个表中提交数据。

我收到以下异常

before insert
Hibernate: INSERT INTO        sequences_rest(dbs_id,active,data,seq_hash,i3bio,regions_regions_id,crtn_d    ate,lst_updt,version)VALUES (?,?,?,md5(?),?,?,SYSDATE(),SYSDATE(),?);
2017-02-09 18:04:18.089  WARN 21470 --- [nio-8080-exec-6]     o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1062, SQLState:    23000
2017-02-09 18:04:18.089 ERROR 21470 --- [nio-8080-exec-6]  o.h.engine.jdbc.spi.SqlExceptionHelper   : Duplicate entry   'b3e9f55904def1290e14209c370f6a08' for key 'seq_hash_UNIQUE'
entered DataIntegrityViolationException blok
exception occured
Hibernate: SELECT * FROM sequences_rest where seq_hash=md5(?)
Hibernate: INSERT INTO     sequences_names_rest(name,crtn_date,lst_updt,version,sequences_id)VALUES(?    ,SYSDATE(),SYSDATE(),?,?);
After printing cause
2017-02-09 18:04:46.074 ERROR 21470 --- [nio-8080-exec-6]     e.u.i.b.utils.SequenceValidation         : Exception for the sequence    GGGAGCTGTCGAAGGTGGGATCGGCGATTGGGACGAAGTCGTAACAAGGTAGCCGTACCGGAAGGTGCGGCTGG    ATCACCTCCTTTCTAAGGAGCACCATTTCCCAGTCGAATGAACTAGGGAACATAAAGTAGGCATCTGTAGTGGA TATCTACTTGGTGAATATGTTTTGTAAATCCTGTCCACCCCGTGGATGGGTAGTCGGCAAAACGTCGGACTGTC ATAAGAATTGAAACGCTGGCACACTGTTGGGTCCTGAGGCAACACGTTGTGTTGTCACCCTGCTTGGTGGTGGG GTGTGGACTTTGACTTCTGAATAGTGGTTGCGAGCATCTAAACATAGCCTCGCTCGTTTTCGAGTGGGGCTGGT TTTTGCAATTTTATTAGCTAAGTTCTTAAGGGCGCATGGTGAATGCCTTGGCACTAGAAGCCGAAGAAGGA
2017-02-09 18:04:46.158 ERROR 21470 --- [nio-8080-exec-6]    e.u.i.b.utils.SequenceValidation         : Exception for the sequence   GGGAGCTGTCGAAGGTGGGATCGGCGATTGGGACGAAGTCGTAACAAGGTAGCCGTACCGGAAGGTGCGGCTGGATCACCTCCTTTCTAAGGAGCACCATTTCCCAGTCGAATGAACTAGGGAACATAAAGTAGGCATCTGTAGTGGA TATCTACTTGGTGAATATGTTTTGTAAATCCTGTCCACCCCGTGGATGGGTAGTCGGCAAAACGTCGGACTGTCATAAGAATTGAAACGCTGGCACACTGTTGGGTCCTGAGGCAACACGTTGTGTTGTCACCCTGCTTGGTGGTGGGGTGTGGACTTTGACTTCTGAATAGTGGTTGCGAGCATCTAAACATAGCCTCGCTCGTTTTCGAGTGGGGCTGGTTTTTGCAATTTTATTAGCTAAGTTCTTAAGGGCGCATGGTGAATGCCTTGGCACTAGAAGCCGAAGAAGGA
2017-02-09 18:04:46.417 ERROR 21470 --- [nio-8080-exec-6]    e.u.i.b.utils.SequenceValidation         : Excpetion occured whle    inserting into DB

org.springframework.transaction.TransactionSystemException: Could not    commit JPA transaction; nested exception is    javax.persistence.RollbackException: Transaction marked as rollbackOnly
at    org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionM    anager.java:526) ~[spring-orm-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at     org.springframework.transaction.support.AbstractPlatformTransactionManager    .processCommit(AbstractPlatformTransactionManager.java:761) ~[spring-    tx-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at        org.springframework.transaction.support.AbstractPlatformTransactionManager     .commit(AbstractPlatformTransactionManager.java:730) ~[spring-tx-   4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at    

在显示上述例外时有一些空格。请忽略。 感谢您的帮助。

0 个答案:

没有答案