Documentation Home
X DevAPI User Guide
Download this Manual
PDF (US Ltr) - 1.4Mb
PDF (A4) - 1.4Mb


X DevAPI User Guide  /  Connection and Session Concepts  /  Working with a Session Object

2.3 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 object 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().

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.

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 URI
var mySession = mysqlx.getSession('user:password@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
from mysqlsh import mysqlx

# Connect to a dedicated MySQL server using a connection URI
mySession = mysqlx.get_session('user:password@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('@mysql/xdevapi');

// Connect to a dedicated MySQL server using a connection URI
mysqlx
  .getSession('user:password@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
    schemaList.forEach(function (schema) {
      console.log(schema.getName() + '\n');
    });
  });

C# Code

// Connect to a dedicated MySQL server node using a connection URI
var mySession = MySQLX.GetSession("mysqlx://user:password@localhost:33060");

// 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.Name);
}

mySession.Close();

Python Code

# Connector/Python
# Connecting to MySQL and working with a Session
from mysqlsh import mysqlx

# Connect to a dedicated MySQL server using a connection URI
mySession = mysqlx.get_session('user:password@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()

Java Code

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

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

// 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 <mysqlx/xdevapi.h>

// Connecting to MySQL and working with a Session

// Connect to a dedicated MySQL server using a connection URI
string url = "mysqlx://localhost:33060/test?user=user&password=password";
{
  Session 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;
  }
}