简体   繁体   中英

How to handle concurrent database request or How to run a short sql query, while a long running query is in progress

Consider there is a user request which needs an sql query to be executed at back-end and resultant data needs to be returned as response, but the user has already initiated another request that consist of a very long running sql query.

While the longer query is in progress, I need the the database to handle the shorter query (give the shorter query a bit more priority than long running query).

  1. Is it possible to achieve such concurrent execution by tweaking the database server?
  2. Can Java Threads be used for achieving this? Is it a right choice?

Most databases support concurrent statement execution, usually with Multiversion concurrency control (MVCC) or other mechanism. The client can usually open multiple connections to the database server and run different SQL statemenets in each connection.

You need to check how your database server handles concurrency, you can start by taking a look at this answer for MySQL . Then look into JDBC Basics docs to understand how to open multiple connections. There will be gotchas eg transaction isolation levels or lock handling that will be specific to your use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM