Java MYSQL/JDBC query is returning stale data from cached Connection
NickName:ricosrealm Ask DateTime:2012-02-17T11:57:21

Java MYSQL/JDBC query is returning stale data from cached Connection

I've been searching Stackoverflow for an answer but can't seem to find one that doesn't involve Hibernate or some other database wrapper.

I'm using JDBC directly via the MYSQL 5.18 JDBC driver in a Tomcat 6 Java EE app. I am caching Connection objects, but not caching Statement objects. The ResultSets for the query are correctly returning up to date data on the first run. When I change a few rows via PHPMyAdmin or some other external tool, rerun the query, I get stale out-of-date data.

I'm using normal Statements, not PreparedStatements. I've tried ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE. I'm also closing out the result set. These do not solve the problem. I've also tried ResultSet.refreshRows(), but that results in an error because the query has a JOIN clause.

The only thing that clearly solves the problem is closing the Connection and reconnecting to the database, which results in a heavy cost for each query attempt.

Is there a way to reuse Connections without returning stale data?

EDIT: I'm not using transactions for queries at the moment.

Here's the general code.

Connection conn; //created elsewhere and reused
...

String query = "SELECT p.ID as oid,rid,handle,summary,city,state,zip,t.name AS category     
                FROM profiles AS p
                JOIN (terms AS t) ON (p.tid = t.ID)
                WHERE p.ID = 1";

ResultSet resultSet;
Statement s;
synchronized (conn)
{                            
   s = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                           ResultSet.CONCUR_UPDATABLE);                        
   resultSet = s.executeQuery(query);
}

//Iterate over the results using .next() and copy data to another data structure   
List retval = getResults(resultSet);
s.close();

Thanks for the help in advance!

Copyright Notice:Content Author:「ricosrealm」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/9322673/java-mysql-jdbc-query-is-returning-stale-data-from-cached-connection

Answers
ricosrealm 2012-02-17T05:53:00

Turns out it was a matter of uncommited queries. Thanks to Brent Worden for the question about transactions which led me to look around and notice that I had disabled auto commit and was not committing after queries.\n\nSo the solutions that worked for me:\n\nconn.setAutoCommit(true);\n\n\nor\n\nstatement.executeQuery(query);\nconn.commit();\n\n\nThis allows the queries to be flushed out and stale data is prevented.",


Gurushanth 2014-02-15T11:57:50

Set the transaction isolation level as below.\nconn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);",


Chonan 2015-12-10T10:01:42

my mysql installation: ENGINE=InnoDB, default tx_isolation=REPEATABLE_READ\n\nspring.xml\n\n<tx:method name=\"find*\" propagation=\"SUPPORTS\" read-only=\"true\" timeout=\"600\" />\n\n\nif use pooled connection which will always return same results!\n\nchange mysql tx_isolation=READ_COMMITTED resolved my problem.",


More about “Java MYSQL/JDBC query is returning stale data from cached Connection” related questions

Java MYSQL/JDBC query is returning stale data from cached Connection

I've been searching Stackoverflow for an answer but can't seem to find one that doesn't involve Hibernate or some other database wrapper. I'm using JDBC directly via the MYSQL 5.18 JDBC driver in a

Show Detail

Spring Jdbc template setAutocommit(false)

I want insert multiple rows in db table. I am using SpringJdbc. How can i manage transaction in SpringJdbc connection. My code is: @Override @Transactional(propagation = Propagation.REQUIRED, isol...

Show Detail

JDBC MYSQL connection issue

I am finally able to create a simple java program through console and notepad which connects to MYSQL database. But when i access the database using conn = DriverManager.getConnection(DB_URL,USE...

Show Detail

How to avoid stale MySQL/Hibernate connections (MySQLNonTransientConnectionException)

I have a Java webapp using Hibernate and MySQL. If the site isn't used for a few days, the MySQL connection goes stale, and I am met with the following exception: com.mysql.jdbc.exceptions.jdbc4.

Show Detail

mysqli query returning false, apparently because the connection is becoming stale

I've inherited a body of PHP 7.2 code that uses the mysqli library to access two databases; one of them is in an Azure instance. The junior developer who wrote this lot reckons that the connection

Show Detail

Unable to make JDBC Connection to jdbc:google:mysql

I started learning about the google cloud platform and I have been able to create a project and an instance and then I created a db using a mysql dump file. Now, I have this application which was c...

Show Detail

JDBC connection to remote MySql fails

In glassfish(3.1.2) I have created a JDBC Connection Pool (and JDBC Resource) to a remote MySql database. When a do a ping test in glassfish it is successful. My Java EE 6 app is using that persis...

Show Detail

Connection to MySQL with JDBC

I have been trying to connect to my external MySQL server location at http://mysql10.000webhost.com using JDBC in eclipse, but I keep getting an error. Here is my code: import java.sql.Connect...

Show Detail

Java JDBC connection to MySQL

I am trying to connect to mysql from java web application in eclipse. Connection con = null; try { //DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Class.forName("com.mysq...

Show Detail

New EntityManager sometimes getting stale data from MySQL

JPA; Hibernate 4.3.6; MySQL 6.2 (InnoDB); Vaadin web application running on Tomcat When I read an entity from the database I sometimes get stale data. I can’t find the pattern— sometimes the same ...

Show Detail