Hi,
Please see below code for singleton instance of Couchbase.
public enum CouchbaseInstance {
INSTANCE; private Cluster cluster = null; private final Logger logger = Logger.getLogger(CouchbaseInstance.class); private CouchbaseInstance() { try { if (cluster == null) { cluster = getCluster(); logger.info("Couchbase connection created successfully"); } } catch (CouchbaseException ex) { logger.error("ERROR while connecting to Couchbase", ex); } } private synchronized Cluster getCluster() { String host = PropertyFileReader.getValue("COUCHBASE_HOST"); return CouchbaseCluster.create(host); } public Bucket getBucket(String bucketName) { if (cluster == null) { logger.warn("Couchbase cluster is null"); cluster = getCluster(); } return cluster.openBucket(bucketName); }
}
and i am doing operations on couchbase like:
Bucket bucket = CouchbaseInstance.INSTANCE.getBucket(“test_bucket”);
bucket.upsert(JsonDocument.create(id, expirySecs, content));
so my question is, is this heavy to call openBucket()
every time i do operation or is this a good approach.
Thanks