Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In spring, we have @Transactional annotation which can be specified at Controller, so everything happens inside a controller method is treated as one transaction. However in dropwizard, we can have transaction at a DAO level by implementing Transactional<DAOclass>. But if I use two DAOs in a single resource method, it is treated 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 everthing under a single transaction So is there any way of specifying transactional boundaries at Resource level?

like image 853
Preethi Avatar asked May 24 '14 15:05

Preethi


1 Answers

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

@CreateSqlObject seems to be the answer.

like image 133
radu-c Avatar answered Oct 18 '22 03:10

radu-c