More than 1 Couchbase Environments found

Oh and btw, you can read more about it in the docs under “Sharing Resources” https://developer.couchbase.com/documentation/server/3.x/developer/java-2.0/managing-connections.html

1 Like

@Sam_K @daschl @ingenthr
Hey i have a doubt. I am getting error while defining bean in the same way you created. It is not able to resolve “couchbaseEnvironment” while creating bean.
any help ??

are you sure you got the bean above in your own configuration? (first one in the config presented by Sam_K)

oops…I missed that…now its working fine upon including.
Thanks !! :slight_smile:

you’re welcome :sun_with_face:

one more help.
How do i create a bean for a particular bucket in that cluster from the cluster bean created ??

my spring-with-xml days are way past me, I’m not sure this can easily be done since the method for creation is neither a constructor nor a factory method on the Bucket class…
you’d have to look up if invoking another bean’s method + passing in bucket name and password is feasible in Spring (especially with xml configuration)…

ohh
@ingenthr @daschl @Sam_K
can anyone else help ??
i want to create bean for a bucket in my xml.

@akshay I recommend you to check out the spring documentation, creating beans is not something that strictly relates to couchbase. You might want to start here: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-class-instance-factory-method by using instance factory methods.

ok…great
Thanks !! :slight_smile:

@akshay ~ I saw later. Try this

<bean id="couchbaseEnvironment" class="com.couchbase.client.java.env.DefaultCouchbaseEnvironment"
      factory-method="create"/>
<bean id="cluster" class="com.couchbase.client.java.CouchbaseCluster" factory-method="create"
      destroy-method="disconnect">
    <constructor-arg name="environment" ref="couchbaseEnvironment"/>
    <constructor-arg name="nodes">
        <list value-type="java.lang.String">
            <value>{TYPE COUCHBASE CLUSTER IP}</value>
        </list>
    </constructor-arg>
</bean>
<bean id="bucket" class="com.couchbase.client.java.Bucket" factory-bean="cluster"
      factory-method="openBucket">
    <constructor-arg name="name" value="{TYPE BUCKET NAME}"/>
</bean>
2 Likes

Thanks @Sam_K
i already did that.
I am facing another issue now. I have defined these beans in file named “applicationContext.xml” and when i am using
Bucket bucket = (Bucket) ( new ClassPathXmlApplicationContext(“applicationContext.xml”).getBean(“bucket”) );
in different classes.And it is creating different environments each time instead of reusing the same environment and giving the same “more than one couchbase environments found” warning.
What wrong am i doing ??

@akshay You are either creating more than one CouchbaseEnvironment or more CouchbaseCluster objects (without passing in the environment to share). Please see http://docs.couchbase.com/developer/java-2.1/managing-connections.html

yeah @daschl
I fixed that now. Thanks for the help !!

I’ve run into this as well… Basically we manage our own configuration and I’ve been able to track and re-use the duplicated cluster info. But… If the client itself detects a duplicate, why would it throw this warning out and not just mitigate the duplicate automagically? Or, is there a reason why one would want to have this behavior?

@unhuman the tracking is kept simple (a static counter incremented and checked during construction of the env) and I think there might be rare but legit corner cases where 2 environments actually make sense so we want to issue a warning to the user, not forcibly change the behavior.

It’s more about limiting creation of multiple environments than about duplicates as in “exactly the same configuration” (a scenario where auto-deduplication could have been implemented, but with the added complexity of managing these checks outside of the constructor of the env, while ensuring they are performed everywhere the env can be created by the user, etc…).

hi @daschl ,

I also get the same error.
In my case, I’m using flink with 3 different jobs.
Each flink job has it’s own connection (I’m not sure how to share that env from different jar / job)
Is it safe to ignore that warning?
Let me know how to fix it.
Thanks

If you cannot share it is OK to ignore it in smaller numbers, but be informed that it is not the best use of resources (thread pools etc). If you can, share it. If it runs in the JVM on the same classloader, using a singleton with static might work?

Hi @daschl ,

Yes, it’s possible using same core using singleton, but this means i will remove the disconnect part?
Bcs if I close / disconnect 1 of my job, the other might be impacted.

@Han_Chris1 you can call disconnect on each Cluster where you share the environment no problem, the only thing you need to be careful is how you shut down the environment itself at the end. Ideally there is some kind of shutdown hook that flink provides you, or lets you know it’s going to complete. In the worst case you can leave out the environment shutdown since it has dameon threads and will not block anything. But it’s important to disconnect your clusters on shutdown, since that will make sure that there are no outstanding operations.