Skip to content

Set custom query id using ThreadLocal #2382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

skauppin
Copy link

Summary

Older JDBC driver had the possibility to set a custom query ID using com.clickhouse.client.ClickHouseRequestManager. Seems like the latest version doesn't have a way to do the same. We have a use case where we need to check whether an database query (maybe an insert also) is already running and not start it again if it is. This may happen e.g. if the operation times out on the client side.

How to now set a custom query ID

Client.setThreadQueryId("my-query-id");

// execute a query

Client.removeThreadQueryId();

This seems to set the query Id as we want, even in the case that we are using Spring JdbcTemplate for executing db operations. com.clickhouse.client.api.query.QuerySettings has a setter and getter for a query ID, but we did not find any way to actually set it that way.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@chernser
Copy link
Contributor

Good day, @skauppin !

Do you need client to check if query is running? (It would be great if you describe your need in an issue)

Here is an example how it supposed to be used:

 settings = new InsertSettings()
                .setDeduplicationToken(RandomStringUtils.randomAlphabetic(36))
                .setQueryId(String.valueOf(UUID.randomUUID()));
           InsertResponse response = client.insert(tableName, simplePOJOs, settings).get(EXECUTE_CMD_TIMEOUT, TimeUnit.SECONDS);

It is up to application how to implement checking queryID.

As for JDBC - it should be implemented in JDBC code. But we need kind of feature description to understand how to implement.

Thanks!

@skauppin
Copy link
Author

Thanks for the quick reply!

I created an example project showing how we are currently executing ClickHouse operations using Spring JdbcTemplate:
https://github.com/skauppin/spring-clickhouse-db
and specifically
https://github.com/skauppin/spring-clickhouse-db/blob/master/src/main/java/com/example/spring_clickhouse_db/DatabaseOperations.java

ClickHouse jdbc driver version is 0.7.2, and this version supports creating a custom implementation of com.clickhouse.client.ClickHouseRequestManager that we use to set query Id.

The point is that we are not using Clickhouse client directly, but relying on standard JDBC api and Spring JdbcTemplate on top of that.

Hope this example clarifies it. I can also try to create an issue if needed.

@chernser
Copy link
Contributor

@skauppin
I appreciate the example and will look into it this week.
I agree we need to support the Spring and will try to find suitable way for this.

@chernser
Copy link
Contributor

/windsurf-review

Copy link

@windsurf-bot windsurf-bot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file skipped due to size limits:
  • client-v2/src/main/java/com/clickhouse/client/api/Client.java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants