How to use the Transaction function from mssql
Find comprehensive JavaScript mssql.Transaction code examples handpicked from public code repositorys.
mssql.Transaction is a class in the mssql library for creating and managing transactions when working with SQL Server databases in Node.js.
GitHub: jsreport/jsreport
69 70 71 72 73 74 75 76 77 78
return opts.transaction.semaphore.execute(() => execute(opts.transaction)) } const transactionManager = { async start () { const transaction = new sql.Transaction(pool) await transaction.begin() transaction.__rolledBack = false
+ 3 other calls in file
GitHub: instana/nodejs
367 368 369 370 371 372 373 374 375 376 377
}); }); }); app.post('/transaction-callback', (req, res) => { const transaction = new sql.Transaction(); transaction.begin(err1 => { if (err1) { log('Failed to begin transaction.', err1); return res.status(500).json(err1);
+ 3 other calls in file
How does mssql.Transaction work?
The mssql.Transaction class is a part of the mssql library, which provides an interface for working with Microsoft SQL Server databases in Node.js. When you want to perform a series of related database operations, you can group them together into a transaction. A transaction is a series of database operations that are executed as a single unit of work. Either all of the operations in the transaction are executed, or none of them are. The mssql.Transaction class provides methods for creating and managing transactions. You can create a new transaction by calling the begin() method of a mssql.Connection object. The begin() method returns a new mssql.Transaction object that represents the transaction. Once you have a mssql.Transaction object, you can execute database operations within the transaction using the request() method of the mssql.Transaction object. The request() method returns a mssql.Request object that you can use to execute SQL statements and retrieve the results. You can commit the transaction by calling the commit() method of the mssql.Transaction object. This will save all of the changes made within the transaction to the database. If there is an error during the transaction, you can roll back the transaction by calling the rollback() method of the mssql.Transaction object. This will undo all of the changes made within the transaction. Overall, the mssql.Transaction class provides a way to group related database operations into a transaction, which allows you to ensure that either all of the operations are executed or none of them are. This can be useful when you need to perform multiple database operations as part of a single unit of work.
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
return; } if (item.type === 'begin') { (Agent.debug || self.debug) && console.log(self.debugname, 'begin transaction'); self.$transaction = new database.Transaction(self.db); self.$transaction.begin(function(err) { if (err) { self.errors.push(err.message); self.command.length = 0;
GitHub: Cognigy/mongotosql
105 106 107 108 109 110 111 112 113 114
process.stdout.write("\n## " + chalk.green("Found " + chalk.yellow(mongoCount.toString()) + " MongoDB document(s) to extract")); process.stdout.write("\n## " + chalk.green("Starting SQL Transaction...")); let x = 0; // document counter let t1 = new Date().getTime(); const transaction = new mssql.Transaction(sql); await transaction.begin(); let r = await new mssql.Request(transaction); if (!date) { await r.query("DELETE FROM " + options.mssql.table + " WHERE 1=1");
+ 20 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
const sql = require("mssql"); async function performTransaction() { try { // create a connection to the database const pool = await sql.connect( "mssql://username:password@localhost/database" ); // start a transaction const transaction = new sql.Transaction(pool); await transaction.begin(); try { // execute some SQL statements within the transaction await transaction .request() .input("productId", sql.Int, 1) .query("UPDATE Products SET Price = Price * 0.9 WHERE Id = @productId"); await transaction .request() .input("customerId", sql.Int, 2) .query("UPDATE Customers SET Discount = 0.05 WHERE Id = @customerId"); // commit the transaction await transaction.commit(); console.log("Transaction committed successfully."); } catch (err) { // if there was an error, roll back the transaction await transaction.rollback(); console.error("Transaction rolled back.", err); } } catch (err) { console.error("Error connecting to the database.", err); } } performTransaction();
In this example, we first create a connection to the database using the sql.connect() method of the mssql library. We then create a new mssql.Transaction object by calling the constructor with the pool object as the argument. We then call the begin() method of the mssql.Transaction object to start the transaction. Within the transaction, we execute two SQL statements using transaction.request().query(). These statements update the price of a product and the discount of a customer in the database. After the SQL statements have been executed, we call the commit() method of the mssql.Transaction object to commit the transaction. If there is an error during the transaction, we catch the error and call the rollback() method of the mssql.Transaction object to roll back the transaction. Overall, this example demonstrates how to use the mssql.Transaction class to create and manage a transaction when working with a SQL Server database in Node.js.
GitHub: Mariede/boiler-server
25 26 27 28 29 30 31 32 33 34
if (__serverConfig.db.msSqlServer.connectionType === 2) { // Conexao simples, direta, sem pool sql.connect(__serverConfig.db.msSqlServer.configDb) .then( pool => { return new sql.Transaction(pool); } ) .then( transaction => {
+ 11 other calls in file
90 91 92 93 94 95 96 97 98 99
return new Promise((resolve, reject) => { p.then(function(pool){ if(pool.error){ resolve(pool); }else{ var transaction = new mssql.Transaction(pool); resolve(transaction); } }) });
+ 3 other calls in file
429 430 431 432 433 434 435 436 437 438
} async function checkDeleteResolvesWithDefaultHeaderTableIsolationOnSelect (expectedLength) { let transaction try { transaction = new sql.Transaction(pool) // using Jest pool await transaction.begin(sql.ISOLATION_LEVEL.READ_COMMITTED) // the isolation level used by other transactions on the three tables concerned const newRequest = new sql.Request(transaction) const query = `
+ 17 other calls in file
GitHub: andrglo/mssql-cr-layer
71 72 73 74 75 76 77 78 79 80
*/ MssqlCrLayer.prototype.transaction = function(fn, options) { options = options || {} const isolationLevel = options.ISOLATION_LEVEL || this.ISOLATION_LEVEL return this.connect().then(function(connection) { const transaction = new mssql.Transaction(connection) let rolledBack = false transaction.on('rollback', function() { rolledBack = true })
+ 3 other calls in file
GitHub: singlesai/hrm_esign
36 37 38 39 40 41 42 43 44 45
async begTran(env) { await this.init(env) if(!this._inTran) { this._inTran = true this._tran = new mssql.Transaction(this._db) await this._tran.begin() this._req = new mssql.Request(this._tran) } }
+ 40 other calls in file
63 64 65 66 67 68 69 70 71 72 73
human.addBenefitPlan = async (benefitPlan) => { try { const { planName, deductable, percentageCoPay } = benefitPlan; const pool = await sql.connect(config); const transaction = new sql.Transaction(pool); await transaction.begin() .then((err) => { if (err) console.log(err, "err begin"); const sqlScript = `INSERT INTO [dbo].[Benefit_Plans] (Plan_Name, Deductable, Percentage_CoPay) VALUES ('${planName}', ${parseInt(deductable)}, ${parseInt(percentageCoPay)})`;
+ 8 other calls in file
GitHub: devinsg/eShop
58 59 60 61 62 63 64 65 66 67 68 69
dbContext.getTransaction = async function() { if(!pool.connected) { await pool.connect(); } const tr = new mssql.Transaction(pool); return tr; } dbContext.getRequest = function(tr) {
mssql.connect is the most popular function in mssql (5681 examples)