The following JUnit test show show how a simple foreign key constraint can
be violated when using MVCC with two concurrent connections. This was
verified on version 1.4.194. When MVCC is turned off the test behaves as
expected, i.e. at least of the commits will fail.
@Test
public void test() throws Exception {
JdbcDataSource ds = new JdbcDataSource();
ds.setUrl("jdbc:h2:mem:1;TRACE_LEVEL_FILE=4");
Connection conn = ds.getConnection();
conn.createStatement().execute("CREATE MEMORY TABLE TEST(ID BIGINT NOT
NULL);");
conn.createStatement().execute("CREATE MEMORY TABLE TEST2(ID BIGINT NOT
NULL, PARENT BIGINT NOT NULL);");
conn.createStatement().execute("ALTER TABLE TEST ADD CONSTRAINT PK PRIMARY
KEY(ID);");
conn.createStatement().execute("ALTER TABLE TEST2 ADD CONSTRAINT PK2
PRIMARY KEY(ID);");
conn.createStatement().execute("ALTER TABLE TEST2 ADD CONSTRAINT FK
FOREIGN KEY(PARENT) REFERENCES TEST(ID);");
conn.createStatement().execute("INSERT INTO TEST (ID) VALUES (1);");
Connection conn2 = ds.getConnection();
conn2.setAutoCommit(false);
Connection conn3 = ds.getConnection();
conn3.setAutoCommit(false);
conn3.createStatement().execute("INSERT INTO TEST2 (ID, PARENT) VALUES (1,
1);");
conn2.createStatement().execute("DELETE FROM TEST WHERE ID = 1;");
try {
conn3.commit();
conn2.commit();
fail("Foreign key constraint has been violated");
}
catch (Exception e) {}
}
--
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.