6

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?

4

1 回答 1

3

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

@CreateSqlObject seems to be the answer.

于 2014-05-25T20:37:21.600 回答