Problem:
It seems that records which are inserted within a transaction Tx1 (Isolation=read committed) can be read by another transaction Tx2 (Isolation=read committed) during running transaction Tx1.
Problem replication:
An insert thread is continuously inserting one record into TABLE1 and one record into TABLE2 with the same primary key values within one transaction (Isolation=read committed).
A select thread is continuously executing the select statement within a transaction (Isolation=read committed).
9 insert threads and 1 select thread are running parallel. The execution of all threads stops, when the result of the select statement is not empty.
The result of the select statement should by always empty, but isn’t. Why?
Finally both tables contain the same records.
Table1:
CREATE TABLE TABLE1 (
ID_TRACTYPE__VALUE NCHAR(3) NOT NULL,
ID_TRACID__VALUE BIGINT NOT NULL,
RECORDINFO_VERSIONNR__VALUE BIGINT NOT NULL,
-- Primary Key
PRIMARY KEY(ID_TRACTYPE__VALUE,ID_TRACID__VALUE,RECORDINFO_VERSIONNR__VALUE)
WITH(ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = OFF)
);
Table2:
CREATE TABLE TABLE2 (
ID_TRACTYPE__VALUE NCHAR(3) NOT NULL,
ID_TRACID__VALUE BIGINT NOT NULL,
RECORDINFO_VERSIONNR__VALUE BIGINT NOT NULL,
-- Primary Key
PRIMARY KEY(ID_TRACTYPE__VALUE,ID_TRACID__VALUE,RECORDINFO_VERSIONNR__VALUE)
WITH(ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = OFF)
);
Insert statement examples:
INSERT INTO TABLE1 (ROWLOCK) (ID_TRACTYPE__VALUE, RECORDINFO_VERSIONNR__VALUE,ID_TRACID__VALUE) VALUES ('TO1',1,2)
INSERT INTO TABLE2 (ROWLOCK) (ID_TRACTYPE__VALUE, RECORDINFO_VERSIONNR__VALUE,ID_TRACID__VALUE) VALUES ('TO1',1,2)
Select statement:
SELECT ID_TRACTYPE__VALUE, RECORDINFO_VERSIONNR__VALUE, ID_TRACID__VALUE FROM TABLE2 AS A WITH(UPDLOCK, ROWLOCK) WHERE NOT EXISTS (SELECT * FROM TABLE1 WHERE ID_TRACID__VALUE = A.ID_TRACID__VALUE AND ID_TRACTYPE__VALUE = A.ID_TRACTYPE__VALUE AND RECORDINFO_VERSIONNR__VALUE = A.RECORDINFO_VERSIONNR__VALUE)
Runtime environment:
SQL Server 2008:
Microsoft SQL Server Management Studio 10.0.5512.0
Microsoft Analysis Services Client Tools 10.0.5500.0
Microsoft Data Access Components (MDAC) 6.0.6002.18005
Microsoft MSXML 3.0 6.0
Microsoft Internet Explorer 8.0.6001.19507
Microsoft .NET Framework 2.0.50727.4247
Operating System 6.0.6002
Database access by Java 1.6 with JDBC driver version 4.0.2206.100.
jdbc:sqlserver://DBSERVER:1433;authenticationScheme=nativeAuthentication;xopenStates=false;sendTimeAsDatetime=true;trustServerCertificate=false;sendStringParametersAsUnicode=true;selectMethod=direct;responseBuffering=adaptive;packetSize=8000;multiSubnetFailover=false;loginTimeout=15;lockTimeout=3000;lastUpdateCount=true;encrypt=false;disableStatementPooling=true;databaseName=DATABASE;applicationName=Microsoft JDBC Driver for SQL Server;applicationIntent=readwrite;, UserName=JDBCUSER, Microsoft JDBC Driver 4.0 for SQL Server