Prepared Statement in Java Client


#1

Hi,

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.


#2

@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)


#3

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


#4

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


#5

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

Example: N1qlQuery query = N1qlQuery.parameterized(myStatement, myPlaceholderValues, N1qlParams.build().adhoc(false));


#6

Thank you @lbertrand. It worked :slight_smile:


#7

glad you folks like it! :smile:


#8

@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!