【发布时间】:2017-10-27 13:26:05
【问题描述】:
我正在通过 JDBC 测试一些数据库驱动程序。其中一项测试是测试数据库的事务能力:
- 我打开了两个到同一个数据库的连接(autoCommit=false)。
- 在连接 A 上,我在表中插入一行而不执行提交。
- 在连接 B 上,我预计不会看到该行。
- 在连接 A 上,我执行了提交。
- 在连接 B 上,我希望看到该行。
该测试适用于 Oracle 12,但对于 HSQLDB、Derby 或 SQL Server 等其他数据库,否:它在第 3 步中间阻塞。
我猜原因可能与事务隔离参数有关,但我在创建两个连接时尝试了所有可能的值,结果始终相同。
为什么会阻塞?为什么Oracle不阻塞?
这是我的代码:
public class JdbcTest
{
@Test
public void twoTransactionsTest()
throws SQLException,
IOException
{
String tableName="dummy01";
// 1. I open two connections to the same db (with autoCommit=false).
try (Connection connectionA=createConnection(); Connection connectionB=createConnection())
{
createTable(connectionA, tableName);
// 2. On connection A, I insert one row in a table without performing a commit.
execute(connectionA, "INSERT INTO " + tableName + " VALUES(50, 'grecia')");
// 3. On connection B, I expect not to see that row yet.
int records=queryAndCountRows(connectionB, "SELECT id FROM " + tableName + " WHERE id=50");
assertEquals(0, records);
// 4. On connection A, I perform a commit.
connectionA.commit();
// 5. On connection B, I expect to see that row.
records=queryAndCountRows(connectionB, "SELECT * FROM " + tableName + " WHERE id=50");
assertEquals(1, records);
dropTable(connectionA, tableName);
}
}
private Connection createConnection()
throws SQLException,
IOException
{
String url="jdbc:hsqldb:demo.hsqldb";
String user="demo";
String password="";
Connection connection=DriverManager.getConnection(url, user, password);
connection.setAutoCommit(false);
return connection;
}
private int queryAndCountRows(Connection connection, String sql)
throws SQLException
{
try (PreparedStatement pst=connection.prepareStatement(sql))
{
try (ResultSet rs=pst.executeQuery())
{
int records=0;
while (rs.next())
{
records++;
}
return records;
}
}
}
private void execute(Connection connection, String sql)
throws SQLException
{
try (Statement statement=connection.createStatement())
{
statement.execute(sql);
}
}
private void createTable(Connection connection, String tableName)
throws SQLException
{
try
{
execute(connection, "DROP TABLE " + tableName);
}
catch (SQLException e)
{
// If the table already exists, let's ignore this error.
}
execute(connection, "CREATE TABLE " + tableName + "(id NUMBER(5) NOT NULL, name VARCHAR2(100))");
}
private void dropTable(Connection connection, String tableName)
throws SQLException
{
execute(connection, "DROP TABLE " + tableName);
}
}
我的依赖:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>
提前致谢。
【问题讨论】:
-
您是否尝试过:hsqldb.tx=mvcc ??看看这个:stackoverflow.com/questions/16108643/…
-
@ErichKitzmueller 感谢您的链接。这是迄今为止我找到的最好的答案。如果您将其发布为答案,我会选择它。
-
@LittleSanti 好的,完成。
标签: java sql-server oracle jdbc hsqldb