Documentation Home
X DevAPI User Guide
Download this Manual

X DevAPI User Guide  /  ...  /  Processing Warnings

Pre-General Availability Draft: 2017-07-20

8.1.1 Processing Warnings

Similar to the execution of single statements committing or rolling back a transaction can also trigger warnings. To be able to process these warnings the replied result object of Session.commit(); or Session.rollback(); needs to be checked.

This is shown in the following example. The example assumes that the test schema exists and that the collection my_collection does not exist.

MySQL Shell JavaScript Code

var mysqlx = require('mysqlx');

// Connect to server
var mySession = mysqlx.getNodeSession( {
        host: 'localhost', port: 33060,
        dbUser: 'mike', dbPassword: 's3cr3t!' } );

// Get the Schema test
var myDb = mySession.getSchema('test');

// Create a new collection
var myColl = myDb.createCollection('my_collection');

// Start a transaction
mySession.startTransaction();
try
{
    myColl.add({'name': 'Jack', 'age': 15, 'height': 1.76, 'weight': 69.4}).execute();
    myColl.add({'name': 'Susanne', 'age': 24, 'height': 1.65}).execute();
    myColl.add({'name': 'Mike', 'age': 39, 'height': 1.9, 'weight': 74.3}).execute();
    
    // Commit the transaction if everything went well
    var reply = mySession.commit();
    
    // handle warnings
    if (reply.warningCount){
      var warnings = reply.getWarnings();
      for (index in warnings){
        var warning = warnings[index];
        print ('Type ['+ warning.level + '] (Code ' + warning.code + '): ' + warning.message + '\n');
      }
    }
    
    print ('Data inserted successfully.');
}
catch(err)
{
    // Rollback the transaction in case of an error
    reply = mySession.rollback();
    
    // handle warnings
    if (reply.warningCount){
      var warnings = reply.getWarnings();
      for (index in warnings){
        var warning = warnings[index];
        print ('Type ['+ warning.level + '] (Code ' + warning.code + '): ' + warning.message + '\n');
      }
    }
    
    // Printing the error message
    print ('Data could not be inserted: ' + err.message);
}

MySQL Shell Python Code

import mysqlx

# Connect to server
mySession = mysqlx.get_node_session( {
        'host': 'localhost', 'port': 33060,
        'dbUser': 'mike', 'dbPassword': 's3cr3t!' } )

# Get the Schema test
myDb = mySession.get_schema('test')

# Create a new collection
myColl = myDb.create_collection('my_collection')

# Start a transaction
mySession.start_transaction()
try:
    myColl.add({'name': 'Jack', 'age': 15, 'height': 1.76, 'weight': 69.4}).execute()
    myColl.add({'name': 'Susanne', 'age': 24, 'height': 1.65}).execute()
    myColl.add({'name': 'Mike', 'age': 39, 'height': 1.9, 'weight': 74.3}).execute()

    # Commit the transaction if everything went well
    reply = mySession.commit()

    # handle warnings
    if reply.warning_count:
      for warning in result.get_warnings():
        print 'Type [%s] (Code %s): %s\n' % (warning.level, warning.code, warning.message)

    print 'Data inserted successfully.'
except Exception, err:
    # Rollback the transaction in case of an error
    reply = mySession.rollback()

    # handle warnings
    if reply.warning_count:
      for warning in result.get_warnings():
        print 'Type [%s] (Code %s): %s\n' % (warning.level, warning.code, warning.message)

    # Printing the error message
    print 'Data could not be inserted: %s' % str(err)

C# Code

