Java jdbc design pattern: handle many inserts

I would like to ask for some advice regarding my problem. I have a package that does some calculations (multi threading environement) and does some insertion in a table. I would like to do something like batch insertion, which means that as soon as I receive the request, wait for example 1000 requests and then start batch insertion (without doing it one by one).

I was wondering if there is any design pattern. I have a solution, but it is a bit complicated:

  • create a method that will receive requests

  • add them to the list (string and / or instructions)

  • are not executed until there are 1000 items in the list

Problem: how do I handle the end? I mean, the last 999 queries, when I execute them, since I will never reach 1000? What should I do?

I am thinking of a thread that wakes up every 5 minutes and checks the number of items in the list. If he wakes up twice, and the number is the same, complete the existing queries.

Anyone have a better idea?

+3
source share
3 answers

The database driver must support batch insertion. Watch this.

Did you find that your system is blocking network traffic because there is too much communication between the service and the database? If not, I would not worry about the dosage until you are sure that you need it.

, 5 . . 1000 5 , . ~ 3 .

, , 2 , . . 0 , 10... ... , .

, . n , , , . - , , . , .

+2

commit API, , . , - 20-50. , . , , , API JDBC.

, , begin, , , , commit. - . , , .

0

, . , , , 1000 . , .

, : -

INSERT INTO table1 VALUES ('4', 'India'), ('5', 'Odisha'), ('6', 'Bhubaneswar')

. , (arraylist, list ..) , , , , .

API SQL (Commit, rollback, setTraction()) ..

, . .

0
source

All Articles