Questions tagged [transaction]

A mechanism for committing a consistent set of changes into a database atomically.

Transactions are the primary unit of concurrent work on a database, allowing a set of changes to be committed into the database atomically. Most database systems guarantee a set of invariants within a transaction called ACID properties.

Atomicity guarantees that all changes in a transaction guarantees that all changes in the transaction will be committed or rolled back - a transaction is never partially applied.

Consistency guarantees that the transaction cannot leave data in an invalid state, including validity checks applied by database constriants.

Isolation guarantees that a transaction cannot interfere with data being used by another transaction. In practice, this constraint is often relaxed to trade consistency for performance by setting the isolation level of the transaction.

Durability guarantees that if a transaction is reported to the client as being committed that the database manager can guarantee that transaction has been committed and the changes stored permanently.

Within a simple transaction the semantics implied by ACID are fairly straightforward, but they can get more complex with more complex transaction semantics.

Distributed transactions guarantee ACID properties across multiple database systems or other resource managers for a transaction through a protocol known as two-phase commit. In this type of protocol each resource manager1 is asked to guarantee the ability to commit and then subsequently asked to actually commit the transaction. The transaction is not reported as committed until all resource managers have reported successful commits.

Nested or chained transactions allow parts of a transaction to be committed, but then rolled back if an error is encountered at a higher level. Rollback at any level aborts the whole transaction. This mechanism is useful for ensuring consistency across a complex transaction involving multiple operations.

Autonomous transactions allow certain updates to break off from the parent transaction context and commit autonomously. This is useful for (for example) ensuring that loggng information persists when an error causes the rollback of a parent transaction. Not all DBMS platforms support autonomous transactions.

The definitive book on transaction processing systems architecture is Grey and Reuters Transaction Processing: Concepts and Techniques (ISBN 978-1558601901).

1In transaction processing terminology a resource manager is something like a database management system that participates in a distributed transaction.

665 questions
114
votes
3 answers

Is it a bad practice to always create a transaction?

Is it a bad practice to always create a transaction? For example, it is a good practice to create a transaction for nothing but one simple SELECT? What is the cost of creating a transaction when it is not really necessary? Even if you are using an…
elranu
  • 1,243
  • 2
  • 9
  • 7
80
votes
5 answers

What risks are there if we enable read committed snapshot in sql-server?

I have read here that some extra data will be stored per row so we might see a performance degradation but what other risks are there? eg. Will this affect recovery of the database? Is there anything else we need to do to take advantage of this? I…
Adam Butler
  • 1,511
  • 4
  • 16
  • 16
45
votes
9 answers

Asked to Not Use Transactions and to Use A Workaround to Simulate One

I've been developing T-SQL for several years and am always digging in further, continuing to learn all I can about all aspects of the language. I recently started working at a new company and have received what I think is an odd suggestion regarding…
Forrest
  • 391
  • 3
  • 5
32
votes
2 answers

Can I select data inserted in the same uncommited transaction?

Maybe this is a dumb beginner question, but I cannot find an answer anywhere. Everywhere I read about the Transaction Isolation which solves the visibility of data within the concurrent transactions. My concern is the behavior within a single…
NumberFour
  • 507
  • 1
  • 7
  • 10
29
votes
4 answers

How to find out who deleted some data SQL Server

My boss had a query from a customer yesterday asking how they could find out who deleted some data in their SQL Server database (it is the express edition if that matters). I thought this could be found from the transaction log (providing it hadn't…
Matt Wilko
  • 392
  • 1
  • 3
  • 7
28
votes
3 answers

What is the "Chaos" Isolation level and when should it be used?

ADO.NET documentation shows the possibility of setting the transaction level for a SQL transaction to Chaos. It sounds unpleasant, but if the feature is there, presumably it has some legitimate use. The SET TRANSACTION ISOLATION LEVEL command in BOL…
MatthewMartin
  • 382
  • 1
  • 3
  • 8
28
votes
2 answers

How to rollback when 3 stored procedures are started from one stored procedure

I have a stored procedure that only executes 3 stored procedures inside them. I am only using 1 parameter to store if the master SP is successful. If the first stored procedure works fine in the master stored procedure, but the 2nd stored procedure…
user2483342
  • 421
  • 1
  • 5
  • 4
28
votes
1 answer

Trigger in combination with transaction

Suppose we have the following situation: We have a table (let's say Table_A), wich has a trigger on INSERT. The trigger job is to update some rows in table_B based on the inserted values in table_A. Now, everything is ok when we simple insert a row…
veljasije
  • 765
  • 2
  • 10
  • 19
25
votes
2 answers

Transactional DDL workflow for MySQL

I was a little surprised to discover that DDL statements (alter table, create index etc) implicitly commit the current transaction in MySQL. Coming from MS SQL Server, the ability to do database alterations in a transaction locally (that was then…
sennett
  • 350
  • 3
  • 7
25
votes
7 answers

Oracle - Any way to view uncommited changes to a particular table?

I'm debugging through a batch process currently that does a lot of DML statements, but doesn't do a commit right away. It would be nice to be able to view the "pending" changes from another session while the transaction is not committed. Is this…
contactmatt
  • 411
  • 1
  • 5
  • 9
24
votes
1 answer

MySQL transaction size - how big is too big?

I have an import process that runs every so often and I want it to be an 'all or nothing' kind of deal, aka: a transaction. There are many aspects, and the imports may yield anywhere between 100k-1mil+ records. This equates to a payload ranging from…
thinice
  • 363
  • 1
  • 2
  • 7
22
votes
4 answers

Transactions within a Transaction

What behaviour would PostgreSQL display if for example the script below were called BEGIN; SELECT * FROM foo; INSERT INTO foo(name) VALUES ('bar'); BEGIN; <- The point of interest END; Would PostgreSQL discard the second BEGIN or would a commit be…
Alex
  • 325
  • 1
  • 2
  • 5
22
votes
1 answer

How to use transactions with SQL Server DDL?

I have a login table into which all inserts are done by a single stored procedure. CREATE TABLE dbo.LogTable( LogRefnr int IDENTITY(1, 1) NOT NULL, LogQuery varchar(255) NOT NULL, LogTime datetime NOT NULL, logQueryDuration int…
bernd_k
  • 11,741
  • 23
  • 73
  • 108
21
votes
4 answers

Can I change table structure in a transaction and then roll it back if there is an error?

I have some ALTER TABLE statements that I am running. Not all of them work (they are the result of running SQL Data Compare) and I want to group them in some transactions and roll back the statements if something goes wrong. Is this possible, or is…
Piers Karsenbarg
  • 919
  • 2
  • 10
  • 23
20
votes
5 answers

Is ROLLBACK a fast operation?

Is it true that RDBMS systems are optimized for COMMIT operations? How much slower/faster are ROLLBACK operations and why?
garik
  • 6,492
  • 10
  • 41
  • 56
1
2 3
44 45