• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Spring Rollback exception problem

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic