Not sure why I am getting this error in my logs. This error happens every so often, but not consistently, and I'm not sure why. Here's my code:
require 'rubygems'
require 'sinatra'
require 'data_mapper'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite3::memory:')
class LevelStatus
include DataMapper::Resource
property :id, Serial
property :italian, Float
property :hairbender, Float
property :decaf, Float
end
DataMapper.finalize
LevelStatus.auto_migrate!
post '/update-levels' do
@status = LevelStatus.create(
:italian => params[:italian],
:hairbender => params[:hairbender],
:decaf => params[:decaf]
)
status 200
end
When I POST
to /update-levels
sometimes I get the message:
DataObjects::SyntaxError - no such table: level_statuses
What is wrong with my code that it is causing this error?
As matt said in a comment above, this answer seems to explain the problem:
The problem is, I suspect, due to the thread pooling which DataMapper (or more accurately, DataObjects, the database driver DataMapper uses) does automatically. The database connection isn't shared between threads. This is fine (and beneficial, even) for something like postgresql or mysql or even sqlite3 as a 'file-backed' database. In the case of sqlite3's in memory store, the connection is the database. So additional threads will fail for that reason. Also, after a period of inactivity (~1 min?), the thread will get scavenged and the database will go away too.
If it is this, I'm not sure there's an easy work around. You might be able to modify do_sqlite3 to avoid this. Another alternative that should be basically as fast, is to use a file-backed sqlite3 DB on a ramdrive.