Skip to content

Instantly share code, notes, and snippets.

@krists
Created March 10, 2019 12:55
Show Gist options
  • Save krists/699e6ae439886f3831df7bdb088375ea to your computer and use it in GitHub Desktop.
Save krists/699e6ae439886f3831df7bdb088375ea to your computer and use it in GitHub Desktop.
require 'bundler/inline'
require "thread"
require "tempfile"
gemfile do
source 'https://rubygems.org'
gem 'sqlite3', '1.3.13'
gem 'pry'
end
puts 'Gems installed and loaded!'
puts "The SQLite3 version #{SQLite3::SQLITE_VERSION}"
begin
database_path = Tempfile.open("testdb")
db = SQLite3::Database.new(database_path.path)
db.transaction do
db.execute("CREATE TABLE `test_1` (connection_role VARCHAR (255))")
db.execute("INSERT INTO test_1 VALUES ('main-1')")
end
thread = Thread.new do
begin
thread_db = SQLite3::Database.new(database_path.path)
thread_db.transaction(:immediate) do
puts "puts:bg:1 #{thread_db.execute("SELECT * FROM test_1")}"
thread_db.execute("INSERT INTO test_1 VALUES ('bg-1')")
puts "puts:bg:2 #{thread_db.execute("SELECT * FROM test_1")}"
sleep 2
thread_db.execute("INSERT INTO test_1 VALUES ('bg-2')")
puts "puts:bg:3 #{thread_db.execute("SELECT * FROM test_1")}"
end
puts "puts:bg:4 transaction closed"
ensure
thread_db.close if thread_db
end
end
puts "puts:main:1 #{db.execute("SELECT * FROM test_1")}"
sleep 1
puts "puts:main:2 #{db.execute("SELECT * FROM test_1")}"
db.busy_timeout 3000 # <-- raises: .rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108:in `step': database is locked (SQLite3::BusyException)
# busy_handler works as expected
# db.busy_handler do
# true
# end
puts "puts:main:3 #{db.execute("SELECT * FROM test_1")}"
db.execute("INSERT INTO test_1 VALUES ('main-2')")
puts "puts:main:4 #{db.execute("SELECT * FROM test_1")}"
thread.join
ensure
database_path.close
database_path.unlink
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment