File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how to commit a code when there is a exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to commit a code when there is a exception" Watch "how to commit a code when there is a exception" New topic
Author

how to commit a code when there is a exception

deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
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
Marshal

Joined: Mar 22, 2005
Posts: 42944
    
  68
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

Joined: Feb 19, 2011
Posts: 165
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
Marshal

Joined: Mar 22, 2005
Posts: 42944
    
  68
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

Joined: Feb 19, 2011
Posts: 165
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

Joined: Feb 19, 2011
Posts: 165
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

Joined: Feb 06, 2012
Posts: 223

I think you should try what ulf said.
keep autocommit as false and use a save point before line three.


OCPJP
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42944
    
  68
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

Joined: Feb 19, 2011
Posts: 165
Yes I think so...(apologies) The lines which are getting executed have to make entries with the database.
Shannon Graham
Greenhorn

Joined: Mar 11, 2013
Posts: 14
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to commit a code when there is a exception