If there is a need to add records in two different tables to accomplish one
goal, that can be a matter of using a transaction. If there is a need to add
12 records in one table, that may be better to use ONE insert query (which
will be almost like one atomic operation) but if that cannot be done and
looping need to be done, then, that may also become a good case to make a
transaction between the start and the end of the loop.
Transaction should be short. If there is a step that requires a user
intervention, inside the transaction, that is *probably* not very good (CAN
be too long, the user may have left for lunch) since tables and records may
become locked. With Jet, you don't get much control on what kind of
'isolation' (what other users can see about your transaction, even if they
can delete or append records on tables you just 'took a look at' , in your
transaction) you have, in comparison with MS SQL Server, but note that you
can indeed lock other users out of tables and records you touched: that is
an inconvenient about transaction, for these other users (or your own access
to the data though other means than through your transaction).
The user interface uses a transaction when you insert a bunch of data into a
table. Indeed, when you get the prompt about nnn records won't be added for
this or that reason, the prompt (user intervention ! ) is about accepting
the records that are not in 'error', or to abort the whole insertion thing.
This *is* a transaction that the interface started for you, and asked you
about committing it, or rolling it back! Inserting a bunch of data and
having to validate it is, indeed, a good example for a transaction. If you
have to 'touch' many tables (or the same table many times) and cannot do it
in one single query, that is typical example where a transaction can be
useful. Note that the operation system can also supply its own
transaction-thing when components outside a database may be involved (bank
transfer of money, as example), but that is, here, out of scope, even if it
is interesting to know that such thing is available.
Vanderghast, Access MVP
"Chris O''''Neill" <(E-Mail Removed)> wrote in message
news:5E2616C6-59F9-4DFF-B649-(E-Mail Removed)...
> That was a *very* good explanation, Michel. Thank you for that!
>
> I'm wondering if there are any general "guidelines" for when one should
> use
> (or not use) transactions? Some folks (he said, pointing at himself) can
> get
> carried away and overdo it if they don't have some guidelines to help them
> decide when a transactional methodology is better (or worse) than just
> doing
> it a regular way.
>
> Fot instance, I'm creating a program that includes a subroutine that
> creates
> a fiscal period table (fiscal period number, period start and end dates,
> Yes/No field for whether the period is closed, etc.). The routine takes
> the
> fiscal year start date supplied by the user (e.g. January 1st of any year)
> and does 12 loops to create each row in the table. (Btw, the routine
> accounts for fiscal years that don't start on January 1st and
> automatically
> adjusts for leap years, which I thought was pretty cleaver of myself. (He
> said proudly!))
>
> Anyway, I'm thinking that maybe it might be a "good idea" to use a
> transaction to create the table? That way, if something pukes during the
> process, the table won't end up partially completed. Would *that* be an
> example of an appropriate use for transactions? What are some other
> examples
> of when (or when not) to use transactions?
>
> Thanks, in advance, for your guidance and teaching...
>
> Regards, Chris
>
> "Michel Walsh" wrote:
>
>> With Jet? I would say that I don't see in what the transactions exposed
>> to
>> us could help against corruption. At least, if the corruption comes from
>> a
>> lost of connection, there is not mechanic I am aware of, which time out a
>> started but uncommitted transaction. On the other hand, if you lost the
>> connection after you started a transaction, it is unlikely that you will
>> be
>> able to get back the same connection... to roll it back.... but why will
>> you
>> roll it back if you can then continue to deal with the database and
>> finally
>> commit the transaction?
>>
>>
>> The goal of a transaction is to make a complex operation appears as ONE
>> operation. As example, you have to append data in tableA, then erase it
>> from
>> tableB. But observe that, in theory, the system may fall after you append
>> in
>> tableA but before you erase it from tableB and that could 'invalidate'
>> your
>> data (as if you send 1 M$ to clientA and fail to debit clientB, you will
>> be
>> losing 1M$). So, will you start a transaction, append in tableA, delete
>> in
>> tableB, and commit your transaction. If there is a failure while the
>> transaction is active, nothing outside the transaction will be influenced
>> by
>> it. Note that technically, appending data in tableA and not being able to
>> delete it from tableB is not "corruption" of data, even if it creates a
>> state you don't want. Corruption is more like a wrong data type, an index
>> pointing to the wrong data, a unique constraint or a data referential
>> integrity rule that is not respected, or something like that.
>>
>>
>> Vanderghast, Access MVP
>>
>>
>> "JString" <(E-Mail Removed)> wrote in message
>> news:22DCA45B-F1BC-4115-BD05-(E-Mail Removed)...
>> > Hello.
>> >
>> > I don't understand much about transactions and I would really like to
>> > know
>> > if commit/rollback can help guard against corruption of the back end,
>> > and
>> > if
>> > so what is the general procedure to make this happen.
>> >
>> > Many thanks in advance.
>>
>>
>>