Permalink
Browse files

PostgreSQL: introduce transaction_active? rather than tracking activi…

…ty ourselves
  • Loading branch information...
1 parent 671bbd9 commit 045713ee240fff815edb5962b25d668512649478 @jeremy jeremy committed Sep 4, 2008
Showing with 32 additions and 0 deletions.
  1. +32 −0 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -518,6 +518,38 @@ def rollback_db_transaction
execute "ROLLBACK"
end
+ # ruby-pg defines Ruby constants for transaction status,
+ # ruby-postgres does not.
+ PQTRANS_IDLE = defined?(PGconn::PQTRANS_IDLE) ? PGconn::PQTRANS_IDLE : 0
+
+ # Check whether a transaction is active.
+ def transaction_active?
+ @connection.transaction_status != PQTRANS_IDLE
+ end
+
+ # Wrap a block in a transaction. Returns result of block.
+ def transaction(start_db_transaction = true)
+ begin
+ if block_given?
+ begin_db_transaction if start_db_transaction
+ yield
+ end
+ rescue Exception => database_transaction_rollback
+ rollback_db_transaction if transaction_active?
+ raise unless database_transaction_rollback.is_a? ActiveRecord::Rollback
+ end
+ ensure
+ if transaction_active?
+ begin
+ commit_db_transaction
+ rescue Exception => database_transaction_rollback
+ rollback_db_transaction
+ raise
+ end
+ end
+ end
+
+
# SCHEMA STATEMENTS ========================================
def recreate_database(name) #:nodoc:

6 comments on commit 045713e

@drogus
Member
drogus commented on 045713e Sep 8, 2008

This transaction commits broke ActiveRecord with postgres for me: http://pastie.org/268222

everything is fine with commit 671bbd962

@jeremy
Member
jeremy commented on 045713e Sep 8, 2008

It’s fixed in a later commit. See http://github.com/rails/rails/commit/039d78a7d81c8ac49dcb6ba0304d32af671d56c9

@drogus
Member
drogus commented on 045713e Sep 10, 2008

Later commit doesn’t solve this, see the pastie, it’s:
http://github.com/rails/rails/tree/master/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L527 in current HEAD

Shouldn’t it be @connection.status instead of @connection.transaction_status.

@rails
rails commented on 045713e Sep 10, 2008

No, transaction_status is correct. I’m guessing you’re using postgres-pr which doesn’t provide full API coverage. Could you try with the postgres gem instead?

@jeremy
Member
jeremy commented on 045713e Sep 10, 2008

(oops, that was me ^^)

@drogus
Member
drogus commented on 045713e Sep 10, 2008

Thanks for help! I missed it – should have checked libraries first…

Please sign in to comment.