spring mysql JPA使用autoReconnect=true不生效

博客内容讲述了在项目中由MyBatis切换到JPA时遇到的数据库连接超时问题。尽管URL中配置了'autoReconnect=true',但JPA默认的'test-on-borrow'配置为false导致该问题。解决方案是为JPA配置'test-on-borrow'以确保连接在使用前进行健全性检查,避免连接失效。这强调了数据库连接池配置在不同框架间的重要性。
部署运行你感兴趣的模型镜像

项目里原先使用的是mybatis
使用的mysql配置是:
url: jdbc:mysql:/xxxx:2400/documentary_test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
表面看着没有问题,但是JPA使用改配置,使用一段时间以后出现如下报错
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 63,313,144 milliseconds ago. The last packet sent successfully to the server was 63,313,144 milliseconds ago. is longer than the server configured value of ‘wait_timeout’. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property ‘autoReconnect=true’ to avoid this problem.
但是上述链接URL中已经配置过了autoReconnect=true
最终原因是: mybatis默认配置
test-on-borrow= true
JPA默认配置
test-on-borrow= false
所以mybatis可以不用配置,但是JPA必须配置
test-on-borrow
它确保(尽最大可能)在从池中返回连接供您使用之前,已进行了基本的健全性检查,以确保客户端和db-server处于通话状态。

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

