Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StaleObjectStateException: can catch exception but stack trace still prints

 
Ryann Ong
Greenhorn
Posts: 19
Fedora Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day fellows,

I just want to ask how to disable the exception shown by StaleObjectStateException. Since i'm using optimistic locking in hibernate, i've tried debugging and catching the exception, thou it catches the exception but still it shows the stack trace on the console. Here's my code for the following method:

private void modifyEmployee() {
String status = null;
this.searchEmployee();
status = this.readClient();
if(status != null) {
Employ = this.extractJSON(status);
session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
try {
tx = session.beginTransaction();
session.update(Employ);
tx.commit();
this.writeClient("Success");
session.flush();
}
catch(StaleObjectStateException a) { // i've tried debugging it, it goes here and executes the statements inside the catch block however it still prints the stacktrace on console
this.writeClient("Modifying failed: Selected employee was updated or deleted by another transaction");
tx.rollback();
}
} catch (Exception e) {
tx.rollback();
this.writeClient("Modifying Product Failed");
}
session.close();
}
else
System.out.println("Success");
}

I'm just new in using hibernate, hope somebody could help....

Thank you
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Locking doesn't have anything to do with Exception handling, so it doesn't matter how you are locking optimistic or pessimistic

I assume the stack trace is being printed because it is printing inside the Hibernate code, meaning it catches it first, does a print stack trace, then throws it up to you.

Mark
 
Ryann Ong
Greenhorn
Posts: 19
Fedora Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark, I will try to examine the code inside hibernate, try to figure it out there.. are there other ways to explicitly prevent the exception from showing.. since im doing a rollback, it is guaranteed that my data is intact right?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic