Documentation Home
X DevAPI User Guide
Download this Manual
PDF (US Ltr) - 1.0Mb
PDF (A4) - 1.0Mb
EPUB - 0.7Mb
HTML Download (TGZ) - 0.6Mb
HTML Download (Zip) - 0.7Mb


X DevAPI User Guide  /  ...  /  Working with a Session Object

Pre-General Availability Draft: 2017-04-25

2.4.2 Working with a Session Object

All previous examples used the getSchema() or getDefaultSchema() methods of the Session object, which return a Schema object. You use this Schema to access Collections and Tables. Most examples make use of the X DevAPI ability to chain all object constructions, enabling you to get to the schema object in one line. For example:

schema = mysqlx.getSession(...).getSchema();

This object chain is equivalent to the following, with the difference that the intermediate step is omitted:

session = mysqlx.getSession();
schema = session.getSchema().

There is no requirement to always chain calls until you get a Schema object, neither is it always what you want. If you want to work with the Session object, for example, to call the Session object method getSchemas(), there is no need to navigate down to the Schema. For example:

session = mysqlx.getSession(); session.getSchemas().

MySQL Shell JavaScript Code

// Connecting to MySQL and working with a Session
var mysqlx = require('mysqlx');

// Connect to a dedicated MySQL server using a connection URL
var mySession = mysqlx.getNodeSession('mike:s3cr3t!@localhost');

// Get a list of all available schemas
var schemaList = mySession.getSchemas();

print('Available schemas in this session:\n');

// Loop over all available schemas and print their name
for (index in schemaList) {
  print(schemaList[index].name + '\n');
}

mySession.close();
      

MySQL Shell Python Code

# Connecting to MySQL and working with a Session
import mysqlx

# Connect to a dedicated MySQL server using a connection URL
mySession = mysqlx.get_node_session('mike:s3cr3t!@localhost')

# Get a list of all available schemas
schemaList = mySession.get_schemas()

print 'Available schemas in this session:\n'

# Loop over all available schemas and print their name
for schema in schemaList:
        print '%s\n' % schema.name

mySession.close()
      

Node.js JavaScript Code

// Connecting to MySQL and working with a Session
var mysqlx = require('mysqlx');

// Connect to a dedicated MySQL server using a connection URL
mysqlx.getSession('mike:s3cr3t!@localhost').then(function (mySession) {
  // Get a list of all available schemas
  return mySession.getSchemas();
}).then(function (schemaList) {
  console.log('Available schemas in this session:\n');

  // Loop over all available schemas and print their name
  for (schema in schemaList) {
    console.log(schema + '\n');
  }
});

C# Code

// Connect to a dedicated MySQL server node using a connection URL
var mySession = MySQLX.GetSession("server=localhost;port=33060;user=mike;password=s3cr3t!;");

// Get a list of all available schemas
var schemaList = mySession.GetSchemas();

Console.WriteLine("Available schemas in this session:");

// Loop over all available schemas and print their name
foreach (var schema in schemaList)
{
      Console.WriteLine(schema);
}

mySession.Close();

Java Code

import java.util.List;
import com.mysql.cj.api.xdevapi.*;
import com.mysql.cj.xdevapi.*;

// Connecting to MySQL and working with a Session
// Connect to a dedicated MySQL server using a connection URL
XSession mySession = new XSessionFactory().getSession("mysqlx://localhost:33060/test?user=mike&password=s3cr3t!");

// Get a list of all available schemas
List<Schema> schemaList = mySession.getSchemas();

System.out.println("Available schemas in this session:");

// Loop over all available schemas and print their name
for (Schema schema : schemaList) {
System.out.println(schema.getName());
}

mySession.close(); 

C++ Code

#include <mysql_devapi.h>

// Connecting to MySQL and working with a Session

// Connect to a dedicated MySQL server using a connection URL
string url = "mysqlx://localhost:33060/test?user=mike&password=s3cr3t!";
{
  XSession mySession(url);

  // Get a list of all available schemas
  std::list<Schema> schemaList = mySession.getSchemas();

  cout << "Available schemas in this session:" << endl;

  // Loop over all available schemas and print their name
  for (Schema schema : schemaList) {
    cout << schema.getName() << endl;
  }
}

In this example the mysqlx.getSession() function is used to open a Session. Then the Session.getSchemas() function is used to get a list of all available schemas and print them to the console.


User Comments
Sign Up Login You must be logged in to post a comment.