0
votes

I am having a strange problem with jdbc and MySql, this is what i am doing: I open a java.sql.Connection, do a query, I do not close the connection for reuse purpose. I then update the same database using phpMyadmin,

when i come back to java, retrieving from the same database with the same unclosed connection gives me old results not the one that i updated. what could be the problem?

What i am not closing is only the connection, the rest, preparedStatement, resultSet are closed after each query.

4
probably your query result is cached and no new query is executed at second time. Can you give some more details about which driver is used and what is the configuration?ergineroglu
Is Auto commit disabled in your PhpMyAdmin tool? Have you checked?JSS
There is no commit for selecting. I am only selecting. the updating part is done in phpmyadmin. i am doing this purposefully since the database will be used by many people, if one person updates from another computer, the other should be able to see the change.Maposa Takalani

4 Answers

1
votes

May be your connection is not set to auto commit.

Check its status by calling con.getCommit(). It returns a boolean. If returned a false, you can set it explicit after creating the connection object.

con.setAutoCommit( true );

OR

After all DML operations, call con.commit()

0
votes

Your session seems not to be commited with the JDBC invocation. That means your changes are not applied to the db until you commit, and are stored in your local session.

Please provide some sample code for further investigation.

0
votes

try to implement JDBC Transaction see this example

HERE

0
votes

I got it.

java.sql.Connection c = ...;
c.setTransactionIsolation(java.sql.Connection.TRANSACTION_READ_COMMITTED);

This ensures that even when we are selecting during a transaction, we wil be able to select even results that are updated somewhere by a different machine.

Apache Docs