Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
0eec9009
Commit
0eec9009
authored
Nov 14, 2013
by
Steve Müller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add connection auto-commit configuration documentation
parent
d2a1d2a0
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
96 additions
and
5 deletions
+96
-5
transactions.rst
docs/en/reference/transactions.rst
+96
-5
No files found.
docs/en/reference/transactions.rst
View file @
0eec9009
...
@@ -143,5 +143,96 @@ wider scope and the control is handed to the outer scope.
...
@@ -143,5 +143,96 @@ wider scope and the control is handed to the outer scope.
nesting level, causing errors with broken transaction boundaries
nesting level, causing errors with broken transaction boundaries
that may be hard to debug.
that may be hard to debug.
Auto-commit mode
----------------
A ``Doctrine\DBAL\Connection`` supports setting the auto-commit mode
to control whether queries should be automatically wrapped into a
transaction or directly be committed to the database.
By default a connection runs in auto-commit mode which means
that it is non-transactional unless you start a transaction explicitly
via ``beginTransaction()``. To have a connection automatically open up
a new transaction on ``connect()`` and after ``commit()`` or ``rollback()``,
you can disable auto-commit mode with ``setAutoCommit(false)``.
::
<?php
// define connection parameters $params and initialize driver $driver
$conn = new \Doctrine\DBAL\Connection($params, $driver);
$conn->setAutoCommit(false); // disables auto-commit
$conn->connect(); // connects and immediately starts a new transaction
try {
// do stuff
$conn->commit(); // commits transaction and immediately starts a new one
} catch (\Exception $e) {
$conn->rollback(); // rolls back transaction and immediately starts a new one
}
// still transactional
.. note::
Changing auto-commit mode during an active transaction, implicitly
commits active transactions for that particular connection.
::
<?php
// define connection parameters $params and initialize driver $driver
$conn = new \Doctrine\DBAL\Connection($params, $driver);
// we are in auto-commit mode
$conn->beginTransaction();
// disable auto-commit, commits currently active transaction
$conn->setAutoCommit(false); // also causes a new transaction to be started
// no-op as auto-commit is already disabled
$conn->setAutoCommit(false);
// enable auto-commit again, commits currently active transaction
$conn->setAutoCommit(true); // does not start a new transaction automatically
Committing or rolling back an active transaction will of course only
open up a new transaction automatically if the particular action causes
the transaction context of a connection to terminate.
That means committing or rolling back nested transactions are not affected
by this behaviour.
::
<?php
// we are not in auto-commit mode, transaction is active
try {
// do stuff
$conn->beginTransaction(); // start inner transaction, nesting level 2
try {
// do stuff
$conn->commit(); // commits inner transaction, does not start a new one
} catch (\Exception $e) {
$conn->rollback(); // rolls back inner transaction, does not start a new one
}
// do stuff
$conn->commit(); // commits outer transaction, and immediately starts a new one
} catch (\Exception $e) {
$conn->rollback(); // rolls back outer transaction, and immediately starts a new one
}
To initialize a ``Doctrine\DBAL\Connection`` with auto-commit disabled,
you can also use the ``Doctrine\DBAL\Configuration`` container to modfiy the
default auto-commit mode via ``Doctrine\DBAL\Configuration::setAutoCommit(false)``
and pass it to a ``Doctrine\DBAL\Connection`` when instantiating.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment