jdbcconnectionexception

mcvgt66p  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(193)

我面临这样的问题。当我发出很多api请求时,基函数停止响应并给出一个 JDBCConnectionException “Unable to acquire JDBC Connection” 错误。我试着增加 POOL Connections ,但没用。你能描述一下jpa是如何工作的吗?我是否正确理解,例如,如果max pool=2,那么通过调用一个线程从一个线程访问的用户接收到一个连接,那么当他访问两个存储库时,他接收到第二个连接?也就是说,当从一个线程访问两个存储库时,用户需要两个连接?或者怎么做?如果是这样,我们如何解决这个问题,使每个用户从一个线程只有一个连接?

class Controller {

  @Resource
  RepositoryOne one;
  @Resource
  RepositoryTwo two;

  /*
    This is a sample code!
    There may be syntax errors.
    I wrote in a notebook.
    The main question is how does returning
    two results from different repositories work?
  */

  String getResult() {
    // If I call this method VERY MANY TIMES!
    // For example ApacheBenchmark.
    // Then the method raises an error:
    // --------------------------------
    // JDBCConnectionException “Unable to acquire JDBC Connection”
    // --------------------------------
    return one.query() + two.query()
  }

  @Repository
  interface RepositoryOne extends JPA {
    @Query("SELECT `title` from `anything` where `something` = 0")
    String query();
  }

  @Repository
  interface RepositoryTwo extends JPA {
    @Query("SELECT `title` from `anything` where `something` = 0")
    String query();
  }

}
vwkv1x7d

vwkv1x7d1#

那么为什么会抛出jdbcconnectionexception呢?
默认情况下,hibernate使用其内部数据库连接池库。这意味着它保持数据库连接打开,以便以后重用。mysql数据库服务器对每个连接都有一个超时值(默认值为8小时或28800秒)。因此,如果一个连接的空闲时间超过这个超时值,它将被服务器丢弃。因此,当java数据库应用程序的空闲时间超过mysql服务器的连接超时值,并且最终用户再次尝试连接时,hibernate会重用服务器已经丢弃的空闲连接,因此会抛出jdbcconnectionexceptions。
可能的解决方案
hibernate.c3p0.timeout:池中保持空闲连接的秒数。如果一个连接的空闲时间超过这个超时值,那么它将被一个新的连接替换。
这意味着您必须将hibernate.c3p0.timeout的值设置为小于mysql服务器上的wait\u timeout值。这无疑解决了问题,因为池中没有空闲时间超过mysql服务器超时值的连接。
在这里阅读更多

相关问题