Under normal circumstances only these stmts perform some DB activity:
- BeginTransaction
- ExecuteNonQuery
- Commit

SqliteCommand constructor as well as the following lines (setting parameters
and command text) are memory constructs that prepare data for
ExecuteNonQuery.

If everything works correctly, then it is basically irrelevant whether these
lines are inside or outside transaction block. However, if something goes
wrong you have to think of the destructors. (i.e. Dispose() methods)

Imagine this situation:
- CommandText is a concatenation of several SQL statements.
- Some of these statements (but not the last one) fail with an exception.

Now SqliteCommand.Dispose() attempts to execute part of the CommandText that
was not executed.

In this case the order of C# using statements makes a difference. Your code,
for example, makes sure that the whole action will be terminated by a
rollback.

On the other hand, if you exchanged the order of
SQLiteCommand/SQLiteTransaction constructors, it might well happen that
SqliteCommand.Dispose() will be run after rollback and thus perform unwanted
data change.

The code of System.Data.Sqlite wrapper is pretty complex (in my opinion too
complex) and it is quite possible that more complications are possible.
Placing the transaction at the uppermost level seems to be the safest and
simplest procedure - you do not need to think of implementation details.



--
View this message in context: 
http://sqlite.1065341.n5.nabble.com/Question-about-begin-commit-tp71289p71365.html
Sent from the SQLite mailing list archive at Nabble.com.
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to