Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DDC-2310] [DDC-2675] Fix SQL generation on table lock hint capable platforms #910

Merged
merged 1 commit into from Jan 31, 2014

Conversation

deeky666
Copy link
Member

SQL Server and SQL Anywhere use table lock hints for locking. ORM generates wrong SQL when used in conjunction with joins, in a table inheritance scenario or in a subquery. It places the table lock hints after the JOIN clauses, which is wrong. The table lock hints have to be placed after each table alias in the FROM clause.

Example (current)

SELECT t0.id FROM users t0 INNER JOIN profiles t1 ON t0.id = t1.user_id WITH (NOLOCK)

Example (expected)

SELECT t0.id FROM users t0 WITH (NOLOCK) INNER JOIN profiles t1 ON t0.id = t1.user_id

The testsuites fail big time at the moment because of that and I suppose ORM is more or less unusable at the moment for those platforms without this patch.

I needed to adjust a lot of tests which compared compiled DQL, to make use of the platforms specific lock hint clauses.
Also I did not add new tests because there are already so many tests, where this fails without this patch that this is not necessary IMO.

@doctrinebot
Copy link

Hello,

thank you for creating this pull request. I have automatically opened an issue
on our Jira Bug Tracker for you. See the issue link:

http://www.doctrine-project.org/jira/browse/DDC-2914

We use Jira to track the state of pull requests and the versions they got
included in.

@billschaller
Copy link
Member

Thank you, from the bottom of my heart. I was just checking back on this today because it came up again in development. I had just forked DBAL and commented out the line that appends the lock hint.

@deeky666
Copy link
Member Author

Just for reference: This issue was partly introduced in doctrine/dbal#257 but that PR was not the root cause of the problem because the proper SQL generation was broken ever since for this.
@zeroedin-bill Sorry for the delay on this issue, I have been pointed into this direction yesterday for the first time :(

@billschaller
Copy link
Member

@deeky666 no worries, it was easy enough to work around in the interim :)

@guilhermeblanco
Copy link
Member

@deeky666 seems perfect to me! Once travis says yay, I'll merge it. =)

@deeky666
Copy link
Member Author

@guilhermeblanco IMO this should be backported to the 2.3 and 2.4 branches also because it breaks nearly all queries ;)
Also I'm not even entirely sure whether the current DBAL implementation of SQLServerPlatform::appendLockHint() is correct. It seems ORM passes null to that method if no query hint is set and the method evaluates this to WITH (NOLOCK). I wonder if we should do a strict comparison for 0 (no lock) there and do not add a hint if null is given. What do you think?

@billschaller
Copy link
Member

@deeky666 That's a good point -- SQL server default is transaction level READ COMMITTED, but using WITH (NOLOCK) is the equivalent of using SET TRANSACTION LEVEL READ UNCOMMITTED. This should really not append a lock hint unless explicitly requested.

@deeky666
Copy link
Member Author

@zeroedin-bill This is a good argument. Okay then I will change that in DBAL, too.
@guilhermeblanco Please wait with merge until the DBAL fix is ready because I will have to change some test here again afterwards.

@deeky666
Copy link
Member Author

@zeroedin-bill @guilhermeblanco DBAL patch supplied in doctrine/dbal#508.

@@ -269,7 +269,7 @@ public function loadOneToManyCollection(array $assoc, $sourceEntity, PersistentC
* Locks all rows of this entity matching the given criteria with the specified pessimistic lock mode.
*
* @param array $criteria
* @param int $lockMode
* @param int $lockMode One of the Doctrine\DBAL\LockMode::* constants.
Copy link
Member

Choose a reason for hiding this comment

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

int|null I suppose

Copy link
Member Author

Choose a reason for hiding this comment

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

Not yet ;) This will be changed in the PR for DDC-2919.

@deeky666
Copy link
Member Author

@Ocramius It's ready now from my perspective. Your remaining remarks will be implemented in the follow-up PR :)

@Ocramius
Copy link
Member

👍

@deeky666
Copy link
Member Author

@beberlei @guilhermeblanco Is this patch okay for you? As soon as you are ok and merge I can continue on the followup DDC-2919 for further SQL Server and SQL Anywhere fixes :)

beberlei added a commit that referenced this pull request Jan 31, 2014
[DDC-2310] [DDC-2675] Fix SQL generation on table lock hint capable platforms
@beberlei beberlei merged commit 310afdf into doctrine:master Jan 31, 2014
@flip111
Copy link
Contributor

flip111 commented Jan 31, 2014

@zeroedin-bill good advice about TRANSACTION LEVEL 👍

@billschaller
Copy link
Member

Very glad to see this getting merged!

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.

None yet

7 participants