Repository navigation
Description
Issue type:
[x] question
[ ] bug report
[x] feature request
[ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[x] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)
Hello, when making a simple query dirty_read works fine:
getRepository(Car).createQueryBuilder('cars').setLock('dirty_read').getOne()
This would result in:
SELECT cars.allFields FROM CAR cars WITH(NOLOCK)
The problem comes when I have to extend dirty_read to a query with joins. Example:
getRepository(Car).createQueryBuilder('cars').setLock('dirty_read')
.innerJoin('car.tires', 'tires')
.getOne()
This would result in:
SELECT cars.allFields, tires.allFields FROM CAR car WITH(NOLOCK) INNER JOIN TIRE tires ON tires.ID = cars.TIREID
The dirty_read will only apply to the main from so that if there is any open transaction that affects the join table, the query will be waiting for the transaction, the idea is to put the dirty_read in the join.
Is there any way to make dirty_read extend to joins?
If not. Can I implement a function that meets that functionality? I have tried it locally and it works quite well, in the same way, you could use that function to apply more mssql hints (https://docs.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-table?view=sql-server-2017), something like this (it's just an incomplete example, the final code would be more readable):
SelectQueryBuilder.ts
innerJoinHints(entityOrProperty: Function|string|((qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>), alias: string, condition: string = "", parameters?: ObjectLiteral, hints?: string[]): this {
this.join("INNER", entityOrProperty, alias, condition, parameters, undefined, undefined, hints);
return this;
}
const hints = joinAttr.hints ? ` WITH (${joinAttr.hints})` : '';
return " " + joinAttr.direction + " JOIN " + this.getTableName(destinationTableName) + " " + this.escape(destinationTableAlias) + hints + " ON " + this.replacePropertyNames(condition + appendedCondition);
JoinAttribute.ts
hints?: string[];
And call it with:
.innerJoinHints('car.tires', 'tires', null, null, ['NOLOCK'])
the only drawback is that it would only apply to mssql which is the one that uses this nomenclature.
Activity
h16rkim commentedon Jan 5, 2022
I'm facing similar issues.
How about adding
WITH(NOLOCK)option ifexpressionMap.lockMode is "dirty_read"?I didn't tried it locally, but rough idea about this suggestion is below.
fix: add lock clause for MSSQL select, select with join clause
fix: add lock clause for MSSQL select, select with join clause
fix: add lock clause for MSSQL select, select with join clause
fix: add lock clause for MSSQL select, select with join clause
fix: add lock clause for MSSQL select, select with join clause
fix: add lock clause for MSSQL select, select with join clause
fix: add lock clause for MSSQL select with join clause
h16rkim commentedon Jan 6, 2022
I added some lines to fix this issue. I made a pull request like above.
danaryu commentedon Jan 9, 2022
I faced same issue. Any updates ?
feat: add WITH (lock) clause for MSSQL select with join queries (#8507)