Handling Password for Couchbase bucket using Java SDK

Hi,
We have a Java application which connects to Couchbase bucket. Currently there is no password set for the bucket. We are in process of setting up a password for the bucket. We want to handle the old and new password in our application and deploy the changes before the Couchbase Password is set. How can we handle the same without any downtime for the application ?

Couchbase Server version - Version: 4.1.1 Community Edition.
Using Spring data Couchbase to connect to the Couchbase cluster.

Handling the cases - when we restart the application Server -
We have tried overriding couchbaseClient() method to handle openBucket method to try with new password first and if there is a InvalidPasswordException then try without password. Did the same for method couchbaseClusterInfo() as well. This works fine.

Handling the cases - when application server is up and the password is set

  1. In this case the operations using the document id works fine.
  2. N1QL give the below exception -
    Unable to execute query due to the following n1ql errors: \n{\"msg\":\"Authorization Failed \",\"code\":10000}

Please suggest how to handle the N1QL related exception.

Error with stacktrace:
Unable to execute query due to the following n1ql errors: \n{\"msg\":\"Authorization Failed \",\"code\":10000}","error_message":"o.s.d.c.c.CouchbaseQueryExecutionException: Unable to execute query due to the following n1ql errors: \n{\"msg\":\"Authorization Failed \",\"code\":10000}\n\tat o.s.d.c.c.CouchbaseTemplate.findByN1QL(CouchbaseTemplate.java:458)\n\tat o.s.d.c.r.q.AbstractN1qlBasedQuery.executeCollection(AbstractN1qlBasedQuery.java:156)\n\tat o.s.d.c.r.q.AbstractN1qlBasedQuery.executeDependingOnType(AbstractN1qlBasedQuery.java:131)\n\tat o.s.d.c.r.q.AbstractN1qlBasedQuery.execute(AbstractN1qlBasedQuery.java:106)\n\tat o.s.d.r.c.s.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:590)\n\tat o.s.d.r.c.s.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578)\n\tat o.s.a.f.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)\n\tat o.s.d.p.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)\n\tat o.s.a.f.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)\n\tat o.s.d.c.r.s.ViewPostProcessor$ViewInterceptor.invoke(ViewPostProcessor.java:87)\n\tat o.s.a.f.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)\n\tat o.s.a.i.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)\n\tat o.s.a.f.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)\n\tat o.s.a.i.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)\n\tat o.s.a.f.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)\n\tat o.s.d.r.c.s.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)\n\tat o.s.a.f.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)\n\tat o.s.a.f.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)\n\tat c.s.proxy.$Proxy130.findByManagedBy(Unknown Source)\n\tat s.r.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java)