jeudi 1 octobre 2015

How to design http connection to database in concurrent environment (static variable or not)

I am using dynamodb from amazon web services as my database. The client providd by AWS uses http to make the requests to the database. This code will be on a server which will accept requests from users and send it over to dynamodb. I had a few questions how to design this then.

Since this is a server accepting many requests I am using the async client (http://ift.tt/1KQzOyW)instead of the sync because I don't want for every request to block and instead I will wait for a future to return (better performance). Is it best to make this client static?

public class Connection {

    AmazonDynamoDBAsyncClient client;
    static DynamoDB dynamoDB;

    public Connection(){

        client = new AmazonDynamoDBAsyncClient(new ProfileCredentialsProvider());
        dynamoDB = null;

    }

    public void setConnection(String endpoint){
        client.setEndpoint(endpoint);
        dynamoDB = new DynamoDB(client);
    }


    public DynamoDB getConnection(){
        return dynamoDB;
    }

}

Then to call this static variable from main:

public class Main{

    Connection c;
    DynamoDB con;

            public  Main() throws Exception {

                try {
          c = new Connection();
          c.setConnection("http://dynamodbserver:8000");
          con = c.getConnection(); 
          //Do stuff with the connection now
                    } catch (Exception e) {
                     System.err.println("Program failed:");
                     System.err.println(e.getMessage());
                 }

Is this a good approach? What will happen if two users are requesting to use the static variable at the same time (I am using a framework called vertx so this program will run on a single thread, but there will be multiple instances of this program)?




Aucun commentaire:

Enregistrer un commentaire