简体   繁体   English

Tomcat 不与 mysql 数据库同步

[英]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.我使用 JDBC 驱动程序管理器和连接池连接到数据库,同时使用 Tomcat 9.0 作为容器。

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.问题是:当我通过网站修改某些产品时(比如说可用的数量),网站并不总是反映更改,而我总是可以在 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.我已经尝试关闭缓存(从查询中,使用 SQL_NO_CACHE),但它似乎并没有解决问题,我尝试使用 Datasource,但它会导致其他问题,很可能我不会没时间解决。

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.请尝试使用@Transactional 或将自动提交设置为true。 You can also try to use db.commit after each statement.您也可以尝试在每个语句之后使用 db.commit。

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.因此,在使用相同连接进行更改后执行查询时,它可能会反映这些更改,较早完成并在其他连接上完成,可能会返回旧值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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