Re: DB API 2.0 and transactions



<posted & mailed>

Magnus Lycka wrote:

> You might have spotted a fairly nasty bug there!

> PostgreSQL violates the SQL standards by running in autocommit mode
> unless you explicitly perform its non-standard BEGIN command. If you
> are right about the behaviour you describe, the PostgreSQL binding
> for Python that you use may have taken the easy route, and performs
> a "BEGIN" on connect and after every commit or rollback.

Check this out...

<code>
import pgdb
import time

print time.ctime()
db = pgdb.connect(user='test', host='localhost', database='test')
time.sleep(5)
db.cursor().execute('insert into time_test
(datetime)
values
(CURRENT_TIMESTAMP)')
db.commit()
curs = db.cursor()
curs.execute('select datetime from time_test order by datetime desc limit
1')
row = curs.fetchone()
print row[0]
</code>

<output>
Fri Jun 10 17:27:21 2005
'2005-06-10 17:27:21.654897-05'
</output>

Notice the times are exactly the same instead of 5 sec difference.

What do you make of that? Some other replies to this thread seemed to
indicate that this is expected and proper behavior.

-- C

> If so, this is a serious bug, and should be reported as one. The correct
> thing to do is to insert the BEGIN just before the first SQL statement
> that is affecting transactions. Of course, this means that the binding
> needs to keep track of transaction state, and this makes it a little
> bit more complicated. You'd need something like this in the binding:
>
> class connection:
> def __init__(...):
> ...
> self.inTxn = False
>
> def commit(...):
> ...
> self.inTxn = False
>
> def rollback(...):
> ...
> self.inTxn = False
>
> def execute(...):
> ...
> if not self.inTxn:
> perform the BEGIN command against the backend
> self.inTxn = True
> ...
>
> Actually, this isn't perfect either, because not all SQL commands
> (should) initate transactions, but it's a lot closer to what we want.
>
> This bug has implications far beyond timestamps. Imagine two transaction
> running with isolation level set to e.g. serializable. Transaction A
> updates the AMOUNT column in various rows of table X, and transaction
> B calculates the sum of all AMOUNTS.
>
> Lets say they run over time like this, with | marking begin and >
> commit (N.B. ASCII art, you need a fixed font):
>
> ...|--A-->.......|--A-->........
> ...........|-B->.........|-B->..
>
> This works as expected...but imagine transactions implicitly
> begin too early:
>
> |-----A-->|---------A-->|-------
> |------------B->|----------B->|-
>
> This will cause the aggregations in B to show "delayed" results.
> Not at all what one might expect...
>
>
> For more about isolation levels, see e.g. here:
> http://pgsqld.active-venture.com/transaction-iso.html


.



Relevant Pages

  • Expired Backup Files
    ... I can't get SQL to delete the old backup and transaction ... I was told that there is a bug with Windows 95, ...
    (microsoft.public.sqlserver.server)
  • Why did Digital go broke? (Was: Re: VMS Source Listing CD - Table of contents?)
    ... allow me to bring some much needed functionality to VMS that will enable any ... Dump of transaction log SYS$COMMON:SYSTEM$TIER3.LM$JOURNAL;1 ... Another 2PC bug - I need your help! ... characters of my participant name because I'm only interested in TIER3 TIP ...
    (comp.os.vms)
  • Re: Asynchronous Stored Procedure Never Returns - Help?
    ... If you have the Sql Server 2000 or 2005 docs they are thorough and can be ... for Transaction SQL Reference from the drop-down or select a keyword from ... your query in Query Analyzer or Sql Server Management Studio, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: DB API 2.0 and transactions
    ... CURRENT_TIMESTAMP within a transaction should be the same. ... manadate that CURRENT_TIMESTAMP in only evaluated once in each SQL ... transaction-initiating SQL statement takes place. ... src = self.__cnx.source ...
    (comp.lang.python)
  • Re: commit work not working?
    ... Because he is using straight sql, ... The transaction is made up of the statements that occur ... locks the stock table, updates rows in the stock table ... The database server must perform this sequence of operations either ...
    (comp.databases.informix)