{
  // Connect to server
  var session = MySQLX.GetSession("server=localhost;port=33060;user=mike;password=s3cr3t!;");

  // Get the Schema test
  var db = session.GetSchema("test");

  // Create a new collection
  var myColl = db.CreateCollection("my_collection");

  // Start a transaction
  session.StartTransaction();
  int warningCount = 0;
  try
  {
	var result = myColl.Add(new { name = "Jack", age = 15, height = 1.76, weight = 69.4}).Execute();
	warningCount += result.Warnings.Count;
	result = myColl.Add(new { name = "Susanne", age = 24, height = 1.65}).Execute();
	warningCount += result.Warnings.Count;
	result = myColl.Add(new { name = "Mike", age = 39, height = 1.9, weight = 74.3}).Execute();
	warningCount += result.Warnings.Count;

	// Commit the transaction if everything went well
	session.Commit();
	if(warningCount > 0)
	{
	  // handle warnings
	}

	Console.WriteLine("Data inserted successfully.");
  }
  catch (Exception err)
  {
	// Rollback the transaction in case of an error
	session.Rollback();
	if(warningCount > 0)
	{
	  // handle warnings
	}

	// Printing the error message
	Console.WriteLine("Data could not be inserted: " + err.Message);
  }
}

Java Code

// c.f. "standard transaction handling"

C++ Code

/*
  Connector/C++ does not yet provide access to transaction warnings
  -- Session methods commit() and rollback() do not return a result object.
*/

By default all warnings are sent from the server to the client. If an operation is known to generate many warnings and the warnings are of no value to the application then sending the warnings can be suppressed. This helps to save bandwith. Session.setFetchWarnings() controls whether warnings are discarded at the server or are sent to the client. Session.getFetchWarnings() is used to learn the currently active setting.

MySQL Shell JavaScript Code

var mysqlx = require('mysqlx');

function process_warnings(result){
  if (result.getWarningCount()){
    var warnings = result.getWarnings();
    for (index in warnings){
      var warning = warnings[index];
      print ('Type ['+ warning.Level + '] (Code ' + warning.Code + '): ' + warning.Message + '\n');
    }
  }
  else{
    print ("No warnings were returned.\n");
  }
}

// Connect to server
var mySession = mysqlx.getNodeSession( {
  host: 'localhost', port: 33060,
  dbUser: 'mike', dbPassword: 's3cr3t!' } );

// Disables warning generation
mySession.setFetchWarnings(false);
var result = mySession.sql('drop schema if exists unexisting').execute();
process_warnings(result);

// Enables warning generation
mySession.setFetchWarnings(true);
var result = mySession.sql('drop schema if exists unexisting').execute();
process_warnings(result);

MySQL Shell Python Code

import mysqlx

def process_warnings(result):
  if result.get_warning_count():
    for warning in result.get_warnings():
      print 'Type [%s] (Code %s): %s\n' % (warning.level, warning.code, warning.message)
  else:
    print "No warnings were returned.\n"


# Connect to server
mySession = mysqlx.get_node_session( {
  'host': 'localhost', 'port': 33060,
  'dbUser': 'mike', 'dbPassword': 's3cr3t!' } );

# Disables warning generation
mySession.set_fetch_warnings(False)
result = mySession.sql('drop schema if exists unexisting').execute()
process_warnings(result)

# Enables warning generation
mySession.set_fetch_warnings(True)
result = mySession.sql('drop schema if exists unexisting').execute()
process_warnings(result)

Node.js JavaScript Code

C# Code

{
  // Assumptions: test schema exists, my_collection exists

  XSession mySession = null;
  try
  {
	// Connect to server on localhost
	mySession = MySQLX.GetSession("server=localhost;port=33060;user=mike;password=s3cr3t!;");
  }
  catch (Exception err)
  {
	Console.WriteLine("The database session could not be opened: " + err.Message);
	throw;
  }

  try
  {
	var myDb = mySession.GetSchema("test");

	// Use the collection "my_collection"
	var myColl = myDb.GetCollection("my_collection");

	// Find a document
	var myDoc = myColl.Find("name like :param").Limit(1)
	  .Bind("param", "S%").Execute();

	// Print document
	PrintResult(myDoc.FetchOne());
  }
  catch (Exception err)
  {
	Console.WriteLine("The following error occurred: " + err.Message);
  }
  finally
  {
	// Close the session in any case
	mySession.Close();
  }
}

Java Code

C++ Code

// setFetchWarnings() not yet implemented in Connector/C++

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