views:

772

answers:

5

Consider a regular web application doing mostly form-based CRUD operations over SQL database. Should there be explicit transaction management in such web application? Or should it simply use autocommit mode? And if doing transactions, is "transaction per request" sufficient?

A: 

You should use transactions given that different users will be hitting the database at the same time. I would recommend you do not use autocommit. Use explicit transaction brackets. As to the resolution of each transaction, you should bracket a particular unit of work (whatever that means in your context).

You might also want to look into the different transaction isolation levels that your SQL database supports. They will offer a range of behaviours in terms of what reading users see of partially updated records.

Andrew
Could you please provide any reasons why one should use transactions and do not use autocommit? And there is no "unit of work" besides handling POST HTTP request, validation data and storing them into database via single INSERT or UPDATE.
Maxim
If it is TRULY a single INSERT or UPDATE then you are using implicit transactions. But, using explicit transactions is a good habit to get into (for reasons above) as you don't know how the product will evolve in the future.
Guy
+1  A: 

It depends on how is the CRUD handling done, if and only if all creations and modifications of model instances is made in a single update or insert query, you can use autocommit.

If you are dealing with CRUD in multiple queries mode (a bad idea, IMO) then you certainly should define transactions explicitly, as these queries would certainly be 'transactionally related', you won't want to end with a half model in your database. This is relevant because some web frameworks tend to do things the 'multiple query' way for various reasons.

As for which transaction mode to use it depends on what you can support in terms of data views (ie, how current the data needs to be when seen by clients) and what you'll have to support in terms of performance.

Vinko Vrsalovic
What do you mean by "done in a single query"? On SQL Server in autocommit mode, each statement is treated individually.See:http://msdn.microsoft.com/en-us/library/ms187878.aspx
Pittsburgh DBA
I'm referring to the application. By "done in a single query" I mean the model creation/modification are made in a single update/insert query, as opposed to update each part of the model in a separate query, as some frameworks end up doing.
Vinko Vrsalovic
Edited to clarify
Vinko Vrsalovic
This position implies that auto-commit mode is ok so long as the app sends a batch rather than single statements. That is incorrect. In auto-commit mode, each statement in the batch is executed individually. In a batch of {StatementA}; {StatementB}; B could fail but A was already committed.
Pittsburgh DBA
+8  A: 

I would only use explicit transactions when you're doing things that are actually transactional, e.g., issuing several SQL commands that are highly interrelated. I guess the classic example of this is a banking application -- withdrawing money from one account and depositing it in another account must always succeeed or fail as a batch, otherwise someone gets ripped off!

We use transactions on SO, but only sparingly. Most of our database updates are standalone and atomic. Very few have the properties of the banking example above.

Jeff Atwood
+3  A: 

I strongly recommend using transaction mode to safe data integrity because autocommit mode can cause partial data saving.

Ringoman
A: 

This is usually handled for me at the database interface layer - The web application rarely calls multiple stored procedures within a transaction. It usually calls a single stored procedure which manages the entire transaction, so the web application only needs to worry about whether it fails.

Usually the web application is not allowed access to other things (tables, views, internal stored procedures) which could allow the database to be in an invalid state if they were attempted without being wrapped in a transaction initiated at the connection level by the client prior to their calls.

There are exceptions to this where a transaction is initiated by the web application, but they are generally few and far between.

Cade Roux