Skip to content

Transactions for SQL Server#6972

Merged
janmeier merged 7 commits intosequelize:masterfrom
harshithkashyap:mssql-tran-name-fix
Dec 21, 2016
Merged

Transactions for SQL Server#6972
janmeier merged 7 commits intosequelize:masterfrom
harshithkashyap:mssql-tran-name-fix

Conversation

@harshithkashyap
Copy link
Copy Markdown

@harshithkashyap harshithkashyap commented Dec 10, 2016

Pull Request check-list

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does your issue contain a link to existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Have you added an entry under Future in the changelog?

Description of change

  • Based on @mbroadst suggestion, generateTransactionId method is added which uses crypto.randomBytes and uuid.v4 in case of other dialects. Closes Transaction name too long for MSSQL #3950
  • Enabled tests for transactions for mssql dialect
  • Added SAVE TRANSACTION check to call tedious.saveTransaction method
  • Fix tests which are skipped

@mention-bot
Copy link
Copy Markdown

@harshithkashyap, thanks for your PR! By analyzing the history of the files in this pull request, we identified @BridgeAR, @felixfbecker and @mbroadst to be potential reviewers.

@harshithkashyap
Copy link
Copy Markdown
Author

@felixfbecker @janmeier I've skipped a lot of tests which were timing out. By default, SQL Server blocks even select calls if the row has an exclusive lock. Most of the tests in the codebase fails as transaction is never committed/rolledback.

How do you guys prefer to fix this? I'm checking if the dialect is mssql and using Promise.resolve to skip the select statement in some tests. There are a lot of tests based on this approach as Postgresql and Mysql only blocks writes and allows reads.

@codecov-io
Copy link
Copy Markdown

codecov-io commented Dec 10, 2016

Current coverage is 94.80% (diff: 95.45%)

No coverage report found for master at cc09a3c.

Powered by Codecov. Last update cc09a3c...28fce11

@harshithkashyap harshithkashyap changed the title Trasactions for SQL Server Transactions for SQL Server Dec 10, 2016
@felixfbecker
Copy link
Copy Markdown
Contributor

Well cheating the tests is not an option of course. Maybe just commit the transaction before the SELECT?

@harshithkashyap
Copy link
Copy Markdown
Author

But would that not cause the subsequent then calls to fail as it tries to use a transaction which is already committed?

@harshithkashyap
Copy link
Copy Markdown
Author

@mickhansen @sushantdhiman Suggestions?

@harshithkashyap harshithkashyap force-pushed the mssql-tran-name-fix branch 2 times, most recently from 8317577 to aea2138 Compare December 17, 2016 17:29
…s, transaction savepoints, uses sequelize_test as default database on appveyor
@harshithkashyap
Copy link
Copy Markdown
Author

@felixfbecker @janmeier SQL Server allows READ_COMMITTED_SNAPSHOT option to allow reads during transactions. I've enabled back all the tests and all of the pass in my localhost.
READ_COMMITTED_SNAPSHOT must be turned on before we can use it and it cannot be enabled on master database. I've changed the database in appveyor-setup.ps1 to sequelize_test but i think it has to be manually created on your end. Could you guys please enable it and review this PR?

@felixfbecker
Copy link
Copy Markdown
Contributor

@harshithkashyap What do you mean with "it has to be enabled on our end"? AppVeyor is 100% configured through appveyor.yml and appveyor-setup.ps1

@harshithkashyap
Copy link
Copy Markdown
Author

I've changed the database from master to sequelize_test here. I expected appveyor to create the sequelize_test database automatically with the credentials from appveyor-setup.ps1 file.
Login failed error is thrown in the build.

@felixfbecker
Copy link
Copy Markdown
Contributor

hm no idea, im no mssql expert

@harshithkashyap
Copy link
Copy Markdown
Author

harshithkashyap commented Dec 17, 2016

@felixfbecker I've fixed it using appveyor-setup.ps1 script

@harshithkashyap
Copy link
Copy Markdown
Author

@janmeier @sushantdhiman @felixfbecker Request review!

Copy link
Copy Markdown
Member

@janmeier janmeier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, good work

Out of curiousity, did you figure out why we have to call the beginTransaction et.al. methods, instead of just sending the sql command?

}, this.options.transaction.name);
} else if (_.includes(this.sql, 'SAVE TRANSACTION')) {
connection.saveTransaction(err => {
if (!!err) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unneeded boolean cast

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felixfbecker beginTransaction, rollbackTransaction and commitTransaction also has boolean cast. Should i remove them aswell?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to do so. The desired coding style is defined in eslintrc.json, it's just that the codebase does not fully comply with it yet.

transaction: transaction.parent || transaction
});

options.transaction.name = transaction.parent ? transaction.name: undefined;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space before colon

supportsSearchPath: false
});

options.transaction.name = transaction.parent ? transaction.name: undefined;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space before colon

@harshithkashyap
Copy link
Copy Markdown
Author

@janmeier Without tedious methods, sql server throws

Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.

We use connection.execSql(request); here. When beginTransaction and other methods are called, tedious internally calls connection.execSqlBatch instead of connection.execSql.

Using connection.execSqlBatch allows us to execute sequelize generated queries. All tests seems to pass aswell.

From the tedious docs, it seems that SQL Server will not reuse the execution plan if the same queries are executed again and again. This might add overhead to non transaction queries.

host: process.env.SEQ_MSSQL_HOST || process.env.SEQ_HOST || 'mssql.sequelizejs.com',
port: process.env.SEQ_MSSQL_PORT || process.env.SEQ_PORT || 1433,
dialectOptions: {
instanceName: process.env.MSSQL_INSTANCE ? process.env.MSSQL_INSTANCE : 'SQLEXPRESS',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not needed anymore?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default instance names are unnamed. I cannot connect to my local server even after specifying the server name and port. And it doesn't default to SQLEXPRESS

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, was just curious. I'm not familiar with MSSQL, I just ported the thing over to AppVeyor and copied a lot from tedious PowerShell setup script.

@janmeier janmeier merged commit 086255e into sequelize:master Dec 21, 2016
@janmeier
Copy link
Copy Markdown
Member

Good work @harshithkashyap ! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants