Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unable to handle exception coming from EntityManager.persist method

 
Ranch Hand
Posts: 247
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sir,
i want to generate log file when exception occurs by using FileWriter.
this is my code

While saving a duplicate entry it throws EntityExistsException and i am unable to write log file in catch .I would be grateful if anybody tell me solution.
 
Marshal
Posts: 79532
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you logging a successful persistence, rather than an error, then throwing the same Exception?
 
Campbell Ritchie
Marshal
Posts: 79532
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you suffer an Exception in the try block, it will be in your line 7. Since your logging would be in line 8, that line will be omitted. Why aren't you logging errors from the catch block?

And avoid logging an error then throwing the same Exception.
 
Raza Mohd
Ranch Hand
Posts: 247
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes.! surely the exception is coming when persist is called.
because i am trying to persist the same object that is already present in the table and exceptions also says that duplicate entry for the key.
but why isn't it coming in catch block from where i need to write a log.Log is not appearing when i remove the comments from catch block.Why i am unable to catch it.I am literally confused.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Raza,

There are only 2 things that could be happening here:
1) The exception being thrown from the try catch block is not EntityExistsException, but one of the others IllegalStateException or IllegalArgumentException or TransactionRequiredException. Try using and see if you are able to generate the log .

2) Make sure the logger is initialised properly and test writing successfully to the log file first, even before you try generating the log from this piece of code.

I'll be happy to know how it went.

Regards,
Minakshi
 
Raza Mohd
Ranch Hand
Posts: 247
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi minakshi.

Exception report reveals caused by EntityExistsException.

and i know explicitly because i am trying to persist the object that is alrealy exists.

that is why a bit confused..!

regards
raza!
 
Raza Mohd
Ranch Hand
Posts: 247
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Caused by: java.sql.BatchUpdateException: Duplicate entry '8363507-4678' for key 1

Caused by: javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update



That is the piece of exception.
 
Monika Joshi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the logger initialised properly? Are you able to see the other logs correctly? Is the log level set correctly? You will not be see INFO level messages if the log level is any less then DEBUG or INFO. I feel quite sure that the exception is caught in the catch block and is then rethrown by use of "throw ex;". So sugest that lets concentrate for some time on the logger coding and its working.

regards,
Min
 
Arthur, where are your pants? Check under this tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic