Search code examples
mysqltransactionslaravel-5.1sphinxindexer

Sphinx indexer does not fetch data when MySQL transaction is used


please help me with running phpunit tests that check module with sphinx search engine.

To search in that module I use two sphinx indexes docs and docsdelta. After new data are appear in the DB, I do following:

exec("indexer docsdelta --rotate");
exec("indexer --merge docs docsdelta --rotate");

It works well on my website and I am able to add new document through the web interface and it appears in the search.

On the same time when I run phpunit test and it creates new document "on fly",

exec("indexer docsdelta --rotate");

does not fetch any new data. My phpunit tests use transactions to rollback the database to the previous state and I notice, that indexer works properly in case if I switch off transactions. Additionally I am able to see new data in the DB, just before and after running indexer. Maybe I missed something but I do not understand why transaction has an influence on indexer.

Is it some way to use indexer docdelta together with MySQL transaction?

Thank you in advance for help!


Solution

  • To make changes you make inside the transaction visible outside, i.e. to indexer you need to change isolation level of the indexer's SELECT queries. You can do it like this:

    sql_query_pre = SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

    You can read more about mysql isolation levels here https://dev.mysql.com/doc/refman/5.7/en/innodb-transaction-isolation-levels.html