在Web应用程序中使用Postgres:“事务中止”错误

时间:2010-02-05 17:46:48

标签: python postgresql concurrency

最近我出于性能原因将我正在从MySQL开发的Web应用程序移动到PostgreSQL(我需要PostGIS提供的功能)。现在经常遇到以下错误:

current transaction is aborted, commands ignored until end of transaction block

服务器应用程序使用mod_python。在hailing函数中发生错误(即为该特定客户端创建新会话的函数)。这里是适当的代码段(异常发生在调用sessionAppId的行上:

def hello(req):
req.content_type = "text/json"
req.headers_out.add('Cache-Control', "no-store, no-cache, must-revalidate")
req.headers_out.add('Pragma', "no-cache")
req.headers_out.add('Expires', "-1")
instance = req.hostname.split(".")[0]

cookieSecret = '....' # whatever :-)
receivedCookies = Cookie.get_cookies(req, Cookie.SignedCookie, secret = cookieSecret)
sessionList = receivedCookies.get('sessions', None)
sessionId = str(uuid.uuid4())
if sessionList:
    if type(sessionList) is not Cookie.SignedCookie:
        return "{status: 'error', errno:1, errmsg:'Permission denied.'}"
    else:
        sessionList = sessionList.value.split(",")
        for x in sessionList[:]:
            revisionCookie = receivedCookies.get('rev_' + str(sessionAppId(x, instance)), None)
            # more processing here....
# .....
cursors[instance].execute("lock revision, app, timeout IN SHARE MODE")
cursors[instance].execute("insert into app (type, active, active_revision, contents, z) values ('session', true, %s, %s, 0) returning id", (cRevision, sessionId))
sAppId = cursors[instance].fetchone()[0]
cursors[instance].execute("insert into revision (app_id, type) values (%s, 'active')", (sAppId,))
cursors[instance].execute("insert into timeout (app_id, last_seen) values (%s, now())", (sAppId,))
connections[instance].commit()
# .....

这是sessionAppId本身:

def sessionAppId(sessionId, instance):
cursors[instance].execute("select id from app where type='session' and contents = %s", (sessionId, ))
row = cursors[instance].fetchone()
if row == None:
    return 0
else:
    return row[0]

一些澄清和其他问题:

  1. 游标[instance]和connections [instance]是数据库连接以及在此域名上提供的Web应用程序实例的游标。即同一台服务器提供example1.com和example2.com,并使用这些词典根据请求所针对的服务器名称调用相应的数据库。
  2. 我真的需要锁定hello()函数中的表吗?
  3. 需要hello()中的大多数代码来维护每个浏览器选项卡一个单独的会话。我找不到与cookie有关的方法,因为打开网站的浏览器标签共享cookie池。有没有更好的方法呢?
  4. 非常感谢。

1 个答案:

答案 0 :(得分:10)

该错误是由于先例错误引起的。看看这段代码:

>>> import psycopg2
>>> conn = psycopg2.connect('')
>>> cur = conn.cursor()
>>> cur.execute('select current _date')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
psycopg2.ProgrammingError: syntax error at or near "_date"
LINE 1: select current _date
                       ^

>>> cur.execute('select current_date')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
psycopg2.InternalError: current transaction is aborted, commands ignored until end of transaction block

>>> conn.rollback()
>>> cur.execute('select current_date')
>>> cur.fetchall()
[(datetime.date(2010, 2, 5),)]
>>> 

如果您熟悉twisted,请查看twisted.enterprise.adbapi以获取如何处理游标的示例。基本上你应该总是提交或回滚你的游标:

try:
    cur.execute("...")
    cur.fetchall()
    cur.close()
    connection.commit()
except:
    connection.rollback()