database - tomcat服务器上使用连接池时的数据同步问题

标签 database tomcat servlets

我正在开发一个 Servlet 应用程序。它从Tomcat容器支持的连接池中获取一个数据库连接,用于查询和更新数据库数据。

我遇到了一个问题。 Servlet 获得一个数据库连接,然后添加一个新的表行或删除一个表行。之后,它提交更改。稍后,获得连接以执行查询。我发现使用第二个连接从查询返回的数据不反射(reflect)使用第一个数据库连接所做的更改。

是不是很奇怪?使用第一个数据库连接所做的更改已成功提交。为什么插入的新行没有出现在后面的查询中?为什么被删除的行在后面的查询中还会出现?

是否与交易级别的设置有关?

有人能帮忙吗?

03-12: More Information (#1):

<醇>
  • 我使用 MySQL Community Server 5.6。
  • 我的 servlet 在 Tomcat 7.0.41.0 上运行。
  • conf/server.xml中的Resource元素如下:

    <Resource type="javax.sql.DataSource"
           name="jdbc/storewscloud"
           factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
           driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/myappdb"
       maxActive="100"
            minIdle="10"
            maxWait="10000"
            initialSize="10"
            removeAbandonedTimeout="60"
           removeAbandoned="true"
           logAbandoned="true"
           username="root"
           password="xxxxxxxxxx"
    /></li>
    

  • I do not use any cache explicitly.
  • Every time the servlet gets a database connection, it turns the auto-commit mode of the connection off.
  • When the servlet is invoked, a database connection is obtained. The servet uses it to update data in the database. After that, it commits the changes. Then, it uses Apache HttpClients to invoke the same servlet to do some other thing which also obtains a database connection and execute query. The later query returns 'old' data. If I refresh the web page, the latest data are shown. It looks like some party, mysql jdbc driver or connection object, cache the data somewhere. I have no clue.
  • 03-12:更多信息(#2): 我做了一个实验,在不使用连接池的情况下获得连接。结果是正确的。所以,问题出在连接池上。

    要使用池中的第二个连接使查询返回正确的数据,我不仅需要使用池中的第一个连接提交数据更改,还需要关闭第一个连接。

    即使调用了 commit(),似乎所做的数据更改并没有完全保存在数据库中,直到调用了 close()。

    为什么?

    最佳答案

    我发现最近发布了新版本的C3P0连接池。我试了一下。有用!我遇到的问题没有发生。因此,我用它来代替Tomcat服务器的捆绑连接池。对于那些和我遇到同样问题的人,C3P0 也许也是适合你的解决方案。

    C3P0 Project URL

    关于database - tomcat服务器上使用连接池时的数据同步问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22325173/

    相关文章:

    tomcat - apache tomcat 监听器端口的范围是多少?

    java - 持久对象异常 : detached entity passed to persist - Got this exception when run method 2nd time

    java - 使用Spring和maven的Web应用程序不起作用

    javascript - 如何从 tomcat 服务器重定向到 google?

    Java Servlet 静态类

    mysql - 如何使用 Peewee 将 Jupyter Notebook 连接到远程 MySQL 数据库?

    PHP MySQL 数据库列引用

    mysql - 未知数量输入的数据库设计

    database - 如何将静态数据放入 Android 中的 SQLite 数据库?

    java - 有没有等同于@Startup的spring?