title: Multiple calls to JdbcTemplate.update not wrapped in a single DB transaction
body: <p>I have looked at several examples and don't understand what I am doing differently; however, this is not working for me. Here is a simplified description of my code, with code snippets, to illustrate what is not working.</p>
  <p>I have a main transaction table that hold one record for each application transaction. I have a linked table that holds images from the transactions with a foreign key pointing back to the master transaction record. Nothing too strange or fancy here. (Full disclosure: There are two other linked tables, but I am certain that the fix for one table will work for all of them.)</p>
  <p>My code creates a RESTful endpoint like this:</p>
  <pre><code>@Controller
  @RequestMapping(&quot;/feed/v1&quot;)
  public class ReportingDataFeedControllerV1 {
      
      @Resource(name = &quot;dataFeedService&quot;)
      private DataFeedService dataFeedService;
      
      @PostMapping(value = &quot;/myend&quot;, consumes = &quot;application/json&quot;, produces = &quot;application/json&quot;)
      public @ResponseBody DataFeedResponse DataFeed(@RequestBody DataFeedRequest request) {
          
          DataFeedResponse response = new DataFeedResponse();
  
          try {
          dataFeedService.saveData(request);
  </code></pre>
  <p>DataFeedService is an interface that defines a saveData method and gets implemented by DataFeedServiceImpl:</p>
  <pre><code>public class DataFeedServiceImpl implements DataFeedService {
      
      @Resource(name = &quot;dataFeedRepository&quot;)
      private DataFeedRepository dataFeedRepository;
  
      @Override
      //@Transactional        // Outer @Transactional commented
      /**
       * Call the save method on the repository implementation class.
       * 
       * @param request - DataFeedRequest object that contains the data to be saved
       * @throws Exception
       * @author SmithDE
       */
      public void saveData(DataFeedRequest request) throws Exception {
          dataFeedRepository.saveData(request);
      }
  </code></pre>
  <p>DataFeedRepository is another interface that gets implemented by DataFeedRepositoryImpl:</p>
  <pre><code>public class DataFeedRepositoryImpl implements DataFeedRepository {
      private static final int REF_SS_MFA = 4;
  
      @Resource(name=&quot;rdpJdbcTemplate&quot;)
      private JdbcTemplate jdbcTemplate;
      
  </code></pre>
  <p>So, there are at least two levels of classes between the method that starts this action and the real call to saveData().</p>
  <p>My method, in class DataFeedRepositoryImpl, that saves transaction data to the database tables is annotated with @Transactional, like this: (It also includes @Override because it is the implementation of a interface from a base class.)</p>
  <pre><code>@Override
  @Transactional           // Nested @Transactional
  /**
   * Method that performs the save of the data to the database tables.
   * 
   * @param request - Request object containing the data to be saved
   * @exception Exception
   * @author SmithDE
   */
  public void saveData(DataFeedRequest request) throws Exception {
  </code></pre>
  <p>The method builds an INSERT statement for the main transaction table. It needs to retrieve the new primary key, so it calls the update method of JdbcTemplate like this:</p>
  <pre><code>        KeyHolder keyHolder = new GeneratedKeyHolder();
          int newRowCount = jdbcTemplate.update(connection -&gt; {
              PreparedStatement ps = connection
                      .prepareStatement(thisquery, Statement.RETURN_GENERATED_KEYS);
                      return ps;
                    }, keyHolder);
          id = (long) keyHolder.getKey();
  </code></pre>
  <p>Next it builds a series of INSERT statements for the images associated with this transaction and sends them to the database in separate calls to JdbcTemplate.update(), like this:</p>
  <pre><code>    for (DataFeedImage image : images) {
          ...
              newRowCount = jdbcTemplate.update(thisquery);
      }
  </code></pre>
  <p>My expectation is that an error in any of the calls to insert images into the linked image table will cause the entire database transaction to rollback. However, my observation is different:</p>
  <ul>
  <li>The main transaction data record is still there in the main transaction table.</li>
  <li>Any images that were inserted before the error are still there in the image table.</li>
  </ul>
  <p>I want all of these calls to update() to be part of the same database transaction, but they clearly are not.</p>
  <p>Please help me understand what I am doing wrong.</p>
  <p>Just a thought as I was writing this edit. Could it be a problem that the method in the previous implementation class is annotated as @Transactional and the method that actually makes the JdbcTemplate.update() calls is also annotated as @Transactional? Could this nested declaration of @Transactional cause a problem?</p>