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?