Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You shouldn't call Rollback programmatically. The best way, as recommended by the <a href="http://static.springsource.org/spring/docs/3.0.x/reference/transaction.html#transaction-declarative-rolling-back" rel="noreferrer">docs</a>, is to use declarative approach. To do so, you need to annotate which exceptions will trigger a Rollback.</p> <p>In your case, something like this</p> <pre class="lang-java prettyprint-override"><code>@Transactional(rollbackFor={MyException.class, AnotherException.class}) public SomeResult doSomething(){ ... } </code></pre> <p>Take a look at the <a href="http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/transaction/annotation/Transactional.html" rel="noreferrer">@Transaction API</a> and the docs about <a href="http://static.springsource.org/spring/docs/3.0.x/reference/transaction.html#transaction-declarative-rolling-back" rel="noreferrer">rolling back a transaction</a>.</p> <p>If, despite the docs recommendation, you want to make a programmatic rollback, then you need to call it from <code>TransactionAspectSupport</code> as already suggested. This is from the docs:</p> <pre class="lang-java prettyprint-override"><code>public void resolvePosition() { try { // some business logic... } catch (NoProductInStockException ex) { // trigger rollback programmatically TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } } </code></pre> <p>There may be a architecture mistake though. If your method fails and you need to throw an exception, you shouldn't expect it to return anything. Maybe you're giving too much responsibilities to this method and should create a separated one that only model data, and throws an exception if something goes wrong, rolling back the transaction. Anyway, read the docs.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload