title: How to rollback transaction in Spring JDBC?
body: <p>I want to do several inserts in my method, and want to rollback all the inserts if anything goes wrong.
  Here's my jdbc bean definition</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
      &lt;context:property-placeholder location="classpath:jdbc.properties"/&gt;
  
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="${jdbc.driverClassName}"/&gt;
          &lt;property name="url" value="${jdbc.url}"/&gt;
          &lt;property name="username" value="${jdbc.username}"/&gt;
          &lt;property name="password" value="${jdbc.password}"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>And here's my method</p>
  
  <pre><code>@Transactional
  public void save(Map&lt;String, String&gt; properties, Long moduleId,
                   String language, String propertyFileName, String filePath) {
  
      KeyHolder keyHolder = new GeneratedKeyHolder();
  
      final String propertyFileQuery = "INSERT INTO property_file " +
              "(moduleId, languageId, propertyFileName,hash,path) " +
              "VALUES (?,?,?,?,?)";
  
      jdbcTemplate.update(connection -&gt; {
          PreparedStatement ps =
                  connection.prepareStatement(propertyFileQuery, new String[]{"id"});
          ps.setLong(1, moduleId);
          ps.setString(2, language);
          ps.setString(3, propertyFileName);
          ps.setString(4, "hash goes here");
          ps.setString(5, filePath);
  
          return ps;
      }, keyHolder);
  
      int x = 0 / 0;
  
      final String propertiesQuery = "INSERT INTO property_value " +
              "(propertyFileId, propKey, propValue) " +
              "VALUES (?,?,?)";
  
      properties.forEach((key, value) -&gt; jdbcTemplate.update(
              propertiesQuery, keyHolder.getKey(), key, value
      ));
  
  }
  </code></pre>
  
  <p>I want it to rollback the first insert, after it meets the runtime exception <code>0/0</code></p>
  
  <p>But the data goes into the database, and doesn't get rolled back.</p>
  
  <p>What am i doing wrong?</p>