Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4082996
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:15:25+00:00 2026-05-20T18:15:25+00:00

Consider the following two blocks of Java psedo-code in a system that uses optimistic

  • 0

Consider the following two blocks of Java psedo-code in a system that uses optimistic transactions.

Example A:

try {
    txn.begin();
    // database operations
    txn.commit();
}
catch (Exception e) {
    txn.rollback();
}

Example B

txn.begin();
// database operations
try {
    txn.commit();
}
catch (Exception e) {
    txn.rollback();
}

I’m seeing transactions being conducted both ways in our code; I’m sure that A is correct. my intuition tells me that B is wrong, but it seems that there is no harm in B since the commit() is in the try block, and can be caught and rolled-back in the case of an error. Please explain whether B is correct, and why. Thanks!

Edit: So I’m not really getting the answer I’m looking for. I already know that B is somehow “bad”, what I’m looking for is why it is bad; that is, is there some possible situation where A would work where B would fail?

-tjw

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:15:26+00:00Added an answer on May 20, 2026 at 6:15 pm

    I would do a slight mix (Example C):

    txn.begin(); 
    try {
        // database operations
        txn.commit();
    }
    catch (Exception e) {
        txn.rollback();
    }
    

    Keep your database commands in the try block, but leave the ‘begin’ transaction out. If you error on ‘begin’ you won’t try to rollback a transaction that was never started in the catch block.

    Edit

    The reason why example B is bad is because the only way you will ever rollback your transaction is if the commit fails. However, the reason A is also bad is because you have the small potential to fail when beginning a transaction, in which case you will attempt to rollback something that doesn’t exist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following two Java files that contain a simplified version of my issue
Consider the following two methods, written in pseudo code, that fetches a complex data
Consider the following C# code structure (S0-S3 are placeholders for arbitrary code blocks): try
Consider the following two toolbars that are in the same project of mine: Notice
Consider the following .Net ASMX web service with two web methods. using System; using
consider the following two pieces of code: public static Time Parse(string value) { string
Consider the following two lines of code if (test ! = null) and if
Consider the following two Java classes: a.) class Test { void foo(Object foobar) {
Consider the following two code samples: NSData *imgData = UIImagePNGRepresentation(imgFull); NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
Consider the following two fragments of code: scala> def f1(x:Any) = x match {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.