server: port: 8080 # 指定服务端口为8080 spring: data: mongodb: uri: mongodb://192.168.1.88:27017/cobot datasource: url: jdbc:mysql://192.168.1.88:3307/zrzk-code-analysis?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true # 替换为你的MySQL数据库地址和库名 username: root password: letmego driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:mapper/*.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zrzk.security</groupId> <artifactId>Mongodb-MySQL</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <properties> <maven.compiler.source>21</maven.compiler.source> <maven.compiler.target>21</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring-boot.version>3.2.0</spring-boot.version> </properties> <dependencies> <!-- Spring Boot 核心 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MongoDB 驱动 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> <version>3.5.6</version> </dependency> <!-- MySQL 驱动 + MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <!-- Lombok 简化实体类 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.30</version> </dependency> <!-- Spring Boot 测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <configuration> <!-- <excludes>--> <!-- <exclude>--> <!-- <groupId>org.projectlombok</groupId>--> <!-- <artifactId>lombok</artifactId>--> <!-- </exclude>--> <!-- </excludes>--> </configuration> </plugin> </plugins> </build> </project> D:\graalvm-jdk-21.0.6+8.1\bin\java.exe -XX:TieredStopAtLevel=1 -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-Dmanagement.endpoints.jmx.exposure.include=*" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.4.1\lib\idea_rt.jar=51961" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\svn-workspace\Mongodb-MySQL\target\classes;D:\maven\maven-repository\org\springframework\boot\spring-boot-starter-web\3.2.0\spring-boot-starter-web-3.2.0.jar;D:\maven\maven-repository\org\springframework\boot\spring-boot-starter\3.2.0\spring-boot-starter-3.2.0.jar;D:\maven\maven-repository\org\springframework\boot\spring-boot\3.2.0\spring-boot-3.2.0.jar;D:\maven\maven-repository\org\springframework\boot\spring-boot-autoconfigure\3.2.0\spring-boot-autoconfigure-3.2.0.jar;D:\maven\maven-repository\org\springframework\boot\spring-boot-starter-logging\3.2.0\spring-boot-starter-logging-3.2.0.jar;D:\maven\maven-repository\ch\qos\logback\logback-classic\1.4.11\logback-classic-1.4.11.jar;D:\maven\maven-repository\ch\qos\logback\logback-core\1.4.11\logback-core-1.4.11.jar;D:\maven\maven-repository\org\apache\logging\log4j\log4j-to-slf4j\2.21.1\log4j-to-slf4j-2.21.1.jar;D:\maven\maven-repository\org\apache\logging\log4j\log4j-api\2.21.1\log4j-api-2.21.1.jar;D:\maven\maven-repository\org\slf4j\jul-to-slf4j\2.0.9\jul-to-slf4j-2.0.9.jar;D:\maven\maven-repository\jakarta\annotation\jakarta.annotation-api\2.1.1\jakarta.annotation-api-2.1.1.jar;D:\maven\maven-repository\org\yaml\snakeyaml\2.2\snakeyaml-2.2.jar;D:\maven\maven-repository\org\springframework\boot\spring-boot-starter-json\3.2.0\spring-boot-starter-json-3.2.0.jar;D:\maven\maven-repository\com\fasterxml\jackson\core\jackson-databind\2.15.3\jackson-databind-2.15.3.jar;D:\maven\maven-repository\com\fasterxml\jackson\core\jackson-annotations\2.15.3\jackson-annotations-2.15.3.jar;D:\maven\maven-repository\com\fasterxml\jackson\core\jackson-core\2.15.3\jackson-core-2.15.3.jar;D:\maven\maven-repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.15.3\jackson-datatype-jdk8-2.15.3.jar;D:\maven\maven-repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.15.3\jackson-datatype-jsr310-2.15.3.jar;D:\maven\maven-repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.15.3\jackson-module-parameter-names-2.15.3.jar;D:\maven\maven-repository\org\springframework\boot\spring-boot-starter-tomcat\3.2.0\spring-boot-starter-tomcat-3.2.0.jar;D:\maven\maven-repository\org\apache\tomcat\embed\tomcat-embed-core\10.1.16\tomcat-embed-core-10.1.16.jar;D:\maven\maven-repository\org\apache\tomcat\embed\tomcat-embed-el\10.1.16\tomcat-embed-el-10.1.16.jar;D:\maven\maven-repository\org\apache\tomcat\embed\tomcat-embed-websocket\10.1.16\tomcat-embed-websocket-10.1.16.jar;D:\maven\maven-repository\org\springframework\spring-web\6.1.1\spring-web-6.1.1.jar;D:\maven\maven-repository\org\springframework\spring-beans\6.1.1\spring-beans-6.1.1.jar;D:\maven\maven-repository\io\micrometer\micrometer-observation\1.12.0\micrometer-observation-1.12.0.jar;D:\maven\maven-repository\io\micrometer\micrometer-commons\1.12.0\micrometer-commons-1.12.0.jar;D:\maven\maven-repository\org\springframework\spring-webmvc\6.1.1\spring-webmvc-6.1.1.jar;D:\maven\maven-repository\org\springframework\spring-aop\6.1.1\spring-aop-6.1.1.jar;D:\maven\maven-repository\org\springframework\spring-context\6.1.1\spring-context-6.1.1.jar;D:\maven\maven-repository\org\springframework\spring-expression\6.1.1\spring-expression-6.1.1.jar;D:\maven\maven-repository\org\springframework\boot\spring-boot-starter-data-mongodb\3.5.6\spring-boot-starter-data-mongodb-3.5.6.jar;D:\maven\maven-repository\org\mongodb\mongodb-driver-sync\4.11.1\mongodb-driver-sync-4.11.1.jar;D:\maven\maven-repository\org\mongodb\bson\4.11.1\bson-4.11.1.jar;D:\maven\maven-repository\org\mongodb\mongodb-driver-core\4.11.1\mongodb-driver-core-4.11.1.jar;D:\maven\maven-repository\org\mongodb\bson-record-codec\4.11.1\bson-record-codec-4.11.1.jar;D:\maven\maven-repository\org\springframework\data\spring-data-mongodb\4.2.0\spring-data-mongodb-4.2.0.jar;D:\maven\maven-repository\org\springframework\spring-tx\6.1.1\spring-tx-6.1.1.jar;D:\maven\maven-repository\org\springframework\data\spring-data-commons\3.2.0\spring-data-commons-3.2.0.jar;D:\maven\maven-repository\org\slf4j\slf4j-api\2.0.9\slf4j-api-2.0.9.jar;D:\maven\maven-repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\3.0.5\mybatis-spring-boot-starter-3.0.5.jar;D:\maven\maven-repository\org\springframework\boot\spring-boot-starter-jdbc\3.2.0\spring-boot-starter-jdbc-3.2.0.jar;D:\maven\maven-repository\com\zaxxer\HikariCP\5.0.1\HikariCP-5.0.1.jar;D:\maven\maven-repository\org\springframework\spring-jdbc\6.1.1\spring-jdbc-6.1.1.jar;D:\maven\maven-repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\3.0.5\mybatis-spring-boot-autoconfigure-3.0.5.jar;D:\maven\maven-repository\org\mybatis\mybatis\3.5.19\mybatis-3.5.19.jar;D:\maven\maven-repository\org\mybatis\mybatis-spring\3.0.5\mybatis-spring-3.0.5.jar;D:\maven\maven-repository\com\mysql\mysql-connector-j\8.0.33\mysql-connector-j-8.0.33.jar;D:\maven\maven-repository\org\projectlombok\lombok\1.18.30\lombok-1.18.30.jar;D:\maven\maven-repository\org\springframework\spring-core\6.1.1\spring-core-6.1.1.jar;D:\maven\maven-repository\org\springframework\spring-jcl\6.1.1\spring-jcl-6.1.1.jar com.jsy.MongodbMysqlApplication . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.2.0) 2025-10-21T17:57:55.076+08:00 INFO 568 --- [ main] com.jsy.MongodbMysqlApplication : Starting MongodbMysqlApplication using Java 21.0.6 with PID 568 (D:\svn-workspace\Mongodb-MySQL\target\classes started by 123 in D:\svn-workspace\Mongodb-MySQL) 2025-10-21T17:57:55.080+08:00 INFO 568 --- [ main] com.jsy.MongodbMysqlApplication : No active profile set, falling back to 1 default profile: "default" 2025-10-21T17:57:56.652+08:00 INFO 568 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode. 2025-10-21T17:57:56.740+08:00 INFO 568 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 81 ms. Found 1 MongoDB repository interface. 2025-10-21T17:57:57.707+08:00 INFO 568 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http) 2025-10-21T17:57:57.722+08:00 INFO 568 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2025-10-21T17:57:57.722+08:00 INFO 568 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.16] 2025-10-21T17:57:57.806+08:00 INFO 568 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2025-10-21T17:57:57.806+08:00 INFO 568 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2626 ms 2025-10-21T17:57:58.225+08:00 INFO 568 --- [ main] org.mongodb.driver.client : MongoClient with metadata {"driver": {"name": "mongo-java-driver|sync|spring-boot", "version": "4.11.1"}, "os": {"type": "Windows", "name": "Windows 10", "architecture": "amd64", "version": "10.0"}, "platform": "Java/Oracle Corporation/21.0.6+8-LTS-jvmci-23.1-b55"} created with settings MongoClientSettings{readPreference=primary, writeConcern=WriteConcern{w=null, wTimeout=null ms, journal=null}, retryWrites=true, retryReads=true, readConcern=ReadConcern{level=null}, credential=null, transportSettings=null, streamFactoryFactory=null, commandListeners=[], codecRegistry=ProvidersCodecRegistry{codecProviders=[ValueCodecProvider{}, BsonValueCodecProvider{}, DBRefCodecProvider{}, DBObjectCodecProvider{}, DocumentCodecProvider{}, CollectionCodecProvider{}, IterableCodecProvider{}, MapCodecProvider{}, GeoJsonCodecProvider{}, GridFSFileCodecProvider{}, Jsr310CodecProvider{}, JsonObjectCodecProvider{}, BsonCodecProvider{}, EnumCodecProvider{}, com.mongodb.client.model.mql.ExpressionCodecProvider@1084ac45, com.mongodb.Jep395RecordCodecProvider@566f1852, com.mongodb.KotlinCodecProvider@6ea246af]}, loggerSettings=LoggerSettings{maxDocumentLength=1000}, clusterSettings={hosts=[localhost:27017], srvServiceName=mongodb, mode=SINGLE, requiredClusterType=UNKNOWN, requiredReplicaSetName='null', serverSelector='null', clusterListeners='[]', serverSelectionTimeout='30000 ms', localThreshold='15 ms'}, socketSettings=SocketSettings{connectTimeoutMS=10000, readTimeoutMS=0, receiveBufferSize=0, proxySettings=ProxySettings{host=null, port=null, username=null, password=null}}, heartbeatSocketSettings=SocketSettings{connectTimeoutMS=10000, readTimeoutMS=10000, receiveBufferSize=0, proxySettings=ProxySettings{host=null, port=null, username=null, password=null}}, connectionPoolSettings=ConnectionPoolSettings{maxSize=100, minSize=0, maxWaitTimeMS=120000, maxConnectionLifeTimeMS=0, maxConnectionIdleTimeMS=0, maintenanceInitialDelayMS=0, maintenanceFrequencyMS=60000, connectionPoolListeners=[], maxConnecting=2}, serverSettings=ServerSettings{heartbeatFrequencyMS=10000, minHeartbeatFrequencyMS=500, serverListeners='[]', serverMonitorListeners='[]'}, sslSettings=SslSettings{enabled=false, invalidHostNameAllowed=false, context=null}, applicationName='null', compressorList=[], uuidRepresentation=JAVA_LEGACY, serverApi=null, autoEncryptionSettings=null, dnsClient=null, inetAddressResolver=null, contextProvider=null} 2025-10-21T17:57:58.307+08:00 INFO 568 --- [localhost:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=25, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=89051700} 2025-10-21T17:57:58.846+08:00 WARN 568 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'migrationController' defined in file [D:\svn-workspace\Mongodb-MySQL\target\classes\com\jsy\controller\MigrationController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'dataMigrationServiceImpl' defined in file [D:\svn-workspace\Mongodb-MySQL\target\classes\com\jsy\service\impl\DataMigrationServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 1: Error creating bean with name 'mysqlMapper' defined in file [D:\svn-workspace\Mongodb-MySQL\target\classes\com\jsy\mapper\MysqlMapper.class]: Cannot resolve reference to bean 'sqlSessionTemplate' while setting bean property 'sqlSessionTemplate' 2025-10-21T17:57:58.853+08:00 INFO 568 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2025-10-21T17:57:58.919+08:00 INFO 568 --- [ main] .s.b.a.l.ConditionEvaluationReportLogger : Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2025-10-21T17:57:58.944+08:00 ERROR 568 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active). Process finished with exit code 1 结合这些信息再次解析报错
最新发布
10-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值