Prepared Statement in Java Client


I am using the latest version of java client 2.2.2 but it looks like it is not possible to do prepared statement with it. As Couchbase Server 4.1 GA is out and officially support prepared statement, any idea or time frame to an update to the SDK to support it?

Many thanks.

@lbertrand it is! on the query params, set adhoc(false) and you’re good to go. It will automatically use prepared statements in the background. If you have changing parameters, I recommend using parameterized queries and setting adhoc(false)

Great. Did not realised it was automatically handle by the SDK!

I am using ParameterizedN1qlQuery. unable to find where to set adhoc(false).

It is part of the N1qlParams that you pass to the method to create your parameterized query.

Example: N1qlQuery query = N1qlQuery.parameterized(myStatement, myPlaceholderValues,;

Thank you @lbertrand. It worked :slight_smile:

glad you folks like it! :smile:

@daschl I’m completely new to Couchbase and I’m very curious about how this works under the hood (both server and the Java sdk). I have the following questions:

  1. Suppose I have multiple front-end servers that use the same prepared statement via this adhoc(false), will the Couchbase server create duplicated prepared statements that are essentially the same? Can it happen that over time, there are too many prepared statements stored in the server?

  2. Is there anyway to give a name to the prepared statement so that all front-end servers will use the same prepared statement? And if yes, is there a way to delete a prepared statement using its name? Couchbase server also creates random names for the prepared statements. How are these handled at the sdk level?

  3. There’s a mention of caching of query plan of the sdk in the doc. So when a statement with adhoc(false) is created, the sdk would search in cache using the statement for a cached query plan and execute it if possible. Otherwise, it will tell the server to execute the plan saved in the server?

Thanks a lot!