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

  • Home
  • SEARCH
  • 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 9156771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:51:01+00:00 2026-06-17T12:51:01+00:00

I want some advice on some concurrency issues regarding jdbc, i basically need to

  • 0

I want some advice on some concurrency issues regarding jdbc, i basically need to update a value and then retrieve that value using a update then a select, I’m assuming by turning auto commit off no other transaction can access this table, hence other transactions won’t be able to perform update and select queries until this has been committed.

Below is some example code. Do you think this will work and does any one else have a better solution to implementing this?

int newVal=-1;
con.setAutoCommit(false);
PreparedStatement statement = con.prepareStatement("UPDATE atable SET val=val+1 WHERE id=?");
statement.setInt(1, id);
int result = statement.executeUpdate();
if (result != 1) {
    throw new SQLException("Nothing updated");
} else {
    statement = con.prepareStatement("SELECT val FROM atable WHERE id=?");
    statement.setInt(1, id);
    ResultSet resultSet = statement.executeQuery();
    if (resultSet.next()) {
        newVal =  resultSet.getInt("val");
    }
}
statement.close();
con.commit();
con.setAutoCommit(true);

Thanks.

  • 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-06-17T12:51:02+00:00Added an answer on June 17, 2026 at 12:51 pm

    Assuming you use some form of data source, you may configure there if you want transactionality and the isolation level. But to be explicit:

    try(Connection con = ds.getConnection()){
       con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
       con.setAutoCommit(false);
       //...
    } catch(SQLException sqle) {
       throw new MyModelException(e)
    }
    

    Now, you could trigger pesimistic locking by updating a version (or timestamp) field in your table. This will trigger a lock in the database (most likely at the record level):

    try(PreparedStatement pStm = con.prepareStatement("update atable set version=version+1")){
       pStm.executeUpdate();
    }
    

    At this point, if another user is trying to update the same record simultaneously, this connection will either wait or timeout, so you must be ready for both things. The record will not be unlocked until your transaction ends (commit or rollback).

    Then, you can safely select and update whatever you want and be sure that nobody else is touching your record as you process your data. If anybody else tries they will be put on wait until you finish (or they will timeout depending on connection configuration).

    Alternatively you could use optimistic locking. In this case you read your record, do modifications to it, but in the update you make sure nobody else has changed it since you read it by checking that the version/timestamp field is the same as the one you orginally read. In this case you must be prepared to retry a transaction (or abort it alltogether) if you realize you have stale/outdated data.

    i.e. update atable set afield=? where id=? and version=1

    If the number of rows affected is 0, then you know that is probable that the record was updated between your read and your update and the record is no longer in version 1.

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

Sidebar

Related Questions

I need some advice regarding which database I should use. I want to create
I need some advice. I have a web page and want to extend it's
I want some piece of advice regarding a LLVM pass. My particular problem is:
please I need some advice about design approaches in iPhone programming. I want to
i'm really confused about something and need some advice. i want to be able
I need some advice here. I need to make a webservice, that can make
I need some advice on how I can proceed with this issue. Using PHP
I want some advice on structuring some (basic) php code. I need to display
I need some advice on a simple bash script. I want to start around
I need some advice. Suppose I have the following string: Read Variable I want

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.