1
目前,我正在學習transactions,我收到此錯誤信息:BadRequestError:嵌套不支持事務
BadRequestError: Nested transactions are not supported
我不知道如何解決這個問題。任何援助將不勝感激。
main.py
import webapp2
import jinja2
import os
import datetime
from google.appengine.ext import db
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
transaction_status = create_message_txn("Python_Board", "message name", "Message Title", "Here is the message text")
message = Message.all()
template_values = {
'transaction_status': transaction_status,
'message': message,
}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
class MessageStore(webapp2.RequestHandler):
def post(self):
message = Message()
message.message_title = self.request.get('message_title')
message.message_text = self.request.get('message_text')
photo.put()
self.redirect('/')
class MessageBoard(db.Expando):
pass
class Message(db.Expando):
pass
def create_message_txn(board_name, message_name, message_title, message_text):
board = db.get(db.Key.from_path('MessageBoard', board_name))
if not board:
board = MessageBoard(key_name=board_name)
board.count = 0
message = Message(key_name=message_name, parent=board)
message.title = message_title
message.text = message_text
message.post_date = datetime.datetime.now()
board.count += 1
db.put([board, message])
try:
db.run_in_transaction(create_message_txn, board_name=board_name,
message_name=message_title,
message_title=message_title,
message_text=message_text)
except db.TransactionFailedError, e:
transaction_status = "Transaction failed: %s " %e
app = webapp2.WSGIApplication([('/', MainPage),
('/new_message',MessageStore)], debug=True)
的index.html
<html>
<body>
<form action="/new_photo" method="post">
<label for="message_title">Message Title</label>
<div><textarea name="message_title" rows="1" cols="60" id="message_title"></textarea></div>
<label for="message_text">Message Text</label>
<div><textarea name="message_text" rows="3" cols="60" id="message_text"></textarea></div>
<input type="submit" value="Submit">
</form>
<div><hr></hr></div>
<div><b>{{ transaction_status }}</b></div>
<div><hr></hr></div>
{% for m in message(limit=5): %}
<div><b>{{ m.title }}</b></div>
<div>{{ m.text }}</div>
<div>{{ m.date }}</div>
<div><hr></hr></div>
<b>{% endfor %}</b>
</body>
</html>
+1感謝您的答覆解決這個問題。我是交易新手,剛開始學習Google App引擎。我將這些代碼砍到一起,試圖瞭解事務是如何工作的。我如何遞歸調用create_message_txn?你能推薦一個簡單的例子來展示交易行爲嗎? – Anthony 2013-03-24 19:52:13
@你的錯誤與你的程序設計中涉及的交易無關,交易很好,因爲整個交易要麼失敗,要麼成功,典型的例子就是銀行業務,因爲你要付款給某人,這是兩部分的過程,從您的賬戶中提取資金並貸給別人,這需要2個查詢。如果您的帳戶被撤銷,然後出現錯誤,您就沒有錢了,而另一個人從未獲得貸記! 'run_in_transaction'允許你執行多部分查詢,可以完成所有部分或者沒有任何部分 – dm03514 2013-03-24 19:56:31
好的,我將嘗試重寫這個並很快回來。 – Anthony 2013-03-24 20:59:40