Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Re: Delphi + Stored Procedure + Locks

Question posted by: Serge Rielau (Guest) on June 30th, 2008 11:55 AM
John Hopfield wrote:
Quote:
Originally Posted by
Maybe the transaction is already active?
The CALL of my Stored Procedure should terminate with a COMMIT? (i
never put "COMMIT" at end of my Stored Procedure)

If you were Pinocchio I'd say: "Listen to the grasshopper".
Locks have be held until the transaction commits.
Ideally your application should do this. But you add the COMMIT in your
procedure as well if it encapsulates a complete transaction.
In addition (not as a substitute) you also want to look at setting
DB2_SKIP_INSERTED. Generally a good feature.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
John Hopfield's Avatar
John Hopfield
Guest
n/a Posts
June 30th, 2008
01:15 PM
#2

Re: Re: Delphi + Stored Procedure + Locks
On 30 Giu, 13:47, Serge Rielau <srie...@ca.ibm.comwrote:
Quote:
Originally Posted by
If you were Pinocchio I'd say: "Listen to the grasshopper".


:) (i live 100Km from Collodi :) )
Quote:
Originally Posted by
Locks have be held until the transaction commits.
Ideally your application should do this. But you add the COMMIT in your
procedure as well if it encapsulates a complete transaction.


I have tried to add a "COMMIT" at the end of my Stored Procedure.
But nothing is changed.
Quote:
Originally Posted by
In addition (not as a substitute) you also want to look at setting
DB2_SKIP_INSERTED. Generally a good feature.


i will search the documentation of DB2_SKIP_INSERTED..

JH

John Hopfield's Avatar
John Hopfield
Guest
n/a Posts
July 1st, 2008
01:25 PM
#3

Re: Re: Delphi + Stored Procedure + Locks
On 30 Giu, 15:14, John Hopfield <Hopfi...@freemail.itwrote:
Quote:
Originally Posted by
I have tried to add a "COMMIT" at the end of my Stored Procedure.
But nothing is changed.


Must i disconnect from DB2 and re-connect every-time?
(or close and open SQL querys ?)
JH

Serge Rielau's Avatar
Serge Rielau
Guest
n/a Posts
July 2nd, 2008
03:15 AM
#4

Re: Re: Delphi + Stored Procedure + Locks
John Hopfield wrote:
Quote:
Originally Posted by
On 30 Giu, 15:14, John Hopfield <Hopfi...@freemail.itwrote:
Quote:
Originally Posted by
>I have tried to add a "COMMIT" at the end of my Stored Procedure.
>But nothing is changed.

>
Must i disconnect from DB2 and re-connect every-time?
(or close and open SQL querys ?)
JH

You definitely do not need to disconnect/reconnect. That would be really
bad for performance.

I don't know what close and open queries mean in your context....

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

Dave Hughes's Avatar
Dave Hughes
Guest
n/a Posts
July 3rd, 2008
01:25 AM
#5

Re: Re: Delphi + Stored Procedure + Locks
John Hopfield wrote:
Quote:
Originally Posted by
On 30 Giu, 13:47, Serge Rielau <srie...@ca.ibm.comwrote:
Quote:
Originally Posted by
If you were Pinocchio I'd say: "Listen to the grasshopper".

>
:) (i live 100Km from Collodi :) )
>
Quote:
Originally Posted by
Locks have be held until the transaction commits.
Ideally your application should do this. But you add the COMMIT in
your procedure as well if it encapsulates a complete transaction.

>
I have tried to add a "COMMIT" at the end of my Stored Procedure.
But nothing is changed.


You don't need a COMMIT in the stored proc definition - you need to
commit the transaction in your client's code.

My Delphi knowledge is rather rusty (v6), but you should find a Commit
method on the connection object (depending on the connection
architecture you're using ... I think the ADO components used something
different like EndTran, but all the rest used methods named Commit and
Rollback). Anyway - that's what you'll need to call to commit the
transaction.


Cheers,

Dave.

John Hopfield's Avatar
John Hopfield
Guest
n/a Posts
July 3rd, 2008
07:45 AM
#6

Re: Re: Delphi + Stored Procedure + Locks
On 3 Lug, 03:19, "Dave Hughes" <d...@waveform.plus.comwrote:
Quote:
Originally Posted by
You don't need a COMMIT in thestoredproc definition - you need to
commit the transaction in your client's code.
>
My Delphi knowledge is rather rusty (v6), but you should find a Commit
method on the connection object (depending on the connection
architecture you're using ... I think the ADO components used something
different like EndTran, but all the rest used methods named Commit and
Rollback). Anyway - that's what you'll need to call to commit the
transaction.
>
Cheers,
>
Dave.


