SAM pools and multi-threading

A SAM pool enables a single set of credentials to be shared between client side threads to leverage multiple threads of parallelism on the 1010data Insights Platform.

Creating a new Session object for a SAM pool user is similar to creating a single-user Session object.

Session s = new Session(gateway, owner, pw, group, LoginType.POSSESS);

The SAM pool user session takes two different parameters. Instead of the individual user name user, this Session object takes the owner parameter. The owner parameter is the group owner's user name. The group parameter is the group ID. The SAM pool Session constructor takes the following parameter variables:

  • gateway - a 1010data Insights Platform URI
  • group - a group ID
  • owner - the group owner ID
  • password - the group owner password

If your users are using a SAM pool to log in, they will not have their own platform IDs. The users share a single set of credentials to log into the platform. A SAM pool has a number of IDs to use, but it is not limitless. You may have to account for cases where peak utilization creates a situation where more threads are trying to access the platform than your SAM pool is configured to handle.

The Java SDK is designed to queue during Session object construction when a SAM pool is fully utilized. During this queuing time the thread trying to construct a new Session object retries the request every ten seconds.

To ensure that your SAM pool is fully utilized, release sessions as soon as your thread is done prevents a full pool of IDs where all of the threads of database concurrency are in use at the same time. For more information, see SAM pools and multi-threading.

In this example, two threads construct two separate Session objects and log in with a SAM pool. The application retrieves data from two different tables and downloads them into two separate files.

import java.io.IOException;
import java.io.Writer;
import java.io.FileWriter;
import java.lang.Thread;
import com.tentendata.javasdk1010v2.*;

public class SimpleThread extends Thread {
    public String table = null;
    public String ops = null;
    private String URL, owner, password, group;
    private Writer output;

    public SimpleThread(String URL, String owner, String password,
                        String group, String table, String ops,
                        Writer output) {
        this.URL = URL;
        this.owner = owner;
        this.group = group;
        this.password = password;
        this.table = table;
        this.ops = ops;
        this.output = output;
    }

    public void run() {
        ResultSet results;
        Query query;
        Session session = null;
        try {
            session = new Session(URL, owner, password, group);
            System.out.println("Session started, username: " +
                               session.getUsername());
            query = new Query(session, table, ops);
            results = query.run();
            System.out.println("Ran query on " + table + ", returned " +
                               results.numRows() + " rows.");
            for (long i = 0; i < results.numRows(); i++) {
                Row row = results.row(i);
                Datum[] data = row.asArray();
                for (int j = 0; j < data.length; j++) {
                    if (j > 0) {
                        output.write('\t');
                    }
                    output.write(data[j].toString());
                }
                output.write('\n');
            }
            output.close();
        }
        catch (IOException e) {
            System.err.println("Error writing: " + e);
        }
        finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        String URL = "https://www2.1010data.com/cgi-bin/gw";
        String path1 = "pub.public_data.census.acs.all_acs_processed_tables";
        String path2 = "pub.public_data.census.acs.reference.column_names";
        String ops = "<sel value=\"(i_<50000)\"/>";

        if (args.length < 3) {
            System.out.println("Usage: SimpleThread OWNER PASSWORD GROUP");
            System.exit(1);
        }
        String owner = args[0];
        String password = args[1];
        String group = args[2];

        Thread thread1 = new SimpleThread(URL, owner, password, group,
                                          path1, ops,
                                          new FileWriter("Output1.txt"));
        Thread thread2 = new SimpleThread(URL, owner, password, group,
                                          path2, ops,
                                          new FileWriter("Output2.txt"));
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
    }
}