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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:19:29+00:00 2026-06-18T08:19:29+00:00

I am trying to insert into a XpressMP database, It’s a new database. I

  • 0

I am trying to insert into a XpressMP database, It’s a new database. I wrote a multithreaded program for that to insert into the database with multiple threads.

I am trying to insert into the below tables with two columns- ID and ACCOUNT

ID      String Primary Key
ACCOUNT String

So currently I have ID as a Primary key. And I need to generate a new ID every time whenever I am inserting in the database otherwise it will throw an exception showing-

duplicate row in unique index:

So for that I made a class Immutable like this in which I have user_id which gets generated everytime a new thread will ask for-

public final class ConstantsFiles {

    private static Random rnd = new Random();
    private final static int NO_OF_DIGITS = 10;

    public static String user_id;

    public final static String ACCOUNT = "{\"lv\":null,\"lmd\":13597}";

    public final static String INSERT_SQL = "INSERT INTO COPY"
        + "("
        + "ID, ACCOUNT) VALUES"
        + "(?, ?)";

    /**
     * Generates a Random user id
     * 
     */

    public static String getUser_id() {
        StringBuilder sb = new StringBuilder(NO_OF_DIGITS);
        for (int i = 0; i < NO_OF_DIGITS; i++)
            sb.append((char) ('0' + rnd.nextInt(10)));

        return sb.toString();
    }

}

Below is my multithreaded code which gets the new ID everytime from the get method I have in my immutable class above-

@Override
public void run() {

    try {
        dbConnection = getDBConnection();
        preparedStatement = dbConnection.prepareStatement(ConstantsFiles.INSERT_SQL);

        preparedStatement.setString(1, ConstantsFiles.getUser_id());
        preparedStatement.setString(2, ConstantsFiles.ACCOUNT);

        preparedStatement.executeUpdate();

    } catch (Exception e) {
        LOG.error(e);
    } finally {
        // close connection
    }
    }

In which each thread will generate a new USER ID every time and use the same ACCOUNT value every time and insert into the database.

But somehow at some stage it is throwing me an exception-

duplicate row in unique index:COPY_PRIMARY_KEY_

That means at some point it is using the same USER ID to insert into the database and because of that it is throwing the exception.

Any thoughts why it is happening. I have already made the class immutable and added the get method. Then what’s the problem? May be my class is not immutable?

I just need to insert into database with a NEW USER ID getting generated for each new thread but I can use the same ACCOUNT value every time.

  • 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-18T08:19:30+00:00Added an answer on June 18, 2026 at 8:19 am

    Random does not guarantee unique. You need to open a transaction with the database and make sure it’s unused before you insert or this is guaranteed to happen eventually.

    Edit: I don’t know XpressMP at all, but in pseudo-SQL you’d need to do something like:

    BEGIN
        DECLARE @free = NULL
        LOCK COPY
        SELECT INTO @free ID FROM COPY WHERE ID = ?
        IF free IS NULL
            INSERT ....
        UNLOCK COPY
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to insert information into multiple tables within a database, i have managed
I am trying to insert data into a SQLite database that is used for
I am trying to insert into Oracle Database using JDBC prepared statement. Below is
I'm trying to insert data into a database that I have on my server.
I am trying to insert values into Oracle 10g database using the code below
Trying to insert into a mongodb database from scala. the below codes dont create
I am trying to insert data into two different tables in the same database,
I'm trying to insert into a database with the code below, I've tried everything
I am trying to insert into my database table using the database helper class.
I'm trying to insert multiple rows into MySql with only one INSERT INTO statement

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.