osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Authentication with Java driver


Hi all,

I want to know how to authenticate Cassandra users for multiple instances with Java driver.
For instance, each thread creates a instance to access Cassandra with authentication.

As the implementation example, only the first constructor builds a cluster and a session.
Other constructors use them.
This example is implemented according to the datastax document: "Basically you will want to share the same cluster and session instances across your application".

However, other constructors don't authenticate the user and the password.
That's because they don't need to build a cluster and a session.

So, should I create a cluster and a session per instance for the authentication?
If yes, can I create a lot of instances(clusters and sessions) to access C* concurrently?

== example ==
public class A {
  private static Cluster cluster = null;
  private static Map<String, Session> sessions = null;
  private Session session;

  public A (String keyspace, String user, String password) {
    if (cluster == null) {
        builder = Cluster.builder();
        ...
        builder = builder.withCredentials(user, password);
        cluster = builder.build();
    }
    session = sessions.get(keyspace);
    if (session == null) {
        session = cluster.connection(keyspace);
        sessions.put(keyspace, session)
    }
    ...
  }
  ...
  public ResultSet update(...) {
  ...
  public ResultSet get(...) {
  ...
}
== example ==

Thanks,
Yuji