Last active
August 29, 2015 13:58
-
-
Save ggiraldez/9963373 to your computer and use it in GitHub Desktop.
SQLite JDBC getGeneratedKeys() 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 = 1000 | |
while (count-- > 0) { | |
//synchronized(lock) { | |
def statement = connection.prepareStatement(query); | |
statement.executeUpdate(); | |
def resultSet = statement.getGeneratedKeys(); | |
connection.commit(); | |
if (resultSet.next()) { | |
//println "${Thread.currentThread()} ${resultSet.getLong(1)}"; | |
} else { | |
println "${Thread.currentThread()} no result" | |
} | |
resultSet.close(); | |
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