Thank you Dave.
But when i call TDataBase.Commit method it say that there is not an
active transaction.
So i think that is not a delphi problem.

Yesterday i found this...
In my Stored Procedure there are some "select into".
When i put the "WITH UR" clause at the end of these "select"
the lock on my table disappears...so the problem is resolved...

(no COMMIT at the end of my SP)

But there is another problem...

I don't have a good knowledge of transactions and isolation
levels...but...
i think that a select "WITH UR" it's a risk...because maybe its
possible
to retrieve "dirty" data not committed by another client...
and i don't want so...



JH

Dave Hughes's Avatar
Dave Hughes
Guest
n/a Posts
July 3rd, 2008
09:55 AM
#7

Re: Re: Delphi + Stored Procedure + Locks
John Hopfield wrote:
Quote:
Originally Posted by
On 3 Lug, 03:19, "Dave Hughes" <d...@waveform.plus.comwrote:
Quote:
Originally Posted by
You don't need a COMMIT in thestoredproc definition - you need to
commit the transaction in your client's code.

My Delphi knowledge is rather rusty (v6), but you should find a
Commit method on the connection object (depending on the connection
architecture you're using ... I think the ADO components used
something different like EndTran, but all the rest used methods
named Commit and Rollback). Anyway - that's what you'll need to
call to commit the transaction.

Cheers,

Dave.

>
Thank you Dave.
But when i call TDataBase.Commit method it say that there is not an
active transaction.
So i think that is not a delphi problem.


Ah, you'll need to "start" the transaction with the StartTransaction
method first. In the case of DB2 this is essentially a no-op (you're
always "in a transaction" if you're connected to a DB2 database), but
the BDE connection architecture needs it anyway.

So, the code for calling your SP should look something like the
following (assuming no silly errors:

{ DB is a connected TDatabase component, SP is a TStoredProc component
with the various properties (StoredProcName, Params, etc.) already
filled out }

DB.StartTransaction;
try
SP.ExecProc;
DB.Commit;
except
on E: Exception do begin
// Something's gone horribly wrong - rollback the
// transaction and report the error
DB.Rollback;
MessageDlg(E.Message, mtError, [mbOk], 0);
end;
end;
Quote:
Originally Posted by
Yesterday i found this...
In my Stored Procedure there are some "select into".
When i put the "WITH UR" clause at the end of these "select"
the lock on my table disappears...so the problem is resolved...
>
(no COMMIT at the end of my SP)
>
But there is another problem...
>
I don't have a good knowledge of transactions and isolation
levels...but...
i think that a select "WITH UR" it's a risk...because maybe its
possible
to retrieve "dirty" data not committed by another client...
and i don't want so...


That's correct. WITH UR is "with uncommitted read"; in other words it
permits the query to read changed rows which haven't yet been committed
by other transactions. If you want to ensure that a particular
transaction isolation level is used (other than whatever the client
stipulates), I'd suggest WITH CS.

Speaking of which, what's the TransIsolation property of your TDatabase
component set to? If its set to tiRepeatableRead, then that's your
problem - set it to tiReadCommitted (the default) instead (I /think/
tiReadCommitted translates to CS in the case of DB2 - I'm not sure
though - I'll try and do a bit of digging later).

Anyway - the main problem here is long running transactions. If you're
not explicitly committing the transaction, then its remaining active
(and potentially holding locks) for as long as you're connected to the
database. Using something like the code above to ensure that the stored
proc execution is committed (or rolled back) ASAP should fix any
contention issues you're having. Still, check the TransIsolation
property too.


Cheers,

Dave.

John Hopfield's Avatar
John Hopfield
Guest
n/a Posts
July 4th, 2008
02:05 PM
#8

Re: Re: Delphi + Stored Procedure + Locks
On 3 Lug, 21:12, "Dave Hughes" <d...@waveform.plus.comwrote:
Quote:
Originally Posted by
Hmm - rather than answer that directly I'll try and give an overview on
how various method calls in Delphi translate into actions in DB2 -
hopefully that should explain the situation above. My apologies if any
of the following appears patronizing - I've assumed very little
knowledge purely in the interests of completeness:


Thank you Dave for your time.

JH

 
Not the answer you were looking for? Post your question . . .
182,322 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors