Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring JDBC's transactions handling doesn't work with Google Guice
    primarykey
    data
    text
    <p>I use Google Guice and <a href="http://www.jooq.org" rel="nofollow">jOOQ</a> in my project. Currently I decided to introduce transaction handling using Spring JDBC.</p> <p>So I did the following.</p> <p>I set a data source and a transaction manager in Guice module.</p> <pre><code>@Provides @Singleton DataSource provideDataSource(IExternalSettings settings) { Jdbc3PoolingDataSource dataSource = new Jdbc3PoolingDataSource(); // configuring DataSource return dataSource; } @Provides @Singleton DataSourceTransactionManager provideDataSourceTransactionManager(DataSource dataSource) { return new DataSourceTransactionManager(new TransactionAwareDataSourceProxy(dataSource)); } </code></pre> <p>Then I inject my transaction manager to a persistence facade</p> <pre><code>@Inject public PersistenceFacade(final DataSourceTransactionManager transactionManager) { this.dataSource = transactionManager.getDataSource(); this.transactionManager = transactionManager; } </code></pre> <p>Later, I use this data source to create jOOQ factory: <code>new Factory(dataSource, ...)</code>.</p> <p>Finaly I run my database access code:</p> <pre><code>DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition(); TransactionStatus transaction = transactionManager.getTransaction(transactionDefinition); try { // db code in transaction transactionManager.commit(transaction); return result; } catch (Exception e) { transactionManager.rollback(transaction); throw e; } </code></pre> <p>So far, so good. It <strong>works as expected</strong>.</p> <p>So, my next step is to introduce <code>@Transactional</code> annotation using Guice AOP. I created an interceptor</p> <pre><code>class TransactionalMethodInterceptor implements MethodInterceptor { @Inject private DataSourceTransactionManager transactionManager; @Override public Object invoke(final MethodInvocation invocation) throws Throwable { DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition(); TransactionStatus transaction = transactionManager.getTransaction(transactionDefinition); try { Object result = invocation.proceed(); transactionManager.commit(transaction); return result; } catch (Exception e) { transactionManager.rollback(transaction); throw e; } } } </code></pre> <p>And configured it in <code>configure()</code> method of Guice module:</p> <pre><code>TransactionalMethodInterceptor transactionalMethodInterceptor = new TransactionalMethodInterceptor(); requestInjection(transactionalMethodInterceptor); bindInterceptor(Matchers.any(), Matchers.annotatedWith(Transactional.class), transactionalMethodInterceptor); </code></pre> <p>And now <strong>the issues begin</strong>. I can see, using debugger, that control flow reaches interceptor. In particular, it reaches <code>transactionManager.rollback(...)</code> invocation. But the transaction is <strong>not actually roll backed</strong>.</p> <p>I have no clue what's the reason. Any ideas? I'll be grateful. Thanks!</p>
    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.
 

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