【问题标题】:Could not open JPA Entity Manager无法打开 JPA 实体管理器
【发布时间】:2019-08-25 08:43:29
【问题描述】:

我正在使用将信息存储在数据库中的 Spring BOOT 应用程序。我收到错误:

org.springframework.transaction.CannotCreateTransactionException: 无法为事务打开 JPA EntityManager

我尝试在 INTERNET 上搜索,但找不到任何答案。当我重新启动我的 jar 时,它工作正常 - 不会出现错误。这种情况每天都会发生,我必须每天重新启动 jar。

例外: org.springframework.transaction.CannotCreateTransactionException:无法为事务打开 JPA EntityManager;嵌套异常是 javax.persistence.PersistenceException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 从服务器成功接收的最后一个数据包是 44,382,666 毫秒前。最后一个成功发送到服务器的数据包是在 44,382,666 毫秒前。比服务器配置的“wait_timeout”值长。您应该考虑在应用程序中使用之前使连接失效和/或测试连接有效性,增加客户端超时的服务器配置值,或使用 Connector/J 连接属性“autoReconnect=true”来避免此问题。

计划每天运行一次,每十分钟运行一次。

我已尝试缩短时间段 - 计划在特定时间运行,但仍然没有成功。

还尝试了 autoReconnect=true,在 db url 中提供了端口号。

【问题讨论】:

    标签: java mysql spring-boot


    【解决方案1】:

    好像this is the answer

    spring.datasource.testOnBorrow=true
    spring.datasource.testWhileIdle = true
    spring.datasource.timeBetweenEvictionRunsMillis = 3600000
    spring.datasource.validationQuery=SELECT 1
    

    【讨论】:

    • 我使用两个数据源,一个用于读取,第二个用于写入。我正在使用以下属性。
    • 用于阅读:spring.datasource.url=jdbc:mysql://ReadingDBIP/DBNAME?autoReconnect=true, spring.datasource.driver-class-name=com.mysql.jdbc.Driver, spring .datasource.tomcat.test-while-idle=true,
    • spring.datasource.tomcat.test-on-borrow=true, spring.datasource.tomcat.validation-query=SELECT 1, spring.datasource.tomcat.maxActive=100, spring.datasource.tomcat .maxIdle=10, spring.datasource.tomcat.minIdle=1, spring.datasource.tomcat.maxWait=60000, spring.datasource.tomcat.initial-size=10
    • 用于写作:spring.secondDatasource.url=jdbc:mysql://WrtingDBIP/DBNAME?autoReconnect=true, spring.secondDatasource.driver-class-name=com.mysql.jdbc.Driver, spring .secondDatasource.tomcat.test-while-idle=true, spring.secondDatasource.tomcat.test-on-borrow=true, spring.secondDatasource.tomcat.validation-query=SELECT 1, spring.secondDatasource.tomcat.maxActive=100, spring.secondDatasource.tomcat.maxIdle=10, spring.secondDatasource.tomcat.minIdle=1, spring.secondDatasource.tomcat.maxWait=60000, spring.secondDatasource.tomcat.initial-size=10
    • 我应该使用 apache dbcp2 吗?
    猜你喜欢
    • 1970-01-01
    • 2020-01-16
    • 2016-02-10
    • 1970-01-01
    • 2013-12-11
    • 2015-06-22
    • 2014-09-06
    • 2016-03-17
    相关资源
    最近更新 更多