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 69911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:38:53+00:00 2026-05-10T19:38:53+00:00

I need to execute a select and then update some of the rows in

  • 0

I need to execute a select and then update some of the rows in the ResultSet in an atomic way.

The code I am using looks like (simplified):

stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = stmt.executeQuery('SELECT ...');  while (rs.next()) {     if (conditions_to_update) {         rs.updateString(...);         rs.updateRow();     } } 
  • Can I guarantee that the updates are going to be executed atomically ? If not, how could I assure that ?
  • What happens if any other process has changed the database row that you are updating via updateRow() ? Is there any way to lock the rows in the ResultSet ?
  • 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. 2026-05-10T19:38:54+00:00Added an answer on May 10, 2026 at 7:38 pm

    There’s probably a whole heap of technologies and concepts that come into play here, and things start to get fairly sticky when you start considering multi-threaded / multi request applications.

    As Iassevk stated, you should look into using Transactions to ensure the atomic nature of your updates – a very low-level example would be to do something along the lines of:

    ... con.setAutoCommit(false); try {   while (rs.next()) {     if (conditions_to_update) {       rs.updateString(...);       rs.updateRow();     }   }   con.setAutoCommit(true); } catch (Exception ex) {   //log the exception and rollback   con.rollback(); } finally {   con.close(); } 

    All the updates would then be batched into the same transaction. If any of the updates generated an Exception (such as an invalid value or the connection failing part way through the results), the whole lot would be rolled back. (Finally added because I am a champion of it ;p )

    This however, won’t address your second issue which is two competing methods trying to update the same table – a race condition. There are, in my mind, two main approaches here – each has it’s merits and drawbacks.

    The easiest approach would be to Lock the table – this would require minimal code changes but has a pretty big drawback. Working on the assumption that, as with most applications, it’s more read that write: locking the table will prevent all other users from viewing the data, with the likelihood the code will hang, waiting for the lock to release before the connection time-out kicks in and throws an exception.

    The more complex approach is to ensure that the methods for performing these updates are implemented in a thread-safe manner. To that end:

    • All the updates for this table pass through a single Class
    • That class implements a Singleton pattern, or exposes the update methods as Static methods
    • The update methods utilise the Synchronized keyword to prevent race conditions
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You need to install excel on the machine that is… May 11, 2026 at 10:02 am
  • added an answer Have you tried this : EXEC sp_depends @objname = [table… May 11, 2026 at 10:02 am
  • added an answer Try this: var idArray = new int[] { 1, 2,… May 11, 2026 at 10:02 am

Related Questions

I need to execute a select and then update some of the rows in
I need to execute a large set of SQL statements (creating a bunch of
I need to execute a directory copy upon a user action, but the directories
I need to execute a http web request from Plesk's Task Scheduler (using shared
I need to execute a PowerShell script from within C#. The script needs commandline
I am working on a bash script where I need to conditionally execute some
I need a tool to execute XSLTs against very large XML files. To be
I need a timer to execute callbacks with relatively low resolution. What's the best
I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this
I have any number of anchor links on a page that need to execute

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.