PDF (US Ltr)
- 1.2Mb
PDF (A4)
- 1.2Mb
Transactions can be used to group operations into an atomic unit. Either all operations of a transaction succeed when they are committed, or none. It is possible to roll back a transaction as long as it has not been committed.
Transactions can be started in a session using the
startTransaction()
method, committed with
commitTransaction()
and cancelled or rolled
back with rollbackTransaction()
. This is
illustrated in the following example. The example assumes that the
test
schema exists and that the collection
my_collection
does not exist.
Press CTRL+C to copyvar mysqlx = require('mysqlx'); // Connect to server var session = mysqlx.getSession( { host: 'localhost', port: 33060, user: 'user', password: 'password' } ); // Get the Schema test var db = session.getSchema('test'); // Create a new collection var myColl = db.createCollection('my_collection'); // Start a transaction session.startTransaction(); try { myColl.add({name: 'Rohit', age: 18, height: 1.76}).execute(); myColl.add({name: 'Misaki', age: 24, height: 1.65}).execute(); myColl.add({name: 'Leon', age: 39, height: 1.9}).execute(); // Commit the transaction if everything went well session.commit(); print('Data inserted successfully.'); } catch (err) { // Rollback the transaction in case of an error session.rollback(); // Printing the error message print('Data could not be inserted: ' + err.message); }