In your code example, I believe the string supplied by searchParams.getSampleId() could introduce a side effect. It’s hard to say exactly the possibilties, but I believe one problem you could have with the above is inserting a subquery since that is valid in a WHERE clause. That may expose data you didn’t mean to be exposed or have another side effect.
By using placeholders, the statement will be parsed for execution by cbq-engine, and then the parameters are applied later using cbq-engine’s API for this.
The .adhoc() parameter on the query will have the Java SDK automatically set up prepared statements for the query and execute them once prepared, which are more of a performance optimization. You can have a combination of parameterized/placeholders and prepared statements, or both independently.
By the way, there is a .toString() on the query if you want to log it to have @geraldss take a look.
However I am getting following error if I use adhoc=false.
Error occurred in Couch base Count{"msg":"Unable to decode prepared statement - cause: Unrecognizable prepared statement - cause: JSON unmarshalling error: Keyspace not found keyspace - cause: No bucket named ","code":4070}
Following is the generated query:
Query: ParameterizedN1qlQuery{statement=SELECT count(*) as size FROMdefaultWHERE sample_id = $sample_id LIMIT 20, params={"sample_id":"abc"}}
If it works without being prepared, it should also work as prepared. The error returned doesn’t trigger any particular thoughts to me, but it might to @marcog.
We can look into it further though. What version of server/sdk are you using? Note that there have been a number of prepared statement fixes over the various versions, so if you’re not on 4.6, I’d recommend trying that first.
The encoded plan is not unmarshalling correctly on the target n1ql service.
Could I have a look at the relevant entry from system:prepareds (specifically, I need generated text as stored in the prepared cache, and the encoded plan) on the node where the request has been successfully prepared?