2011-06-27 7 views
1

のRuby 1.9.1 + ActiveRecordの2.3.5 + Postgresの8.3.7「PGError:サーバーへの接続なし」の作成やここで

を保存しようとしているときには、私のコードのラフスケッチです。明白な構文の詳細は無視してください。以下のモデルはActiveRecord :: BaseをActiveRecord 2.3.5経由でPostgres 8.3.7データベースに継承しています。私は保存し、保存を呼び出す!、または代わりに新しいの作成して保存やっている場合にかかわらず....

/...gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:in `rescue in log': PGError: no connection to the server (ActiveRecord::StatementInvalid) 
: ROLLBACK 
    from .../gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `log' 
    from .../gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/connection_adapters/postgresql_adapter.rb:550:in `execute' 
    from .../gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/connection_adapters/postgresql_adapter.rb:576:in `rollback_db_transaction' 
    from .../gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:143:in `rescue in transaction' 
    from .../gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:125:in `transaction' 
    from .../gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/transactions.rb:182:in `transaction' 
    from .../gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/transactions.rb:200:in `block in save_with_transactions!' 
    from .../gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/transactions.rb:208:in `rollback_active_record_state!' 
    from .../gems/activerecord-2.3.5/lib/active_record/transactions.rb:200:in `save_with_transactions!' 

:常に何が起こる

class TableA 
    has_many :tableB 
end 

class TableB 
    belongs_to :tableA 
    has_many :tableC 
end 

class TableC 
    belongs_to :tableB 
    has_many :tableD 
end 

class TableD 
    belongs_to :tableC 
    has_many :tableE 
end 

class TableE 
    belongs_to :tableD 
end 

# Note that tableA has fids that are referenced in tableE but is not part of this model 
# 
# Later in the script, in the same global scope, I want to add entries to these tables if 
# I cannot find what I need. Bear in mind that this part betrays much Ruby noobiness. 

toAdd.each do |widget| 
    add_tableA = TableA.find_by_sql().first # assumes I will get one back based on earlier sanity checks 

    add_tableB = TableB.find_by_sql().first 
    if (add_tableB == nil) 
     new_tableB = TableB.new(# value assignments) 
     new_tableB.save 
     add_tableB = TableB.find_by_sql().first 
    end 

    add_tableC = TableC.find_by_sql().first 
    if (add_tableC == nil) 
     new_tableC = TableC.new(# value assignments) 
     new_tableC.save 
     add_tableC = TableC.find_by_sql().first 
    end 

    add_tableD = TableD.find_by_sql().first 
    if (add_tableD == nil) 
     new_tableD = TableD.new(# value assignments) 
     new_tableD.save 
     add_tableD = TableD.find_by_sql().first 
    end 

    # I step into TableA again because items in TableE are linked to items in TableA, but they are 
    # distinct from the "high level" item I grabbed from TableA earlier. 

    add_tableA = TableA.find_by_sql().first 
    if (add_tableA == nil) 
     new_tableA = TableA.new(# value assignments) 
     new_tableA.save 
     add_tableA = TableA.find_by_sql().first 
    end 

    # Now that I have a TableA id to put into TableE, just create TableE row because I know this 
    # does not exist yet. 

    new_tableE = TableE.new(# value assignments) # again, this is assumed to be new based on earlier checks 
    new_tableE.save 

end 

は、私は次のスタックトレースを取得することです。

straceは、これを実行するたびに1つのBEGIN..INSERT..COMMITトランザクションしか取得できないことを示しています。 COMMITが送信される前に、同じループ実行または次のトランザクションのいずれかでトランザクション内のINSERTを試みると、接続が切断されて終了します。明らかに、私はActiveRecordモデルにステップインしているところで何か間違っています。

最初の成功したINSERT文が設定される直前に次のstraceが表示されます。 ActiveRecordにテーブルを通ってこれを保存できるものがありますか、それとも単にDoing It Wrongですか?

rt_sigaction(SIGPIPE, {0x1, [], SA_RESTORER|SA_RESTART, 0x3876c0eb10}, {0x4b2ff0, [], SA_RESTORER|SA_RESTART, 0x3876c0eb10}, 8) = 0 

sendto(3, "Q\0\0\2e   SELECT attr.attna"..., 614, 0, NULL, 0) = 614 

rt_sigaction(SIGPIPE, {0x4b2ff0, [], SA_RESTORER|SA_RESTART, 0x3876c0eb10}, {0x1, [], SA_RESTORER|SA_RESTART, 0x3876c0eb10}, 8) = 0 

poll([{fd=3, events=POLLIN|POLLERR}], 1, -1) = 1 ([{fd=3, revents=POLLIN}]) 

recvfrom(3, "T\0\0\0:\0\2attname\0\0\0\4\341\0\2\0\0\0\23\[email protected]\377\377\377\377\0"..., 16384, 0, NULL, NULL) = 541 

ここのヘルプは大歓迎です。

+0

サーバーに接続できません。データベースサーバーを正しく構成して、適切なモードで実行していますか? –

+0

ありがとうございますが、私は最後に編集してから問題がないことを発見しました。このpostgresのインスタンスは、トリガーイベントを他のプロセスにプッシュする処理を実行する第2のプロセスに依存します。そのプロセスは実行されていないため、データベースサーバは最初にコミットされたINSERTの後に起動されました。 – giromide

答えて

1

ありがとうございます。私は誰でもこの問題を解決しようとしていることをお詫びします。このpostgresのインスタンスは、トリガーイベントを他のプロセスにプッシュする処理を実行する第2のプロセスに依存します。そのプロセスは実行されていないため、データベースサーバは最初にコミットされたINSERTの後に起動されました。これはカスタムの社内のものです。

関連する問題