File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Spring Rollback exception problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Application Frameworks » Spring
Reply Bookmark "Spring Rollback exception problem" Watch "Spring Rollback exception problem" New topic
Author

Spring Rollback exception problem

Mohammed Amine Tazi
Greenhorn

Joined: Feb 09, 2009
Posts: 22
Hi.

I have a User Table that contains Login as primary key. I have LoginService calling LoginDAO and LoginService is transactional.

I have a method LoginService saveUser(User user) calling LoginDAO.save(User user). The problem I get is that when I add a user with an existing login, the LoginService.saveUser method doesn't catch the exception (DataIntegrityViolationException), and a rollback exception is thrown after the method finishes.

I make a solution of creating a Manager class that's transactional and keep the service not transactional to solve the problem. But I wonder how can I solve the problem as it is?

Thanks in advance.

Amine.


SCJP 5 / SCWCD 5 / SCBCD 5 / SCDJWS 4 / SCDJWS 5 / SCEA 5
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16624

I don't understand where the problem is. You try to save a new User, but it breaks data integrity because the PK already exists. So it should throw an exception and rollback.

First, why have a PK that is manually entered and has some meaning to it. Usually a PK is unique and has no meaning like an autoincrementing field that just has some int in it.

What DB tech are your using in your DAO, is it JDBC, ORM??? If it is ORM like Hibernate you can call session.saveOrUpdate and Hibernate will know to try to do an update statement instead of insert and you won't get an exception. Of if JDBC, write logic to check if the login already exists, if it does then don't run the insert. If it doesn't then run the insert.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mohammed Amine Tazi
Greenhorn

Joined: Feb 09, 2009
Posts: 22
Thanks for your reply.

I'm using JPA for persistence and MySQL as an RDBMS.

What I want to say is that when an exception of DataIntegrityViolationException is needed to be thrown, it's not thrown and code goes well even if it's error.

I think spring transaction when used when MySQL postpone errors to the end of the transaction.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16624

Isn't DataIntegrityViolationException one of Spring's DataAccessException heirarchy. And don't you need to translate JPAExceptions? Using the PerisistenceExceptionSomethingOrOther as a bean so Spring will do the conversion/translation?

Mark
 
 
subject: Spring Rollback exception problem
 
Threads others viewed
Frameworks? Do I need one? want one?
A Simple Test Framework using JUnit and Spring (Part 1)
Spring form input binding problem
how do i solve this error caused due to problem in spring initiation on tomcat
how to inject DAO with Spring???
developer file tools