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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:01:39+00:00 2026-05-12T11:01:39+00:00

I’m developing a server that should receive nightly reports from hundreds of business units.

  • 0

I’m developing a server that should receive nightly reports from hundreds of business units. Reports are currently encrypted csv-files. In total the reports should amount to 500 000 to 1 000 000 records each day that are saved to a database for later use.

I’ve create a set of PreparedStatements for each transmission. These statements are used to batch 50 records before executing and commiting. Each record may cause up to 20 database inserts. All is well when transmissions are queued and handled one-by-one.

As I tried to do this concurrently I noticed that different threads got the exact same instances of the PreparedStatements. This caused the following problem

  1. Multiple threads added statements to the same batch
  2. Batches were being executed when any of the threads decided it was time to do so
  3. Commit was called when database did not meet it’s constraints as some of the threads had not had time to use some of the statements

The question is: Is there are way to force a prepared statement to be created instead of reusing an existing one from the statement cache?

If not is there any better way to handle the situation than by

  • creating a separate data source for the batches that does not have statement/connection pooling
  • dropping constraints from the database; insert order would not matter anymore
  • forcing sequential processing

Edit: Attempt to clarify the problem

Let there be threads T1 and T2.
Let there be prepared statements S1 and S2.
Let there be batches B1 and B2.

Each time S1 is used, it is added to B1. Each time S2 is used, it is added to B2.
When commiting, S1 must be commited before S2 per foreign key constraint.

Problem occurs when

  • T1 processes transmissions gleefully
  • T2 processes transmissions innocently
  • T1 uses statement S1 adding s1a to batch B1 containing s1a
  • T1 uses statement S2 adding s2a to batch B2 containing s2a
  • T1 decides it is time to commit
  • T1 commits batch B1 containing s1a
  • T2 uses S1 adding s1b to batch B1 containing s1b
  • T2 uses S2 adding s2b to batch B2 containing s2a, s2b
  • T1 commits batch B1 containting s2a, s2b
  • Database says ‘no no’ as s2b is commited before s1b which is forbidden in the foreign key.

This can be avoided with manual synchronization as well as pointed in the answers but then I still have to track separately the size of each batch instead of applying logic local to each thread.

  • 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-05-12T11:01:40+00:00Added an answer on May 12, 2026 at 11:01 am

    My current solution is stop worrying and start loving the shared batches. I have split the processing algorithm to two phases

    1. Parse a set of N records and save them in an intermidiate format
    2. Persist the set of N records as a batch when a lock is awarded to the current thread

    This allows the parsing to concurrent and batching sequential. I’ll just have to find a sweet spot to minimize the waiting time between threads.

    The quest for a sweet spot may lead to implementing some sort of a two-phased locking scheme i.e. let each thread do as they please and on commit, make sure all threads have completed their current record before the actual batch execution.

    In the latter solution it might be necessary to synchronize over parameter setting for each PreparedStatement although I haven’t tested if that causes any trouble. It should.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a

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.