Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>We don't use EJB, but simple Spring and we have chosen AOP approach. We've implemented new annotation <code>@TransactionalWithRollback</code> and used AOP to wrap those annotated methods with "around" advice. To implement the advice we use mentioned <code>TransactionTemplate</code>. This means a little work at the beginning, but as a result we can just annotate a method with <code>@TransactionalWithRollback</code> like we use <code>@Transactional</code> in other cases. The main code looks clean and simple.</p> <pre><code>// // Service class - looks nice // class MyServiceImpl implements MyService { @TransactionalWithRollback public int serviceMethod { // DO "read only" WORK } } </code></pre> <hr> <pre><code>// // Annotation definition // @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface TransactionalWithRollback { } </code></pre> <hr> <pre><code>// // the around advice implementation // public class TransactionalWithRollbackInterceptor { private TransactionTemplate txTemplate; @Autowired private void setTransactionManager(PlatformTransactionManager txMan) { txTemplate = new TransactionTemplate(txMan); } public Object doInTransactionWithRollback(final ProceedingJoinPoint pjp) throws Throwable { return txTemplate.execute(new TransactionCallback&lt;Object&gt;() { @Override public Object doInTransaction(TransactionStatus status) { status.setRollbackOnly(); try { return pjp.proceed(); } catch(RuntimeException e) { throw e; } catch (Throwable e) { throw new RuntimeException(e); } } }); } } </code></pre> <hr> <pre><code>// // snippet from applicationContext.xml: // &lt;bean id="txWithRollbackInterceptor" class="net.gmc.planner.aop.TransactionalWithRollbackInterceptor" /&gt; &lt;aop:config&gt; &lt;aop:aspect id="txWithRollbackAspect" ref="txWithRollbackInterceptor"&gt; &lt;aop:pointcut id="servicesWithTxWithRollbackAnnotation" expression="execution( * org.projectx..*.*(..) ) and @annotation(org.projectx.aop.TransactionalWithRollback)"/&gt; &lt;aop:around method="doInTransactionWithRollback" pointcut-ref="servicesWithTxWithRollbackAnnotation"/&gt; &lt;/aop:aspect&gt; &lt;/aop:config&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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