Branch: Tag:

2013-03-14

2013-03-14 10:32:17 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Search.Queue.MySQL: Protect against interleaving SQL queries.

The Mysql database connection object is not reentrant,
so if multiple threads attempt to use the same connection
object at the same time, it will likely cause the error
"Lost connection to MySQL server during query".

In this case the function num_with_stage() could be called
to present ststus information while the queue was in use.

This patch fixes the problem by using thread local connections.

It also survives the database connection having gone stale.

2:      inherit .Base;    - Sql.Sql db; +    string url, table;    -  + protected Thread.Local _db = Thread.Local(); + Sql.Sql `db() + { +  // NB: We need to have a thread local connection, +  // since the status functions may get called +  // from some other thread while we're busy +  // performing sql queries elsewhere. +  Sql.Sql ret = _db->get(); +  if (ret && !ret->ping()) return ret; +  return _db->set(Sql.Sql( url )); + } +    Web.Crawler.Stats stats;   Web.Crawler.Policy policy;   Web.Crawler.RuleSet allow, deny;
29:    stats = _stats; policy = _policy;    allow=_allow; deny=_deny;    table = _table; +  url = _url;    -  db = Sql.Sql( _url ); +     perhaps_create_table( );   }