Issue: test_multiple_writers from ConcurrencyTestCase fails occasionally
Author: sYnfo
State: closed
Created: 2013-08-09 11:04:55

it seems to be very rare, I've only seen it happen once for 2.0.9 [0] and once for 2.1.4 [1] in ~30 builds. Sorry if opening an issue is not the right thing to do, just figured you may want to know. :)

[0] http://koji.fedoraproject.org/koji/getfile?taskID=5763686&name=build.log
[1] http://koji.fedoraproject.org/koji/getfile?taskID=5783003&name=build.log


Thank you for mentioning this, I will look into it.


I think the problem is the threads are waiting for locks to release and give up.  I noticed the time it took to run the tests was pretty high (versus ~4.4 seconds on my machine).

```
Ran 107 tests in 570.951s  # 2.0.9

Ran 127 tests in 273.637s   # 2.1.4
```

> The timeout parameter specifies how long the connection should wait for the lock to go away until raising an exception. The default for the timeout parameter is 5.0 (five seconds).

Looks like the tests are timing out, so two ideas come to mind:
1. Raise the timeout.
2. Use an in-memory sqlite database for this test.


OK, an in-memory database won't work since in-memory databases only exist for their connection.


@coleifer 
I think this also happens for MySQL. In somehow different way....
Sometimes multiple read will cause error.
I don't know whether I should open a new issue..lol
When I perform several requests nearly at the same time, there is around one tenth of the time I got

```
  File "/Data/filmsoc/src/src/peewee/peewee.py", line 1627, in get
    return clone.execute().next()
  File "/Data/filmsoc/src/src/peewee/peewee.py", line 1663, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "/Data/filmsoc/src/src/peewee/peewee.py", line 1420, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/Data/filmsoc/src/src/peewee/peewee.py", line 1824, in execute_sql
    res = cursor.execute(sql, params or ())
  File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 201, in execute
    self.errorhandler(self, exc, value)
  File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (2006, 'MySQL server has gone away')
```

I tried to switch MySQLdb to pyMySQL....But this doesn't help.
I tried to increase the timeout in my `/etc/mysql/my.cnf`, but this problem still happens.
FYI, I am using MySQL 5.5.32 and the Database is InnoDB. Both peewee 2.1.3 and 2.1.4 have this issue.
I wonder whether this is a configuration error (my fault)...Or maybe others are also facing this issue.


@johnmave126 -- are these errors happening with the test suite, or with your application?    You said requests so is this a web app?  How are you handling opening and closing the connection to the db?


@coleifer 
It is with my application.
I just now found some hints in it. I found when I set threadlocals to True, the problem is away.
In fact I am using Flask + flask-peewee, and before I didn't realize there is an option to set threadlocals.
I am now opening an issue in flask-peewee to add some hints in the docs.