• Post Reply Bookmark Topic Watch Topic
  • New Topic

Spring Rollback exception problem  RSS feed

 
Mohammed Amine Tazi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mohammed Amine Tazi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!