Question

I am using Spring Framework to make database calls. I have an update that involves calling 2 stored procedures, each is doing multiple operations.

I have transaction management defined in each SP separately as follow:

 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
    ROLLBACK;
 END;

START TRANSACTION;

     .......logic .........
    COMMIT;

This works fine if I call each SP by itself, but I want to make the whole operation in one transaction, so if the call to the first SP works, but the call to the second SP fails, it rolls back both.

So obviously I need to remove the commit from the sps and handle it on the Spring side.

Currently For Spring I am using SimpleJDBCCall (code extract below)

public List executeSP(String spName, Map<String,Object> params, RowMapper mapper) throws Exception {

try {
    SimpleJdbcCall template = new SimpleJdbcCall(getDataSource()).withProcedureName(spName);
MapSqlParameterSource in = new MapSqlParameterSource();
for (String key : params.keySet()) {
        in.addValue(key, params.get(key));
}

template.returningResultSet("items", mapper);
Map<String, Object> out = template.execute(in);
return (List) out.get("items");
}
catch (Exception e) { 
logger.error("DataManager executeSP with Mapper (" + spName + ") with params = " + params +  e.getMessage() );
            throw e;
} catch (......... )

My questions is how can I call the second Stored procedure and rollback both of them if the second one fails ?

Thank you

Was it helpful?

Solution

@Transactional(rollbackFor=Exception.class)
public void performBothSProcsTransactionally(){
   //executeSP1

   //executeSP2
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top