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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:36:54+00:00 2026-06-18T12:36:54+00:00

I need to insert into database which has two columns- ID PrimaryKey String ACCOUNT

  • 0

I need to insert into database which has two columns-

ID      PrimaryKey String
ACCOUNT String

So that means each thread should be using unique id always and I need to store the same ID in Account column also. So suppose if ID is 1 then in Database it should be stored as

ID  Account
1   SomeString+1
2   SomeString+2
3   SomeString+3
....
..

100 SomeString+100

I am concatenating that userID with that String always in Account column.

Below is my multithreaded code which will spawn multiple threads- And each thread will be getting a new unique ID everytime as I am using AtomicInteger for that. And it will insert that ID to ID column and also append that ID to Account column

But somehow in my below program what I have seen in that database is-

ID Account
1  String+2
2  String+1
3  String+3

Which is not right. It should be something like this-

ID Account
1  String+1
2  String+2
3  String+3

Below is the code

 public static void main(String[] args) {

        final int noOfThreads = 4;
        final int noOfTasks = 10;

        final AtomicInteger id = new AtomicInteger(1);

        ExecutorService service = Executors.newFixedThreadPool(noOfThreads);

        for (int i = 0; i < noOfTasks * noOfThreads; i++) {
            service.submit(new Task(id));
        }
    }


class Task implements Runnable {

    private final AtomicInteger id;
    private volatile int userId;

    public Task(AtomicInteger id) {
        this.id = id;
    }


    @Override
    public void run() {

        dbConnection = getDBConnection();

        preparedStatement = dbConnection.prepareStatement(Constants.INSERT_ORACLE_SQL);

        userId = id.getAndIncrement();

        preparedStatement.setString(1, String.valueOf(userId));
        preparedStatement.setString(2, Constants.getaAccount(userId));

        preparedStatement.executeUpdate();
    }  
}

And below is my Constants class which I have made immutable.

public final class Constants {

    public static String A_ACCOUNT;

    public final static String INSERT_ORACLE_SQL = "INSERT INTO XMP_TEST"
        + "("
        + "ID, A_ACCOUNT) VALUES"
        + "(?, ?)";



    public static String getaAccount(int userId) {      
        A_ACCOUNT = "{\"lv\":[{\"v\":{\"userId\":"+userId+"},\"cn\":1}]}";

        return A_ACCOUNT;
    }


}

Can anyone tell me what wrong I am doing here? I believe it’s happening because of thread safety issue. Multiple threads modifying the userID integer I guess and that’s why it is getting written wrongly to database.

How can I fix this problem?

  • 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-18T12:36:55+00:00Added an answer on June 18, 2026 at 12:36 pm

    The major problem I see is not with Task.userId, but rather with Constants.A_ACCOUNT: if two separate threads call getaAccount at the same time, then they’ll both set Constants.A_ACCOUNT and both read it, so they can end up both having the same value, or each having the other’s value, or whatnot. To fix this, you can use a local variable instead of a static field:

        public static String getaAccount(int userId) {      
            final String ret = "{\"lv\":[{\"v\":{\"userId\":"+userId+"},\"cn\":1}]}";
    
            return ret;
        }
    

    or just dispense with the variable:

        public static String getaAccount(int userId) {      
            return "{\"lv\":[{\"v\":{\"userId\":"+userId+"},\"cn\":1}]}";
        }
    

    (You say that you’ve made Constants immutable, but that’s not really true. Instances of Constants will be immutable, because they have no fields at all; but Constants itself has a publically modifiable field, so it’s very mutable!)

    More generally, you shouldn’t be using fields for temporary values only needed within a specific method, and only during a single call to it. Even when it’s not a synchronization problem, it’s a maintenance problem. For example, Task does not need a volatile int userId; userId should just be a local variable inside its run method.

    Also, I’d recommend wrapping your AtomicInteger in its own class, IncrementingCounter or something, that offers just one method, called (say) getNewId. Then getNewId will be the only class that has to deal with coordination between threads. All other classes can be made threadsafe by regular techniques (immutability, only existing within a single thread, etc.).

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

Sidebar

Related Questions

I need to insert into database which has two columns- ID Primary Key String
I need to insert data into a column in the database. Which hook should
I need to insert rows rows into a database, then in another thread read
i have asp.net project which has dropdownlist. i need to insert data into my
I have a database that has two tables, one of which contains a foreign
I need to copy data into an MSSQLServer 2005 database table which has an
Here my code, I need to insert into mysql database I have mysql,php,android 1)
I need a way to insert new articles straight into my MediaWiki database without
I have around 4000 entities that I need to insert into a Java App
I need to insert some details into a database and get the auto_incremented ID

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.