简体   繁体   中英

Tomcat doesn't sync with the mysql database

I'm currently working on a college project, and I'm creating a very simple e-commerce style website.

I'm using JDBC driver manager and connection pool for the connection to the db, while using Tomcat 9.0 as the container.

The problem is: when I modify some product through the website (let's say the amount available for example), the website doesn't always reflect the changes, while I can always see the data correctly in MySql Workbench.

It actually works one time out of two on the same query:

  1. I run the query for the first time after the changes -> it shows the old value
  2. I run the query for the second time after the changes -> it shows the new value
  3. I run the query for the third time after the changes -> it shows the old value

And so on.

I've already tried to set caching off (from the query, using the SQL_NO_CACHE), but it didn't seem to solve the problem, I've tried to use Datasource instead, but it causes other problems that most likely I won't have the time to solve.

This is the connection pool file, which I think might be problem, I'm not that sure tho:

public class DriverManagerConnectionPool  {

private static List<Connection> freeDbConnections;

static {
    freeDbConnections = new LinkedList<Connection>();
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        System.out.println("DB driver not found:"+ e.getMessage());
    } 
}

private static synchronized Connection createDBConnection() throws SQLException {
    Connection newConnection = null;
    String ip = "localhost";
    String port = "3306";
    String db = "storage";
    String username = "root";
    String password = "1234";

    newConnection = DriverManager.getConnection("jdbc:mysql://"+ ip+":"+ port+"/"+db+"?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", username, password);

    newConnection.setAutoCommit(false);
    return newConnection;
}


public static synchronized Connection getConnection() throws SQLException {
    Connection connection;

    if (!freeDbConnections.isEmpty()) {
        connection = (Connection) freeDbConnections.get(0);
        freeDbConnections.remove(0);

        try {
            if (connection.isClosed())
                connection = getConnection();
        } catch (SQLException e) {
            connection.close();
            connection = getConnection();
        }
    } else {
        connection = createDBConnection();      
    }

    return connection;
}

public static synchronized void releaseConnection(Connection connection) throws SQLException {
    if(connection != null) freeDbConnections.add(connection);
}

}

I really hope you can help me, I haven't found any solution online!

I guess it is because of auto-commit is disabled. Please try using @Transactional or set auto-commit to true. You can also try to use db.commit after each statement.

As per your connection pool implementation, all connection in your pool seems to be auto committed false.
Please check you have properly committed the connection after executing the query or not.
So it might be the case that, when executing the query after changes with same connection it reflects those changes, done earlier and on other connections, old values are might get returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM