Roll back A if B goes wrong. spring boot, jdbctemplate
<p>I have a method, 'databaseChanges', which call 2 operations: A, B in iterative way. 'A' first, 'B' last.
'A' &amp; 'B' can be <strong>C</strong>reate, <strong>U</strong>pdate <strong>D</strong>elete functionalities in my  persistent storage, Oracle Database 11g.</p>

<p>Let's say, </p>

<p>'A' update a record in table Users, attribute zip, where id = 1. </p>

<p>'B' insert a record in table hobbies. </p>

<p><strong>Scenario:</strong> databaseChanges method is been called, 'A' operates and update the record. 'B' operates and try to insert a record, something happen, an exception is been thrown, the exception is bubbling to the databaseChanges method.</p>

<p><strong>Expected:</strong> 'A' and 'B' didn't change nothing. the update which 'A' did, will be rollback. 'B' didn't changed nothing, well... there was an exception.</p>

<p><strong>Actual:</strong> 'A' update seems to not been rolled back. 'B' didn't changed nothing, well... there was an exception.</p>

<hr>

<p><strong>Some Code</strong></p>

<p>If i had the connection, i would do something like:</p>

<pre><code>private void databaseChanges(Connection conn) {
   try {
          conn.setAutoCommit(false);
          A(); //update.
          B(); //insert
          conn.commit();
   } catch (Exception e) { 
        try {
              conn.rollback();
        } catch (Exception ei) {
                    //logs...
        }
   } finally {
          conn.setAutoCommit(true);
   }
}
</code></pre>

<p><strong>The problem:</strong> I don't have the connection (see the Tags that post with the question)</p>

<p>I tried to:</p>

<pre><code>@Service
public class SomeService implements ISomeService {
    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate npjt;

    @Transactional
    private void databaseChanges() throws Exception {   
        A(); //update.
        B(); //insert
    }
}
</code></pre>

<hr>

<p>My AppConfig class:</p>

<pre><code>import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

@Configuration
public class AppConfig {    
    @Autowired
    private DataSource dataSource;

    @Bean
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate() {
        return new NamedParameterJdbcTemplate(dataSource);
    }   
}
</code></pre>

<hr>

<p>'A' makes the update. from 'B' an exception is been thrown. The update which been made by 'A' is not been rolled back.</p>

<p>From what i read, i understand that i'm not using the @Transactional correctly.
I read and tried several blogs posts and stackverflow Q &amp; A without succeess to solve my problem.</p>

<p>Any suggestions? </p>

<hr>

<p><strong>EDIT</strong></p>

<p>There is a method that call databaseChanges() method</p>

<pre><code>public void changes() throws Exception {
    someLogicBefore();
    databaseChanges();
    someLogicAfter();
}
</code></pre>

<p>Which method should be annotated with @Transactional,</p>

<p>changes()? databaseChanges()?</p>