会话管理

交易

DML操作可以由关系表和NoSQL集合的事务包装。

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://root@localhost:33060/testSchema') .then(session =>{返回session. starttransaction () .then(() => {const collection = session. getdefaultschema ().getCollection('testCollection');返回集合。修改('name = "foo"') .execute();}) .then(() =>{返回session.commit();}) .catch(err =>{返回session.rollback();});});

保存点

保存点可用于回滚事务的部分。

创建一个保存点

方法可以创建事务保存点Session.setSavepoint ()方法。的承诺在创建保存点时返回,将解析为保存点名称的字符串。如果没有提供保存点的名称,则在表单中生成一个connector-nodejs -{字符串}在哪里{字符串}是32个随机字母数字字符,例如:

连接器nodejs - 097 d39590784a43511e7b89cd9fb1810
Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://root@localhost:33060') .then(session =>{返回session. starttransaction ();.then(() =>{//为保存点返回会话提供一个名称。}) .then(savepoint => {console.log(savepoint);/ / sp});});

注意:定义的标识符是从字面上理解的。空字符串是不允许的,即使它们被服务器允许。

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://root@localhost:33060') .then(session =>{返回session. starttransaction ();.then(() =>{//使用自动生成的保存点返回会话的名称。}) .then(savepoint => {console.log(savepoint);/ /连接器nodejs - 097 d39590784a43511e7b89cd9fb1810});});

回滚到现有的保存点

控件提供的或由连接器生成的保存点名称用于回滚到现有的保存点Session.rollbackTo ()方法。

如果保存点名称无效或保存点不存在,则将拒绝该承诺。

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://root@localhost:33060') .then(session => {const collection = session. getschema ('testSchema').getCollection('testCollection');返回session.startTransaction() .then() =>{返回集合。Add ({_id: '2', name: 'bar'}) .execute();}) .then(() =>{返回session.setSavepoint();}) .then(savepoint =>{返回集合。add({_id: '3', name: 'baz'}) .execute() .then() =>{返回session.rollbackTo(savepoint);});}) .then(() =>{返回session.commit();}) .then(() =>{返回collection.find() .execute();}) .then(res => {console.log(res. fetchall ()); // [{ _id: '2', name: 'bar' }] }); });

释放一个现有的保存点

方法提供的或由连接器生成的保存点名称用于释放到现有的保存点Session.releaseSavepoint ()方法。

如果保存点名称无效或保存点不存在,则将拒绝该承诺。

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://root@localhost:33060') .then(session => {const collection = session. getschema ('testSchema').getCollection('testCollection');返回session.startTransaction() .then(() =>{返回session.setSavepoint('sp');}) .then(() =>{返回集合。Add ({_id: '2', name: 'bar'}) .execute();}) .then(() =>{返回session.releaseSavepoint('sp');});});

试图释放不存在的保存点将导致被拒绝承诺

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://root@localhost:33060') .then(session => {const collection = session. getschema ('testSchema').getCollection('testCollection');返回session.startTransaction() .then() =>{返回集合。Add ({_id: '2', name: 'bar'}) .execute();}) .then(() =>{返回session.releaseSavepoint('sp');});}) .catch(err => {console.log(err.message);// SAVEPOINT sp不存在});

类似地,试图回滚到不存在的保存点将导致被拒绝承诺

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://root@localhost:33060') .then(session => {const collection = session. getschema ('testSchema').getCollection('testCollection');返回session.startTransaction() .then() =>{返回集合。Add ({_id: '2', name: 'bar'}) .execute();}) .then(() =>{返回session.releaseSavepoint('sp');});}) .catch(err => {console.log(err.message);// SAVEPOINT sp不存在});

如果释放了保存点,并且试图回滚到该保存点,则Promise将被拒绝。

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://root@localhost:33060') .then(session => {const collection = session. getschema ('testSchema').getCollection('testCollection');返回session.startTransaction() .then(() =>{返回session.setSavepoint('sp');}) .then(() =>{返回集合。Add ({_id: '2', name: 'bar'}) .execute();}) .then(() =>{返回session.releaseSavepoint('sp');})() = >{返回session.rollbackTo (sp);});}) .catch(err => {console.log(err.message);// SAVEPOINT sp不存在});

在自动提交模式下保存点

保存点的确切行为由服务器定义。这与默认启用的自动提交模式结合在一起可能会导致令人困惑的行为。考虑如下不显式的代码片段开始, startTransaction()或类似的调用:

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://root@localhost:33060') .then(session =>{返回session. setsavepoint ('fun');}) .then(savepoint =>{返回session.releaseSavepoint('fun');}) .catch(err => {console.log(err.message);// SAVEPOINT乐趣不存在});

在自动提交模式下,调用setSavepoint ()将创建一个事务,创建一个保存点,然后提交,这将删除保存点。因此,呼吁releaseSavepoint ()会抛出错误SAVEPOINT“乐趣”并不存在.为了保存点的存续,用户需要首先启动一个显式事务块。

使用DDL语句保存点

打开的事务在执行DDL语句时自动关闭并提交。考虑以下代码:

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://root@localhost:33060') .then(session =>{返回session. starttransaction () .then(() =>{返回session. setsavepoint ('fun');}) .then(savepoint =>{返回会话。sql('CREATE TABLE schema.table') .execute() .then(() =>{返回session.releaseSavepoint(保存点);});});}) .catch(err => {console.log(err.message);// SAVEPOINT乐趣不存在});

由于事务已关闭,保存点也不再存在。