How do we force a resource level (controller) transaction in Dropwizard with jdbi?

In spring, we have the @Transactional annotation, which can be specified in the controller, so everything happens inside the controller method, it is considered as one transaction. However, in the dropwizard, we can have a transaction at the DAO level by implementing the Transactional<DAOclass> . But if I use two DAOs in one resource method, it is considered as two different transactions.

Say I have two DAOs

Dao1.java

  public abstract class Dao1 implements Transactional<Dao1>{ @sqlQuery(//somequery) public abstract void insertIntoArticles(); } 

Dao2.java

  public abstract class Dao2 implements Transactional<Dao2>{ @sqlQuery(//somequery) public abstract void insertIntoArticlesChildren(); } 

ArticleResource.java

  @POST @PATH("/articles") public void insertArticleAndItsChildren(Integer articleId){ try{ dao1.begin(); dao2.begin(); dao1.insertIntoArticles(); dao2.insertIntoArticlesChildren(); dao1.commit(); dao2.commit(); }catch(Exception e){ dao1.rollback(); dao2.rollback(); } } 

In the above example, dao1 and dao2 have two different transactions. But I need ever under one transaction. So, is there a way to specify transaction boundaries at the resource level?

+6
source share
1 answer

Take a look at this post https://groups.google.com/forum/#!topic/jdbi/O5rxzwmlwjM

@CreateSqlObject seems to be the answer.

+3
source

Source: https://habr.com/ru/post/969804/


All Articles