سؤال

I'm working on a Java application which uses the Ektorp Framework to communicate with a CouchDB. I use the following code to create a new CouchDbInstance:

HttpClient httpClient = new StdHttpClient.Builder()
    .host("localhost")
    .port("5984")
    .username("")
    .password("");

/* no user name and password required because, its admin party */

CouchDbInstance couchDbInstance = new StdCouchDbInstance(httpClient);

Now I want to test the connection to the CouchDB before I create/modify etc. documents. There is no method for testing the connection. Do you have a tip for me?

هل كانت مفيدة؟

المحلول

I'm using Ektorp in my current project and we're testing this with couchDbConnector.getAllDatabases(), catching DbAccessExceptions.

This has the slight advantage over checkIfDbExists() of being totally agnostic of the actual databases configured on the CouchDB server, and not referencing any application-specific configuration for connection checking just feels a little cleaner.

نصائح أخرى

You can use CouchDbConnector#getDbInfo()

I'm currently working on a project using ektorp and I am testing my database connection like this.

Step 1 -- Connect to the HttpClient as you have done

    HttpClient httpClient = new StdHttpClient.Builder()
    .host("localhost")
    .port("5984")
    .username("")
    .password("");

/* no user name and password required because, its admin party */

CouchDbInstance couchDbInstance = new StdCouchDbInstance(httpClient);

Step 2 -- Create a CouchDbInstance

CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);

Step 3 -- Use CouchDbConnector to connect to a specific database in your localhost CouchDB (in this example my database is called albums but you just put in the name for whatever database you are connecting to in place of albums from where you are coding)

CouchDbConnector albums = dbInstance.createConnector("albums", true);

Step 4 -- You can print off the name of the database you are connected to like this.

System.out.println(albums.getDatabaseName());

You could also create an endpoint that would print the name of the database you are connected to in the browser by creating a method of type string with

return albums.getDatabaseName();

I hope this is helpful. I used ektorp's reference documentation to learn this. It took me a while to figure out how to use ektorp like this and I am still learning!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top