heroku current transaction is aborted
I have a small free app running on heroku.
intermittently the app stops working and displays the default heroku error page on the screen. when I check the logs I see the following:
ActiveRecord::StatementInvalid (PGError: ERROR: current transaction is aborted, commands ignored until end of transaction block : SELECT * FROM "users" WHERE ("users"."password" = E'' AND "users"."userid" = E'') LIMIT 1): app/models/user.rb:5:in
authenticate' app/controllers/admin_controller.rb:6:in
login'
in my controller I am just doing following:
user = User.authenticate(params[:storeid], params[:password])
and in User model:
def self.authenticate(userid, password)
user = self.find_by_userid_and_password(userid, password)
user
end
The error messages leads me to believe that some connections are left hanging and are never closed. Is this ever a case in a ruby app?
In my app there are also many places where I am using find_by_sql. does that require us to explicitly close connections?
Like this:
@sqlstmmt1 = "INSERT INTO addresses (\"line1\", \"line2\", city, state, zip, county) VALUES ('" + params[:line1] + "', 开发者_如何学运维'"+ params[:line2] + "', '"+params[:city]+ "', '" + params[:state] + "', '" + params[:zip]+ "', '" + params[:county]+"')"
sql = ActiveRecord::Base.connection();
sql.begin_db_transaction
I get that error due to what I think is when trying to insert utf-8 characters, so I guess your password could have exotic characters. BTW: You should escape the sql-string you use, embedding params[:anything] is baaaad.
i was getting a similar error and this bit of Rails documentation helped:
http://apidock.com/rails/ActiveRecord/Transactions/ClassMethods
Warning: one should not catch ActiveRecord::StatementInvalid exceptions inside a transaction block. ActiveRecord::StatementInvalid exceptions indicate that an error occurred at the database level, for example when a unique constraint is violated. On some database systems, such as PostgreSQL, database errors inside a transaction cause the entire transaction to become unusable until it’s restarted from the beginning. Here is an example which demonstrates the problem:
...
I just relaunched the application and everything goes back right
heroku restart
Hope it will help!
精彩评论