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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:27:08+00:00 2026-06-18T10:27:08+00:00

When an external event occurs (incoming measurement data) an event handler in my Java

  • 0

When an external event occurs (incoming measurement data) an event handler in my Java code is being called. The data should be written to a MySQL database. Due to the high frequency of these calls (>1000 per sec) I’d like to handle the inserts efficiently. Unfortunately I’m not a professional developer and an idiot with databases.

Neglecting the efficiency aspect my code would look roughly like this:

public class X {

    public void eventHandler(data) {
        connection = DriverManager.getConnection()
        statement = connection.prepareStatement("insert …")
        statement.setString(1, data)
        statement.executeUpdate()
        statement.close()
        connection.close()
    }
}

My understanding is that by calling addBatch() and executeBatch() on statement I could limit the physical disk access to let’s say every 1000th insert. However as you can see in my code sketch above the statement object is newly instantiated with every call of eventHandler(). Therefore my impression is that the batch mechanism won’t be helpful in this context. Same for turning off auto-commit and then calling commit() on the connection object since that one is closed after every insert.

I could turn connection and statement from local variables into class members and reuse them during the whole runtime of the program. But wouldn’t it be bad style to keep the database connection open at all time?

A solution would be to buffer the data manually and then write to the database only after collecting a proper batch. But so far I still hope that you smart guys will tell me how to let the database do the buffering for me.

  • 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-18T10:27:09+00:00Added an answer on June 18, 2026 at 10:27 am

    I would suggest you use a LinkedList<> to buffer the data(like a queue), then store the data into the dbms as and when required in a separate thread, executed at regular intervals(maybe every 2 seconds?)

    See how to construct a queue using linkedlist in java

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

Sidebar

Related Questions

I need to perform data import from external source to my database. Because there
When I'm trying to delete an external event from my calendar, if I add
How can i rewrite all external links with a single onmousedown event? Kinda like
Visual studio 2005: I am moving from post build event to using the external
I have a problem; I'm using an external library where one particular event has
No functions get called, and I can't even alert. It is just an external
I am designing a database for a system where billing occurs at regular, custom
For an ordinary binary semaphore, a task attempting to synchronize to an external event
Am using fullcalendar external event drag. When I create those external event (that is
I have been working on code that parses external XML-files. Some of these files

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.