File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to commit a code when there is a exception

 
deepak carter
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Suppose there is a piece of code

class
{
line 1;

line 2

line 3

}


Now Suppose there is exceptiopn in line 3.I can catch the exception but how to commit which ever is already excuted i mean suppose there is a task which i am creating in the line 2 i want that to commit even though there is a exception in line 3....

how to do it.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean "commit" as in the DB sense of the word? That could be achieved by keeping several savepoints within an overall transaction. Or you could just use several transactions, maybe one for each step.
 
deepak carter
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry not in the DB sense in the in the java whatever i have wriiten in line2 gets executed even though there is an exception in line 3...can it be achieved
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't follow - by the time line 3 is executed, line 2 has already been executed. You just need to ensure that whatever needs to be persisted that happens in line 2 does get persisted before moving on.
 
deepak carter
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No the exception is getting thrown the transaction is getting rolled back so whatever is written in line 2 is getting rolledback....i dont how but its doing that...i have debugged the application and seen and for the same scenario i have written the JUNIt and its getting failed because of this
 
deepak carter
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i am registering a person it makes multiple enteries to different table approx 90 tables....however the frameework is designed in such a way that if there is an exception then all the transactions should rolled back nothing should go in the table...but i require that one piece of code should get executed no matter what the exception is thrown or not
 
Ishan Pandya
Ranch Hand
Posts: 225
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should try what ulf said.
keep autocommit as false and use a save point before line three.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused (and, I suspect, so are you :-) I asked specifically if this was about DB transaction, which you said it was not. So please clarify what this "transaction" is that gets "rolled back" and which deals with "tables" - that sounds very much like a DB.
 
deepak carter
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I think so...(apologies) The lines which are getting executed have to make entries with the database.
 
Shannon Graham
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about git or subversion source control? Cause if you are, the answer is:
If you have an exception, don't commit the code. End of story. Don't make other people deal with your mistakes, fix them before you push the code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic