Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
ae095732
Commit
ae095732
authored
10 years ago
by
Usman Khalid
Browse files
Options
Downloads
Patches
Plain Diff
commit_on_success_with_read_committed should only work with atmost
1 level of transactions. TNL-367
parent
25a90301
Loading
Loading
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
common/djangoapps/util/db.py
+8
-1
8 additions, 1 deletion
common/djangoapps/util/db.py
common/djangoapps/util/tests/test_db.py
+21
-1
21 additions, 1 deletion
common/djangoapps/util/tests/test_db.py
with
29 additions
and
2 deletions
common/djangoapps/util/db.py
+
8
−
1
View file @
ae095732
...
...
@@ -13,6 +13,8 @@ def commit_on_success_with_read_committed(func): # pylint: disable=invalid-name
If the function returns a response the transaction is committed and if the function raises an exception the
transaction is rolled back.
Raises TransactionManagementError if there are already more than 1 level of transactions open.
Note: This only works on MySQL.
"""
@wraps
(
func
)
...
...
@@ -21,7 +23,12 @@ def commit_on_success_with_read_committed(func): # pylint: disable=invalid-name
if
connection
.
vendor
==
'
mysql
'
:
# The isolation level cannot be changed while a transaction is in progress. So we close any existing one.
if
connection
.
transaction_state
:
connection
.
commit
()
if
len
(
connection
.
transaction_state
)
==
1
:
connection
.
commit
()
# We can commit all open transactions. But it does not seem like a good idea.
elif
len
(
connection
.
transaction_state
)
>
1
:
raise
transaction
.
TransactionManagementError
(
'
Cannot change isolation level.
'
'
More than 1 level of nested transactions.
'
)
# This will set the transaction isolation level to READ COMMITTED for the next transaction.
cursor
=
connection
.
cursor
()
...
...
This diff is collapsed.
Click to expand it.
common/djangoapps/util/tests/test_db.py
+
21
−
1
View file @
ae095732
...
...
@@ -7,7 +7,7 @@ import unittest
from
django.contrib.auth.models
import
User
from
django.db
import
connection
,
IntegrityError
from
django.db.transaction
import
commit_on_success
from
django.db.transaction
import
commit_on_success
,
TransactionManagementError
from
django.test
import
TransactionTestCase
from
util.db
import
commit_on_success_with_read_committed
...
...
@@ -79,3 +79,23 @@ class TransactionIsolationLevelsTestCase(TransactionTestCase):
self
.
assertIsNone
(
thread2
.
status
.
get
(
'
exception
'
))
self
.
assertEqual
(
thread2
.
status
.
get
(
'
created
'
),
created_in_2
)
def
test_transaction_nesting
(
self
):
"""
Test that the decorator raises an error if there are already more than 1 levels of nested transactions.
"""
if
connection
.
vendor
!=
'
mysql
'
:
raise
unittest
.
SkipTest
(
'
Only works on MySQL.
'
)
def
do_nothing
():
"""
Just return.
"""
return
commit_on_success_with_read_committed
(
do_nothing
)()
with
commit_on_success
():
commit_on_success_with_read_committed
(
do_nothing
)()
with
self
.
assertRaises
(
TransactionManagementError
):
with
commit_on_success
():
with
commit_on_success
():
commit_on_success_with_read_committed
(
do_nothing
)()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment