title: Could not commit JPA transaction: Transaction marked as rollbackOnly
body: <p>I'm using Spring and Hibernate in one of the applications that I'm working on and I've got a problem with handling of transactions.</p>
  
  <p>I've got a service class that loads some entities from the database, modifies some of their values and then (when everything is valid) commits these changes to the database. If the new values are invalid (which I can only check after setting them) I do not want to persist the changes. To prevent Spring/Hibernate from saving the changes I throw an exception in the method. This however results in the following error: </p>
  
  <pre><code>Could not commit JPA transaction: Transaction marked as rollbackOnly
  </code></pre>
  
  <p>And this is the service:</p>
  
  <pre><code>@Service
  class MyService {
  
    @Transactional(rollbackFor = MyCustomException.class)
    public void doSth() throws MyCustomException {
      //load entities from database
      //modify some of their values
      //check if they are valid
      if(invalid) { //if they arent valid, throw an exception
        throw new MyCustomException();
      }
  
    }
  }
  </code></pre>
  
  <p>And this is how I invoke it:</p>
  
  <pre><code>class ServiceUser {
    @Autowired
    private MyService myService;
  
    public void method() {
      try {
        myService.doSth();
      } catch (MyCustomException e) {
        // ...
      }        
    }
  }
  </code></pre>
  
  <p>What I'd expect to happen: No changes to the database and no exception visible to the user.</p>
  
  <p>What happens: No changes to the database but the app crashes with:</p>
  
  <pre><code>org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction;
  nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
  </code></pre>
  
  <p>It's correctly setting the transaction to rollbackOnly but why is the rollback crashing with an exception?</p>