Bug #14378 Select Query always returns cached result even if cache is disabled
Submitted: 27 Oct 2005 8:31 Modified: 27 Oct 2005 12:24
Reporter: Ghadi Rayess Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / J Severity:S1 (Critical)
Version:mysql-max-4.1.12-sun-solaris2.8-sparc-64 OS:SunOS 5.8 sparc
Assigned to: CPU Architecture:Any

[27 Oct 2005 8:31] Ghadi Rayess
Description:
The Select query from a JDBC connection always returns the cached result even if cache was disabled at the server with query_cache_size = 0.

This senario occurs with innodb engine. When MyISAM tables are used the Select Query is consistent with the actualy data in the tables. No further tests were made with other types of engine.

An example of the problem is described below.

Suppost a Database called DB_TEST contains one table "SERVICE" as following:
CREATE TABLE SERVICE(
	SERVICE_ID		 INTEGER 		NOT NULL,
	SERVICE_NAME		 VARCHAR(25) 	        NOT NULL,
	PRIMARY KEY (SERVICE_ID)
	)ENGINE=InnoDB

Then the table was populated with the following data:
INSERT INTO SERVICE VALUES(1,'SERVICE_1');
INSERT INTO SERVICE VALUES(2,'SERVICE_2');

There are two JAVA applications that performs queries and updates on this table, Let's say Application-A and Application-B.

The following scenario will produce false data:

STEP 1 : Application-A performs: "SELECT * FROM SERVICE". the result should be
             +------------+---------------+
             | SERVICE_ID | SERVICE_NAME|
             +------------+---------------+
             |          1      | SERVICE_1      |
             |          2      | SERVICE_2      |
             +------------+---------------+
STEP 2 : Application-B performs: "INSERT INTO SERVICE VALUES(3,'SERVICE_3')" 

STEP 3: Application-A performs the command: "SELECT * FROM SERVICE" --> the result returned is the same one as at STEP 1, 
             +------------+---------------+
             | SERVICE_ID | SERVICE_NAME|
             +------------+---------------+
             |          1      | SERVICE_1      |
             |          2      | SERVICE_2      |
             +------------+---------------+
which is inconsistent with the actual data in the table...

I'm connection to the Database using a JDBC connection as following:

 try {
         Class.forName("com.mysql.jdbc.Driver").newInstance();
       }
       catch (Exception ex2) {
        ....
       }
       try {
         con = DriverManager.getConnection(jdbc:mysql://localhost:3306/DB_TEST?user=USR&password=PASWD);
         }

Note that if the engine and the tables were MyISAM the problem does not occur.
       catch (SQLException ex1) {
          ....
       }

How to repeat:
1 - Start mysql with innodb as default engine
2 - Create a database: DB_TEST for example
3 - Create a table: SERVICE for example
4 - Develop two java Applications that connects to this database.
5 - Implement the scenario described in the Description section.

Suggested fix:
Have an option to completely turn off Cache with a JDBC connection
[27 Oct 2005 12:24] Heikki Tuuri
Hi!

If you do not commit your SELECT and INSERT transactions, then this is the expected behavior of the consistent read.

Please look at:

http://dev.mysql.com/doc/refman/5.0/en/innodb-consistent-read.html

Regards,

Heikki
[27 Oct 2005 12:45] Vasily Kishkin
Test on Windows. All works fine.