cvdr
cvdr

Reputation: 1119

Spring boot rollback multi datasource

My service do a delete in 2 database, oracle and postgresql. When one of repository throw a exception, the other one dont rollback.

Example:

public MyService { 
@Autowired private OracleRep oracleRep;
@Autowired private PostgreRep postgreRep ;

@Transactional
public void delete(Long id){

    oracleRep.delete(id);
    postgreRep.delete(id);
}
}

public OracleRepImpl { 

@Autowired private NamedParameterJdbcTemplate namedParameterJdbcTemplate ;

public void delete(Long id){
    //do delete
}
}

public PostgreRepImpl { 

@Autowired private @Qualifier("postgresql")NamedParameterJdbcTemplate namedParameterJdbcTemplate ;

public void delete(Long id){
    //do delete
}
}

I guess the annotation @Transactional dont share the transcational between database.

How could i implement this behavior?

Upvotes: 0

Views: 975

Answers (1)

cvdr
cvdr

Reputation: 1119

On the database configuratio i built this:

@Bean(name = "postgresqlTransaction")

 public DataSourceTransactionManager  postgresqlDataSourceTransactionManager(@Qualifier("postgresqlDataSource") DataSource datasource) {
    return new DataSourceTransactionManager(datasource);

}

Then i build a annotation

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional("postgresqlTransaction")
public @interface TransactionalPostgresql {
}

and now:

@TransactionalPostgresql 
@Transactional
public void delete(Long id){

    oracleRep.delete(id);
    postgreRep.delete(id);
}

Upvotes: 3

Related Questions