aspose file tools*
The moose likes Other Application Frameworks and the fly likes SpringMVC and  Transactions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "SpringMVC and  Transactions " Watch "SpringMVC and  Transactions " New topic
Author

SpringMVC and Transactions

Kashif Mughal
Ranch Hand

Joined: Jun 19, 2008
Posts: 44
Hi

I am working on spring MVC application and i want to delete the record from DB. I am starting the transaction in referenceData() and closing in onSubmit() mehtods. Code is below, Please advice is that right way to define trnsaction boundries in Controller. Should we handle transactions in Controllers for MVC applications.

<blockquote>code:
<pre name="code" class="core"> protected Map referenceData(HttpServletRequest request) throws Exception {
// start a transaction
tx = (ResourceTransactionManager) ctx.getBean("transactionManager");
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
TransactionStatus status = tx.getTransaction(def);
HashMap model = new HashMap();
List list = userServiceImpl.findAllUsers();
model.put("users", list);
return model;
}

/** returns ModelAndView(getSuccessView()) */
public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,
BindException errors) throws Exception {

UserImpl userImpl = (UserImpl) command;
userServiceImpl.removeUser(userImpl);
//Commit transaction
tx.commit(status);

return new ModelAndView(getSuccessView());
}
</pre>
</blockquote>


Thanks in Advance,
Kash.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Personally, and this is just my opinion. I thought declaring transactions at your service/use case level is the place to do it, and I personally would use @Transactional, but that is my preference. Rather than manually code transaction support, which couples it with that code. Which we want to avoid tight coupling, otherwise we wouldn't be using Spring.

If you don't like @Transactional, then what about declaring Transactions through Spring AOP. Then it is 100% decoupled from your code.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SpringMVC and Transactions
 
Similar Threads
Classcast Exception using Spring Transaction
Page appears only on refresh
java. lang. IllegalStateException: Neither Binding Result nor plain target object
Classcast Exception using Spring Transaction
Spring MVC - Search query