Concurrency and Transaction
See original GitHub issueimport { Table } from "../Table";
import { SqliteConnection } from "../connections/SqliteConnection";
import { BetterSqlite3QueryRunner } from "../queryRunners/BetterSqlite3QueryRunner";
import * as betterSqlite3 from 'better-sqlite3'
class DBConection extends SqliteConnection<'DBConnection'> {
}
const tCompany = new class TCompany extends Table<DBConection, 'TCompany'> {
id = this.autogeneratedPrimaryKey('id', 'int');
name = this.column('name', 'string');
constructor() {
super('company'); // table name in the database
}
}()
const db = betterSqlite3(':memory:')
const global_conn = new DBConection(new BetterSqlite3QueryRunner(db))
koaApp.use(async (ctx, next) => {
// await global_conn.beginTransaction(); // I can not write code like this, it may cause concurrency bug. I must write like:
const scope_conn = new DBConection(new BetterSqlite3QueryRunner(db));
scope_conn.beginTransaction();
xxxx;
});
Why not:
const global_conn = new DBConection(new BetterSqlite3QueryRunner(db))
koaApp.use(async (ctx, next) => {
const t = await global_conn.beginTransaction(); // t is of interface DBConection
t.selectFrom();
t.commit();
t.rollback();
});
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Concurrency Control in DBMS - GeeksforGeeks
In general, concurrency means, that more than one transaction can work on a system. The advantages of a concurrent system are: Waiting Time:...
Read more >Transaction and Concurrency Control - W3schools
It is the method of managing concurrent operations on the database without getting any obstruction with one another. The Need for Concurrency Control....
Read more >Transactions and Concurrency - ADO.NET - Microsoft Learn
A transaction consists of a single command or a group of commands that execute as a package. Transactions allow you to combine multiple ......
Read more >Transaction Management - Concurrency — CSCI 4380 ...
To study concurrency, we will abstract how we view transactions. · The only operations that matter in a transaction are the data items...
Read more >DBMS Concurrency Control - Javatpoint
The problem occurs when two different database transactions perform the read/write operations on the same database items in an interleaved manner (i.e., ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Let me explain my synchronous-promise proposal for use with better-sqlite3
I think the best approach for you regarding better-sqlite3 is to have the possibility to use synchronous-promise; I’m going to add this feature for you. This will allows you to use all the advantages offered by ts-sql-query.
The approach of have a
executeOnDB
function adapted for synchronous calls will work; all the code executed inside the executeOnDB will be executed synchronously even when you program it async. The important part is you cannot use the async/await syntax. You will need to use a complementarysync
function that transforms the promises in sync code. For example, your code can look like this:Or even simpler if you perform only one operation (the sync call will be made automatically):
The required code can be something like this:
Let me know if the synchronous-promise will work for you.
Synchronous queries implemented in ts-sql-query 1.1.0. See the Synchronous query runners section in the documentation and the example.