activegraph
activegraph copied to clipboard
Neo.ClientError.Transaction.TransactionNotFound
Getting the following error when running tests using rspec and rails 5.1. It is only happening in the test environment, the same code executed on a console works fine.
Neo4j::Core::CypherSession::CypherError:
Cypher error:
Neo.ClientError.Transaction.TransactionNotFound: Unrecognized transaction id. Transaction may have timed out and been rolled back.
/bundle/gems/neo4j-core-8.1.0/lib/neo4j/core/cypher_session/responses/http.rb:107:in validate_faraday_response!
/bundle/gems/neo4j-core-8.1.0/lib/neo4j/core/cypher_session/responses/http.rb:14:in initialize
/bundle/gems/neo4j-core-8.1.0/lib/neo4j/core/cypher_session/adaptors/http.rb:41:in new
/bundle/gems/neo4j-core-8.1.0/lib/neo4j/core/cypher_session/adaptors/http.rb:41:in query_set
/bundle/gems/neo4j-core-8.1.0/lib/neo4j/core/cypher_session/adaptors.rb:117:in block in queries
/bundle/gems/neo4j-core-8.1.0/lib/neo4j/core/cypher_session/adaptors.rb:199:in new_or_current_transaction
/bundle/gems/neo4j-core-8.1.0/lib/neo4j/core/cypher_session/adaptors.rb:116:in queries
/bundle/gems/neo4j-core-8.1.0/lib/neo4j/core/cypher_session/adaptors.rb:108:in query
/bundle/gems/neo4j-core-8.1.0/lib/neo4j/core/cypher_session.rb:30:in block (2 levels) in <class:CypherSession>
/bundle/gems/neo4j-core-8.1.0/lib/neo4j-core/query.rb:244:in response
/bundle/gems/neo4j-core-8.1.0/lib/neo4j-core/query.rb:276:in each
/bundle/gems/neo4j-9.1.2/lib/neo4j/active_node/query/query_proxy_methods.rb:96:in first
/bundle/gems/neo4j-9.1.2/lib/neo4j/active_node/query/query_proxy_methods.rb:96:in block in include?
/bundle/gems/neo4j-9.1.2/lib/neo4j/active_node/query/query_proxy_methods.rb:283:in query_with_target
/bundle/gems/neo4j-9.1.2/lib/neo4j/active_node/query/query_proxy_methods.rb:89:in include?
./spec/models/friendship_request_spec.rb:111:in block (4 levels) in <top (required)>
./spec/rails_helper.rb:107:in block (3 levels) in <top (required)>
/bundle/gems/neo4j-9.1.2/lib/neo4j/active_base.rb:40:in block in run_transaction
/bundle/gems/neo4j-core-8.1.0/lib/neo4j/transaction.rb:135:in run
/bundle/gems/neo4j-9.1.2/lib/neo4j/active_base.rb:39:in run_transaction
./spec/rails_helper.rb:106:in block (2 levels) in <top (required)>
Placed this on rails_helper.rb
Neo4j::ActiveBase.run_transaction do |tx|
example.run
tx.mark_failed
end
end
This happens on the following test
friendship_request.create_relationship
expect(friendship_request.user.node.pending_friends.include?(friendship_request.friend.node)).to be true
The create_relationship method finds two nodes and sets some params between them via Neo4j::Core::Query
friendship_request.user.node.pending_friends
returns a QueryProxy
friendship_request.friend.node
return an object that extends from ActiveNode
Neo4j database version:
neo4j
gem version: 9.1.2
neo4j-core
gem version: 8.1
Anything that I might be missing?
This is certainly a somewhat strange. Some questions:
In the rails_helper.rb
, where is the run_transaction
placed? It should be around examples, not around the suite. Neo4j doesn't currently have nested transactions and so if you have a transaction running for an entire suite you could get problems. There could be timeout issues, though if you are consistently hitting the database within the timeout you should be fine there. The thing that I would worry about more is that if a transaction is around multiple examples, if one example has an exception bubble up through a transaction, the entire outer transaction would be rolled back, meaning that it wouldn't be available for the next example. I think that would cause what you are seeing.
Actually, looking more closely I see that you are always doing tx.mark_failed
, so if you've had multiple example for a bit you would have already noticed problems.
I might suggest trying to simply clear the database after each example (see this section of the docs which also discusses the issues with transactions a bit). It might be a bit slower (though I haven't noticed much slowness in my usage of this technique), but it should avoid any issues with nested transaction weirdness.
Hey @cheerfulstoic, run_transaction
is placed around each example like you said. Also tried to clean up the database after each example and it didn't fix the issue 😢
Did you try removing the run_transaction
when adding the cleanup of the DB?
I seem to made the test pass when changing it to
config.around(:each) do |example|
example.run
Neo4j::ActiveBase.run_transaction(&:mark_failed)
end
I'll give more news once I have them
Although one the things I noticed when running the tests and using byebug was that
Neo4j::ActiveBase.run_transaction do |tx|
tx.mark_failed
end
inside the run_transaction
block, tx.id is nil
@cheerfulstoic the whole suit passes with
config.around(:each, neo4j: true) do |example|
example.run
Neo4j::ActiveBase.run_transaction(&:mark_failed)
end
and cleaning the database after each test like you said
Glad that it's working, though I'm a bit puzzled why the Neo4j::ActiveBase.run_transaction(&:mark_failed)
is needed unless you still have a transaction around your examples somehow...