0

In my current project if something needs to be written in db asynchronously(to log some data) we are using thread pool executors. As It is just an IO operation instead of using thread for each write call can we have something similar to Event driven mechanism. Where a single thread will be running in while loop and will make async db call.similar to java NIO is done for network calls.

2 Answers 2

1

How about single thread executor?

Sign up to request clarification or add additional context in comments.

Comments

1

No, you're going to need at least one thread no matter what you do. If you did have a driver that uses NIO you would still have an extra thread that runs select().

I'm not sure how you've implemented it in your current project, but even when using executors (as was suggested, SingleThreadExecutor is enough) it shouldn't be very complicated.

2 Comments

Isn't it true that in this case statements (updates) will only be run sequentially? Slow statements will affect other statements? N statements taking t each to execute will take N*t even if the DB could handle them in parallel? The queue of updates can grow faster than with many requests attempted concurrently?
I'm assuming inserting a log statement isn't going to be an expensive operation (if it is, then you should be tuning the database, not Java code). Even then, parallelizing isn't going to be very helpful, as they're working with the same table. More efficient would be to do batching. The question didn't really provide much information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.