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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:16:39+00:00 2026-06-19T03:16:39+00:00

I have started working on a project in which I have two tables in

  • 0

I have started working on a project in which I have two tables in a different database with different schemas. So I will be having two different connection parameters to connect to the database. And after connecting to each database, I need to insert in those two tables using the sql given in corresponding table.

And I am supposed to insert in all the two tables or any one of them using JDBC depending on the command line parameters. So that means a single thread will be inserting into Table1 and Table2 or any one of them.

Command Line Parameters:- Here 10 is number of threads, 100 is number of tasks, table1 and table2 are table names.

10 100 table1 table2

Below is my code. In this code what will happen is- suppose if we are passing only one table named table1, then it will insert into that table by using the SQL meant for table1.

And also I need to insert same id in both table1 and table2 and that id is being passed as AtomicInteger to Constructor Task. So that means if id is 1, then this 1 id should be there both in table1 and table2.

 final AtomicInteger id = new AtomicInteger(1);

 ExecutorService service = Executors. newFixedThreadPool(noOfThreads);

 for (int i = 0; i < noOfTasks * noOfThreads; i++) {

      for (String arg : tableNames) {
          String url = (String) prop.get(arg + ".url");
          String user = (String) prop.get(arg + ".user");
          String password = (String) prop.get(arg + ".password");
          String driver = (String) prop.get(arg + ".driver");
          String suffix = (String) prop.get(arg + ".suffix");
          String sql = (String) prop.get(arg + ".sql");

          service.submit( new Task(id, url, user, password, driver, sql, suffix));
        }
    }

Below is the Task class that implements Runnable interface

class Task implements Runnable {

    private final AtomicInteger id ;
    private final String url ;
    private final String username ;
    private final String password ;
    private final String sql ;
    private final String driver ;
    private final String suffix ;

    public Task(AtomicInteger id, String url, String user, String password, String driver, String sql, String suffix) {
        this.id = id;
        this.url = url;
        this.username = user;
        this.password = password;
        this.driver = driver;
        this.sql = sql;
        this.suffix = suffix;
    }

    @Override
    public void run() {

        try {

           dbConnection = getDBConnection(url , username , password , driver );
           callableStatement = dbConnection .prepareCall(sql);

           int userId = id .getAndIncrement();

           callableStatement.setString(1, String.valueOf(userId));

        //other callableStatement


        callableStatement.executeUpdate();
       }
    }

So If I am running the above program with multiple threads like Number of Threads as 10 and Number of Tasks as 1000 and id if I choose as 1.

Then in both the tables same id is not there, meaning id 1 will be there only in one table either table1 or table2. The only reason I can think of is- id is AtomicInteger so everytime it will get a new id for each thread. Is there any way I can use the same id to insert in each of the tables? And then making sure id is PrimaryKey so each thread will get a new id if it is inserting again in those tables.

  • 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-19T03:16:41+00:00Added an answer on June 19, 2026 at 3:16 am

    If I understand you correctly, you should define the counter as static to avoid duplicate PrimaryKey:

    static AtomicInteger id;
    

    Then move the increment code to the main loop like this(do not forget to remove the int userId = id .getAndIncrement(); in the Task class):

    for (int i = 0; i < noOfTasks * noOfThreads; i++) {
      // add following line
      int userId = id.getAndIncrement();
    
      for (String arg : tableNames) {
          String url = (String) prop.get(arg + ".url");
          String user = (String) prop.get(arg + ".user");
          String password = (String) prop.get(arg + ".password");
          String driver = (String) prop.get(arg + ".driver");
          String suffix = (String) prop.get(arg + ".suffix");
          String sql = (String) prop.get(arg + ".sql");
    
          service.submit( new Task(id, url, user, password, driver, sql, suffix));
        }
    }
    

    Besides, as the id counter is kept in memory, you might need to persistent it, otherwise it would be reset to 0 after restarting your application.

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

Sidebar

Related Questions

I have started working on a project which requires Natural Language Processing. We have
I've started working on a little ruby project that will have sample implementations of
Recently we started working with Database project in Visual Studio 2010. I have added
I have started working on a legacy project (ASP.NET 1.1) which was done in
I have started using Linq to SQL for a project im working on and
everyone, I've started working yesterday on the Euler Project in Clojure and I have
I am new to Django and have started working on a mature Django project.
I have just started working on an MVC project and things are going ok
I have been working on a project which is about data copy from one
I have started working on my own project, but I am stuck due to

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.