Khaled
Khaled

Reputation: 19

@Transactionnal annotation in spring boot does it really create a transaction?

I want to call different methods that interact with my database in one method.

something like this :

@Autowired
EnteteService es; // service for jpaRepository 

@Autowired
SystemOracleServiceJpa so; // service using  jdbcTemplate

@Autowired
DetailService ds; // service for jpaRepository 


@Transactional
public void configure(EnteteAR entete) throws ConfigurationException {
    try{
        
        this.es.save(entete); // first methode 
        List<DetailAR> details = this.so.getStructure(entete.getTableName());
        if(details.size()>0){
                this.ds.saveAllDetails(details); // second  
                this.so.CreateTable(details, entete.getTableName(), "DEM");//third 
                this.so.createPkIdxDem(entete.getTableName()); // fourth 
                this.so.CreateTable(details, entete.getTableName(), "BACK"); // fifth 
        }
        else{
            throw new ConfigurationException("Configuration error");

        }
        
        
        
    }catch(Exception e){
        throw new ConfigurationException(e.getMessage());

        
    }
    
    
}

I want to commit only if no errors appears in all this methods inside my main method "configure".

I was thinking that @transactionnal annotation work for this, but that commit after each method inside.

Exemple :

if this.es.save work and this.ds.saveAllDetails dont, I find data of es.save on database :(

Someone can help my please ?

thank with advance for your reading and your potential help.

Upvotes: 1

Views: 187

Answers (1)

Panagiotis Bougioukos
Panagiotis Bougioukos

Reputation: 19193

@Transactional will automatically invoke a rollback if an unchecked exception is thrown from the executed method.

ConfigurationException in your case is a checked exception and hence it does not work.

You can make it work by modifying your annotation to

      @Transactional(rollbackOn = ConfigurationException.class)
      public void configure(EnteteAR entete) throws ConfigurationException {
         try{ ....

Upvotes: 2

Related Questions