新手问题 mysql2 报错 Unsupported statement: BEGIN

cichol · 2014年07月01日 · 最后由 cicholgricenchos 回复于 2014年07月02日 · 4009 次阅读

京东云擎里使用 mysql2 的时候,包括 ActiveRecord 和 Sequel,使用 create save 等方法时会出现如下错误

ActiveRecord::StatementInvalid - Mysql2::Error: Unsupported statement: BEGIN: Sequel::DatabaseError - Mysql2::Error: Unsupported statement:

【Sequel 没有给出那个 statement 是什么】 然而,若是 Sequel 中使用DB[:users].insert()却可以成功完成查询,我猜测可能是 create 等方法使用了京东云不支持的查询语句(上面的 BEGIN?),但是我不知道怎么配置 AR 来禁用这些查询语句。

京东云擎 mysql 的可用查询有 insert delete update select create。

请问有什么方法可以解决此问题?谢谢。

贴上完整的报错代码

Sequel::DatabaseError - Mysql2::Error: Unsupported statement:
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/database/logging.rb:58:in `query'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/database/logging.rb:58:in `block in log_connection_execute'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/database/logging.rb:33:in `log_yield'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/database/logging.rb:58:in `log_connection_execute'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/adapters/shared/mysql.rb:301:in `begin_new_transaction'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/database/transactions.rb:224:in `begin_transaction'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/adapters/shared/mysql.rb:310:in `begin_transaction'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/database/transactions.rb:116:in `_transaction'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/database/transactions.rb:100:in `block in transaction'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/database/connecting.rb:229:in `block in synchronize'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/connection_pool/threaded.rb:104:in `hold'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/database/connecting.rb:229:in `synchronize'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/database/transactions.rb:89:in `transaction'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/model/base.rb:1978:in `checked_transaction'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/model/base.rb:1520:in `block in save'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/model/base.rb:1966:in `checked_save_failure'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/model/base.rb:1520:in `save'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sequel-4.11.0/lib/sequel/model/base.rb:148:in `create'
/home/vcap/app/app.rb:19:in `block in '
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1603:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1603:in `block in compile!'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:966:in `[]'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:966:in `block (3 levels) in route!'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:985:in `route_eval'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:966:in `block (2 levels) in route!'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1006:in `block in process_route'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1004:in `catch'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1004:in `process_route'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:964:in `block in route!'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:963:in `each'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:963:in `route!'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1076:in `block in dispatch!'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `block in invoke'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `catch'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `invoke'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1073:in `dispatch!'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:898:in `block in call!'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `block in invoke'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `catch'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `invoke'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:898:in `call!'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:886:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.5.3/lib/rack/protection/xss_header.rb:18:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.5.3/lib/rack/protection/path_traversal.rb:16:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.5.3/lib/rack/protection/json_csrf.rb:18:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.5.3/lib/rack/protection/frame_options.rb:31:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/logger.rb:15:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/commonlogger.rb:33:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:217:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:210:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/head.rb:11:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:180:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:2014:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1478:in `block in call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1788:in `synchronize'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.5/lib/sinatra/base.rb:1478:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/deflater.rb:25:in `call'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/thin-1.6.2/lib/thin/connection.rb:86:in `block in pre_process'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/thin-1.6.2/lib/thin/connection.rb:84:in `catch'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/thin-1.6.2/lib/thin/connection.rb:84:in `pre_process'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/thin-1.6.2/lib/thin/connection.rb:53:in `process'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/thin-1.6.2/lib/thin/connection.rb:39:in `receive_data'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/thin-1.6.2/lib/thin/backends/base.rb:73:in `start'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/thin-1.6.2/lib/thin/server.rb:162:in `start'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/thin-1.6.2/lib/thin/controllers/controller.rb:87:in `start'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/thin-1.6.2/lib/thin/runner.rb:199:in `run_command'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/thin-1.6.2/lib/thin/runner.rb:155:in `run!'
/home/vcap/app/vendor/bundle/ruby/1.9.1/gems/thin-1.6.2/bin/thin:6:in `'
/home/vcap/app/vendor/bundle/ruby/1.9.1/bin/thin:23:in `load'
/home/vcap/app/vendor/bundle/ruby/1.9.1/bin/thin:23:in `
'
221.203.22.245, 172.18.137.55 - - [01/Jul/2014 17:57:43] "GET /test HTTP/1.1" 500 - 0.0038

顺带一问,有没有人成功在 Win 下安装过 bson(~>2.2.0) 这个包?总是在安装过程中编译出错,按照官方 github 给出的方案修改 ext/native.c 仍然无法解决,错误信息中涉及 ruby 的 win32.h。这个包是 mongoid 的必需项,但是似乎没考虑支持 win 啊,类似的 issue 和提问有很多,但官方似乎根本没在意这个问题。。

被折腾疯了,翻 Sequel 的源码中,有没有什么方法可以快速定位 method 位置(:3」∠)

不要用 sequel,乖乖用 AR

可能是京东云不支持 MySQL Transaction 的 BEGIN 语法,可以尝试换成这样:

config/initializers/activerecord_patch.rb

ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.class_eval do
  def begin_db_transaction
    execute "SET AUTOCOMMIT=0"
  rescue
  end

  def commit_db_transaction #:nodoc:
    execute "COMMIT"
    execute "SET AUTOCOMMIT=1"
  rescue
    # Transactions aren't supported
  end

  def rollback_db_transaction #:nodoc:
    execute "ROLLBACK"
    execute "SET AUTOCOMMIT=1"
  rescue
    # Transactions aren't supported
  end
end

#3 楼 @huacnlee 测试成功!感谢指点!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号