提交 fdb4eb02 authored 作者: noelgrandin's avatar noelgrandin

fix deadlock in concurrent LOB update

上级 3c754151
......@@ -18,7 +18,7 @@ Change Log
<h1>Change Log</h1>
<h2>Next Version (unreleased)</h2>
<ul><li>-
<ul><li>Fix deadlock when updating LOB's concurrently. See TestLob#testDeadlock2()
</li></ul>
<h2>Version 1.3.172 (2013-05-25)</h2>
......
......@@ -2356,7 +2356,7 @@ public class Database implements DataHandler {
}
@Override
public Connection getLobConnection() {
public JdbcConnection getLobConnection() {
String url = Constants.CONN_URL_INTERNAL;
JdbcConnection conn = new JdbcConnection(systemSession, systemUser.getName(), url);
conn.setTraceLevel(TraceSystem.OFF);
......
......@@ -9,6 +9,7 @@ package org.h2.test.db;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
......@@ -64,6 +65,7 @@ public class TestLob extends TestBase {
testBlobInputStreamSeek(true);
testBlobInputStreamSeek(false);
testDeadlock();
testDeadlock2();
testCopyManyLobs();
testCopyLob();
testConcurrentCreate();
......@@ -275,6 +277,103 @@ public class TestLob extends TestBase {
conn2.close();
}
private final class Deadlock2Task1 extends Task {
public final Connection conn;
private Deadlock2Task1() throws SQLException {
this.conn = getDeadlock2Connection();
}
@Override
public void call() throws Exception {
Random random = new Random();
Statement stat = conn.createStatement();
char[] tmp = new char[1024];
while (!stop) {
try {
ResultSet rs = stat.executeQuery("select name from test where id = " + random.nextInt(999));
if (rs.next()) {
Reader r = rs.getClob("name").getCharacterStream();
while ( r.read(tmp) > 0) {}
r.close();
}
rs.close();
} catch (SQLException ex) {
// ignore "LOB gone away", this can happen in the presence of concurrent updates
if (ex.getErrorCode() != ErrorCode.IO_EXCEPTION_2) {
ex.printStackTrace();
}
} catch (IOException ex) {
// ignore "LOB gone away", this can happen in the presence of concurrent updates
if (!(ex.getCause() instanceof SQLException)) {
ex.printStackTrace();
}
SQLException ex2 = (SQLException) ex.getCause();
if (ex2.getErrorCode() != 90028) {
ex.printStackTrace();
}
}
}
}
}
private final class Deadlock2Task2 extends Task {
public final Connection conn;
private Deadlock2Task2() throws SQLException {
this.conn = getDeadlock2Connection();
}
@Override
public void call() throws Exception {
Random random = new Random();
Statement stat = conn.createStatement();
while (!stop) {
stat.execute("update test set counter = " + random.nextInt(10) + " where id = " + random.nextInt(1000));
}
}
}
private void testDeadlock2() throws Exception {
deleteDb("lob");
Connection conn = getDeadlock2Connection();
Statement stat = conn.createStatement();
stat.execute("create cached table test(id int not null identity, name clob, counter int)");
stat.execute("insert into test(id, name) select x, space(100000) from system_range(1, 1000)");
Deadlock2Task1 task1 = new Deadlock2Task1();
Deadlock2Task1 task2 = new Deadlock2Task1();
Deadlock2Task1 task3 = new Deadlock2Task1();
Deadlock2Task1 task4 = new Deadlock2Task1();
Deadlock2Task2 task5 = new Deadlock2Task2();
Deadlock2Task2 task6 = new Deadlock2Task2();
task1.execute();
task2.execute();
task3.execute();
task4.execute();
task5.execute();
task6.execute();
for (int i = 0; i < 1000; i++) {
stat.execute("insert into test values(null, space(10000 + " + i + "), 1)");
}
task1.get();
task1.conn.close();
task2.get();
task2.conn.close();
task3.get();
task3.conn.close();
task4.get();
task4.conn.close();
task5.get();
task5.conn.close();
task6.get();
task6.conn.close();
conn.close();
}
private Connection getDeadlock2Connection() throws SQLException {
return getConnection("lob;MULTI_THREADED=TRUE;LOCK_TIMEOUT=60000");
}
private void testCopyManyLobs() throws Exception {
deleteDb("lob");
Connection conn = getConnection("lob");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论