Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I commit a transaction -- in the Stored Procedure or in the calling application code?

I'm using PHP + Oracle and was wondering if there are any recommendations on where to commit my transactions. I call stored procedures to do all my inserts/updates/deletes, and currently am committing at the end of my stored procedures.

I was wondering:

  1. Is there any difference between calling commit/rollback in my stored procedure vs calling oci_commit / oci_rollback in my PHP code based on the success of the stored procedure call.

  2. Which is preferable? Originally I was thinking in the stored procedures themselves, but now I'm wondering, if there's no difference, perhaps it would give me more flexibility to commit in the calling application code since I could call several stored procedures in a single transaction rather than having to write new stored procedures every time I want to mix/match a variety of SQL statements in a single transaction.

Thoughts?

like image 594
aw crud Avatar asked Jan 29 '10 14:01

aw crud


People also ask

Can we use COMMIT in stored procedure?

A COMMIT statement in a stored procedure closes cursors that are not declared WITH HOLD and leaves open those cursors that are declared WITH HOLD. The effect of COMMIT or ROLLBACK on cursors applies to cursors that are declared in the calling application and to cursors that are declared in the stored procedure.

How are transactions implemented in stored procedure?

The behavior of a stored procedure depends on whether it is called as a singleton SQL statement or within a SQL transaction block. When a stored procedure is called as a singleton SQL statement, the changes made by the stored procedure are committed when the procedure exits or rolled back if the procedure aborts.

How do you COMMIT a transaction in SQL Server?

Marks the end of a successful implicit or explicit transaction. If @@TRANCOUNT is 1, COMMIT TRANSACTION makes all data modifications since the start of the transaction a permanent part of the database, frees the transaction's resources, and decrements @@TRANCOUNT to 0.

What is the role of COMMIT () and where is it used?

COMMIT in SQL is a transaction control language that is used to permanently save the changes done in the transaction in tables/databases. The database cannot regain its previous state after its execution of commit.


2 Answers

I definitely agree with option 2, for the reasons you give. Having each stored procedure act as a separate transaction can be too limiting sometimes. Tom Kyte would back option 2 as well: see this AskTom thread for example.

like image 191
Tony Andrews Avatar answered Oct 18 '22 06:10

Tony Andrews


Option 2 is the way to go as a general rule. If you have your commits inside your stored procedures, you are making a decision that no matter who calls me, I know better and should commit any open transactions when I finish my work.

Say you have some process that has multiple steps and needs to either commit them all or roll them all back. If that process happens to call one of your stored procedures that has a commit in it as part of doing its work, then everything up to that point could be committed by that stored procedure.

It is almost always preferable to let the caller decide about what should happen with the transaction. One normal exception is an autonomous transaction inside a procedure for cases that need it specifically.

like image 21
Doug Porter Avatar answered Oct 18 '22 06:10

Doug Porter