I had problems with sqlobject's doInTransaction() in the past. Ian wrote 
the following for me which I have been using successfully.

def doInTransaction(func, *args, **kw):
     """
     This routine can be used to run a function in a transaction,
     rolling the transaction back if any exception is raised from
     that function, and committing otherwise.

     Use like::

         doInTransaction(process_request, os.environ)

     This will run ``process_request(os.environ)``.  The return
     value will be preserved.
     """
     old_conn = sqlhub.getConnection()
     conn = old_conn.transaction()
     sqlhub.threadConnection = conn
     try:
         try:
             value = func(*args, **kw)
         except:
             conn.rollback()
             raise
         else:
             conn.commit()
             return value
     finally:
         sqlhub.threadConnection = old_conn
### /end doInTransaction ###

Hope this helps.

--
Rick

On Thu, 15 Mar 2007, sophana wrote:

> Hi
>
> I'm testing transactions with sqlobject.
> I found the doInTransaction method
>
> so I made a test:
> (python2.4 sqlobject 0.8.0 mysql innodb table)
>
> class TestSql(SQLObject):
>    k    = IntCol(default=None)
>    v    = IntCol(default=None)
>
>
> def init():
>    TestSql.dropTable()
>    TestSql.createTable(ifNotExists=True)
>
>    TestSql(k=1,v=1)
>    TestSql(k=2,v=2)
>
> def inc(k):
>    a=TestSql.select(TestSql.q.k==k,forUpdate=True)[0]
>    sleep(0.1)
>    a.v += 1
>
> def go2(k):
>    for a in range(100):
>        sqlhub.doInTransaction(inc,k)
>
> go2(1)
>
> At the end, I have an sql error saying that there is no more connection
> possible. There are indeed 100 processes sleeping.
> What is creating all these connections?
>
> Now some more questions. What is the difference between
> - using a transaction object
> - simply using connection.query('begin;') and connection.query('commit;')
>
> example:
>
> def inc(k):
>    connection.query('begin;')
>    a=TestSql.select(TestSql.q.k==k,forUpdate=True)[0]
>    sleep(0.1)
>    a.v += 1
>    connection.query('commit;')
>
> this works just fine. Any problem with this approach? Isn't it optimal
> and thread safe?
>
> What about the cache? Do I have to add in sqlmeta: cacheValues=False?
> When doing this, I saw the debug log and it is terribly not efficient.
>
> I tested this with 2 process and it was ok. Is it ok with 1
> multithreaded process?
> My application has 2 multithreaded (web server) process over 2 distant
> replicated mysql databases.
> I've read somewhere that mysql transaction should work with replicated
> databases.
>
> Thanks for your help
>
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share your
> opinions on IT & business topics through brief surveys-and earn cash
> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
> _______________________________________________
> sqlobject-discuss mailing list
> sqlobject-discuss@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
>

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to