Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Joe Ess
  • salvin francis
  • fred rosenberger

SpringMVC and Transactions

Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

<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;
//Commit transaction

return new ModelAndView(getSuccessView());
Posts: 17344
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

bacon. tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!