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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:24:24+00:00 2026-06-15T06:24:24+00:00

I have a table named CUSTOMERS with the following columns : CUSTOMER_ID (NUMBER), DAY(DATE),

  • 0

I have a table named CUSTOMERS with the following columns :

CUSTOMER_ID (NUMBER), DAY(DATE), REGISTERED_TO(NUMBER)

There are more columns in the table but it is irrelevant to my question as only the above columns are defined together as the primary key

In our application we do a large amount of inserts into this table so we do not use MERGE but use the following statement :

INSERT INTO CUSTOMERS  (CUSTOMER_ID , DAY, REGISTERED_TO)                      
                   SELECT ?, ?, ? 
                   FROM DUAL WHERE NOT EXISTS 
                            (SELECT NULL 
                             FROM CUSTOMERS
                             WHERE CUSTOMER_ID = ? 
                               AND DAY = ? 
                               AND REGISTERED_TO = ?
                              )";

We use a PreparedStatement object using the batch feature to insert a large number of records collected through the flow of the application per customer.

Problem is that sometimes I get the following error :

ORA-00001: unique constraint (CUSTOMERS_PK)
violated

Strange thing is that when I do NOT use batch inserts and insert each record one by one (by simply executing pstmt.execute()) there are no errors.

Is it something wrong with the insert statement ? the jdbc driver ? Am I not using the batch mechanism correctly ?

Here is a semi-pseudo-code of my insertion loop :

  pstmt = conn.prepareStatement(statement);
  pstmt.setQueryTimeout(90);

  for each customer :
     - pstmt.setObject(1, customer id);
     - pstmt.setObject(2, current day);
     - pstmt.setObject(3, registered to);
     - pstmt.addBatch();
  end for

  pstmt.executeBatch();

It is all enclosed in a try/catch/finally block making sure the statement and connection are closed at the end of this process.

  • 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-15T06:24:25+00:00Added an answer on June 15, 2026 at 6:24 am

    I guess you are using several threads or processes in parallel, each doing inserts. In this case, Oracle’s transaction isolation feature defeats your attempt to do the merge, because sometimes the following is bound to happen:

    • session A runs your statement, inserts a row (x,y,z)
    • session B runs the same statement, tries to insert row (x,y,z), gets a lock and waits
    • session A commits
    • session B receives the “unique constraint violated” error

    That’s because until session A commits, session B doesn’t see the new row, so it tries to insert the same.

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

Sidebar

Related Questions

Lets say I have table named Place with columns: placeId int not null auto_increment,
I have the following two tables and data:- CREATE TABLE customers ([id] int, [name]
I have table with the folowing columns: row_id, customer_id, worker_id, vehicle_id, task_id, subcontractor_id. I
suppose I have the following tables and relations: CREATE TABLE [dbo].[Customers] ( [CustomerID] [int]
I have a db with the following tables: customer_info table with the following columns:
Lets say i have a table with the following data Customer table: Name amount
I have a simple database named customer with a single table data.I want to
I have 3 tables: Customers ID_CUSTOMER NAME Products ID_PRODUCT PRODUCTNAME PRICE Orders ID_ORDER CUSTOMER_ID
I have a table named tbl_Subjects_Taken which lists all the subjects taken by the
I have a table named datas and I'm executing a query like this: SELECT

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.