Created
April 3, 2014 21:53
-
-
Save ggiraldez/9963666 to your computer and use it in GitHub Desktop.
SQLite JDBC autoCommit race condition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class.forName('org.sqlite.JDBC') | |
import groovy.sql.Sql | |
import java.sql.* | |
def sql = Sql.newInstance('jdbc:sqlite::memory:', [:] as Properties, 'org.sqlite.JDBC') | |
sql.execute('drop table if exists test') | |
sql.execute('create table test (key integer primary key, value string)') | |
def connection = sql.connection | |
connection.autoCommit = false | |
def query = "insert into test (value) values ('foo')" | |
def lock = new Object() | |
def t = { -> | |
def count = 10000 | |
while (count-- > 0) { | |
//synchronized(lock) { | |
def statement = connection.prepareStatement(query); | |
statement.executeUpdate(); | |
Thread.sleep(1); | |
connection.commit(); | |
statement.close(); | |
//} | |
} | |
} | |
def t1 = Thread.start(t) | |
def t2 = Thread.start(t) | |
t1.join() | |
t2.join() | |
connection.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment