mysql wait_timeout 8小时问题解决,tomcat数据源的配置

本文详细记录了一次因MySQL连接超时导致的应用异常,并提供了详细的解决方案,包括调整数据库配置参数、增加连接验证机制等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常报错:

2017-02-13 09:30:17.597 [startQuertz_Worker-6] ERROR com.autoyol.task.TransStatManageTask#execute [line:257] - 定时任务6. 出错org.springframework.dao.RecoverableDataAccessException: 
### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 44,997,436 milliseconds ago.  The last packet sent successfully to the server was 44,997,437 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.
### The error may exist in com/autoyol/mapper/trans/TransMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: ~
### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 44,997,436 milliseconds ago.  The last packet sent successfully to the server was 44,997,437 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.
; SQL []; The last packet successfully received from the server was 44,997,436 milliseconds ago.  The last packet sent successfully to the server was 44,997,437 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.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 44,997,436 milliseconds ago.  The last packet sent successfully to the server was 44,997,437 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.
    at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:98) ~[spring-jdbc-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73) ~[spring-jdbc-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:74) ~[mybatis-spring-1.2.3.jar:1.2.3]
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:399) ~[mybatis-spring-1.2.3.jar:1.2.3]
    at com.sun.proxy.$Proxy34.selectList(Unknown Source) ~[na:na]
    at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:205) ~[mybatis-spring-1.2.3.jar:1.2.3]
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:122) ~[mybatis-3.3.0.jar:3.3.0]
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:64) ~[mybatis-3.3.0.jar:3.3.0]
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53) ~[mybatis-3.3.0.jar:3.3.0]
    at com.sun.proxy.$Proxy43.queryAfterXhStartTransRenterInfoList(Unknown Source) ~[na:na]
    at com.autoyol.service.trans.TransService.queryAfterXhStartTransRenterInfoList(TransService.java:3859) ~[auto-service-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
    at com.autoyol.service.trans.TransService$$FastClassBySpringCGLIB$$82326640.invoke(<generated>) ~[spring-core-4.0.9.RELEASE.jar:1.0-SNAPSHOT]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708) ~[spring-aop-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) ~[spring-tx-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) ~[spring-tx-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) ~[spring-tx-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644) ~[spring-aop-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at com.autoyol.service.trans.TransService$$EnhancerBySpringCGLIB$$d1bda840.queryAfterXhStartTransRenterInfoList(<generated>) ~[spring-core-4.0.9.RELEASE.jar:1.0-SNAPSHOT]
    at com.autoyol.service.trans.TransTaskService.transNotify(TransTaskService.java:983) ~[auto-service-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
    at com.autoyol.service.trans.TransTaskService$$FastClassBySpringCGLIB$$13fabb7b.invoke(<generated>) ~[spring-core-4.0.9.RELEASE.jar:1.0-SNAPSHOT]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708) ~[spring-aop-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) ~[spring-tx-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) ~[spring-tx-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) ~[spring-tx-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644) ~[spring-aop-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at com.autoyol.service.trans.TransTaskService$$EnhancerBySpringCGLIB$$d89a1841.transNotify(<generated>) ~[spring-core-4.0.9.RELEASE.jar:1.0-SNAPSHOT]
    at com.autoyol.task.TransStatManageTask.execute(TransStatManageTask.java:255) ~[auto-service-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
    at sun.reflect.GeneratedMethodAccessor3972.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
    at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:269) [spring-core-4.0.9.RELEASE.jar:4.0.9.RELEASE]
    at com.autoyol.quartz.BeanInvokingJobDetailFactoryBean$BeanInvokingJob.execute(BeanInvokingJobDetailFactoryBean.java:486) [BeanInvokingJobDetailFactoryBean$BeanInvokingJob.class:na]
    at org.quartz.core.JobRunShell.run(JobRunShell.java:223) [quartz-1.8.6.jar:na]
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549) [quartz-1.8.6.jar:na]
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 44,997,436 milliseconds ago.  The last packet sent successfully to the server was 44,997,437 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.
    at sun.reflect.GeneratedConstructorAccessor471.newInstance(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_101]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_101]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:389) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3609) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2417) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2535) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
    at com.mysql.jdbc.MultiHostMySQLConnection.execSQL(MultiHostMySQLConnection.java:140) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1911) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1203) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
    at sun.reflect.GeneratedMethodAccessor129.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
    at com.mysql.jdbc.MultiHostConnectionProxy$JdbcInterfaceProxy.invoke(MultiHostConnectionProxy.java:91) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
    at com.sun.proxy.$Proxy39.execute(Unknown Source) ~[na:na]
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:62) ~[mybatis-3.3.0.jar:3.3.0]
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:78) ~[mybatis-3.3.0.jar:3.3.0]
    at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62) ~[mybatis-3.3.0.jar:3.3.0]
    at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:303) ~[mybatis-3.3.0.jar:3.3.0]
    at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:154) ~[mybatis-3.3.0.jar:3.3.0]
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:102) ~[mybatis-3.3.0.jar:3.3.0]
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:82) ~[mybatis-3.3.0.jar:3.3.0]
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:120) ~[mybatis-3.3.0.jar:3.3.0]
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:113) ~[mybatis-3.3.0.jar:3.3.0]
    at sun.reflect.GeneratedMethodAccessor149.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:386) ~[mybatis-spring-1.2.3.jar:1.2.3]
    ... 40 common frames omitted
Caused by: java.net.SocketException: Connection timed out
    at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.8.0_101]
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109) ~[na:1.8.0_101]
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153) ~[na:1.8.0_101]
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82) ~[na:1.8.0_101]
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140) ~[na:1.8.0_101]
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3591) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
    ... 64 common frames omitted

2.mysql数据库的配置

# 28800   8小时
# 21600   6小时
SET GLOBAL wait_timeout=21600; SHOW GLOBAL VARIABLES
LIKE 'wait_timeout';

3.缺少如下配置项:

<property name="validationQuery"><value>SELECT 1</value></property>
<
property name="testOnBorrow"><value>true</value></property> <property name="testOnReturn"><value>true</value></property>

I had used the same settings in pure JDBC projects few times before and never encoutered similar exception so now I wanted to ask uncle Google for some answers  At the beginning I dove into the MySQL manual to check why autoReconnect isn’t enough in my application but found nothing relevant to my problem. After some thinking I got the idea that maybe some settings in Commons-DBCP are missing so I read dbcp configuration to find that connection could be validated before real usage by specifing two parameters: testOnBorrow=“true” validationQuery=“select 1” . As written in docs:

testOnBorrow – The indication of whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another. NOTE – for a true value to have any effect, the validationQuery parameter must be set to a non-null string.
validationQuery – The SQL query that will be used to validate connections from this pool before returning them to the caller. If specified, this query MUST be an SQL SELECT statement that returns at least one row.

Link:http://tomaszdziurko.com/2010/06/solving-com-mysql-jdbc-exceptions-jdbc4-communicationsexception-in-spring-jdbc-based-application

4.完整的数据源配置参考:

<bean id="settleDataSource" class="org.apache.tomcat.jdbc.pool.DataSource">  
        <property name="driverClassName"><value>${settleDataSource.driverClassName}</value></property>  
        <property name="url"><value>${settleDataSource.url}</value></property>  
        <property name="username" ><value>${settleDataSource.username}</value></property>  
        <property name="password"><value>${settleDataSource.password}</value></property>  
        <property name="maxIdle" value="20" /><!--最大空闲连接 -->
           <property name="minIdle" value="10" /> <!--最小空闲连接 -->
           
        <property name="maxActive"><value>200</value></property>   <!-- 连接池的最大数据库连接数。设为0表示无限制。 -->
        <property name="initialSize"><value>10</value></property>  <!-- 初始化连接数量 -->
          <property name="maxWait" value="10000"/>  <!-- 最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。 -->
          <property name="removeAbandoned" value="true"/>  <!-- 是否自动回收超时连接-->
        <property name="removeAbandonedTimeout" value="600" /><!--  泄露的连接可以被删除的超时值, 单位秒应设置为应用中查询执行最长的时间,单位为秒 -->
        <property name="validationInterval" value="60000"/>  <!--检查连接死活的时间间隔(单位:毫妙) 0以下的话不检查。默认是0。 -->
        
        <property name="validationQuery"><value>SELECT 1</value></property>  
        <property name="testOnBorrow"><value>true</value></property>  
        <property name="testOnReturn"><value>true</value></property>  
        <property name="testWhileIdle"><value>true</value></property>  <!-- 验证失败时,是否将连接从池中丢弃 -->
    </bean> 

 

<think>好的,我需要解决用户关于如何修改Spring Boot动态数据源配置问题,特别是重新启用被禁用的数据源,同时还要调整HikariCP连接池参数和设置MySQL数据源的最佳实践。让我先理清用户的需求和他们提供的引用内容。 首先,用户提到他们之前禁用了Spring Boot的默认数据源自动配置,现在想重新启用它。根据引用[2],禁用是通过在@SpringBootApplication注解中添加exclude属性排除DataSourceAutoConfiguration类。所以,要重新启用,应该移除这个exclude设置。我需要确认这一点,并指导用户检查他们的主应用类,确保没有排除相关的自动配置类。 接下来是关于动态数据源配置。用户可能已经配置了多个数据源,但想重新引入默认数据源。这时可能需要重新配置多个数据源,并确保正确使用@Primary注解来指定主数据源。引用[2]和[3]提到了多数据源配置和Druid连接池的设置,但用户现在可能想用HikariCP,因为它是Spring Boot 2.x的默认连接池。需要指导用户如何同时配置多个数据源,包括默认的HikariCP和可能的其他数据源,比如MySQL。 然后是关于HikariCP参数的调整。用户需要最佳实践,比如设置连接池大小、超时时间等。需要解释如何通过application.properties或application.yml文件配置这些参数,特别是针对每个数据源配置。例如,对于主数据源,使用spring.datasource.hikari.*,而对于其他数据源,可能在配置类中定义Bean并设置属性。 另外,用户提到了MySQL数据源的设置。需要确保依赖正确,比如引入MySQL的JDBC驱动,然后在配置文件中指定URL、用户名、密码等信息。同时,要处理可能的多数据源冲突问题,可能需要使用不同的配置前缀,并在Java配置中分别定义不同的DataSource Bean。 还需要考虑动态数据源的切换,比如使用AbstractRoutingDataSource,但用户的问题可能更偏向于多数据源的静态配置而非动态切换。需要明确用户的需求,如果只是启用被禁用的默认数据源并添加另一个数据源,可能需要同时配置两者,并正确使用@Primary和@Qualifier注解来注入不同的数据源。 最后,综合引用内容中的信息,确保步骤正确,包括移除exclude,配置多个数据源,调整HikariCP参数,设置MySQL连接,并验证配置是否生效。可能还需要提到如何测试配置,比如检查连接池状态或执行简单的数据库查询来确认各个数据源正常工作。</think>### 分步解答:修改Spring Boot动态数据源配置 #### 一、重新启用默认数据源自动配置 1. **移除排除项** 检查主启动类`@SpringBootApplication`注解中的`exclude`属性,若存在`DataSourceAutoConfiguration.class`则删除: ```java // 修改前 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) // 修改后 @SpringBootApplication ``` 这将恢复Spring Boot对单数据源的自动配置能力[^2]。 2. **验证默认数据源** 在`application.properties`中配置数据源参数: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/main_db spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` Spring Boot会自动使用HikariCP作为连接池[^1]。 #### 二、多数据源共存配置(主从结构) 1. **添加第二个数据源配置** 在`application.properties`中声明第二个数据源: ```properties # 主数据源默认) spring.datasource.url=jdbc:mysql://localhost:3306/main_db spring.datasource.hikari.maximum-pool-size=20 # 从数据源 app.datasource.secondary.url=jdbc:mysql://localhost:3306/secondary_db app.datasource.secondary.username=admin app.datasource.secondary.password=admin123 app.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver ``` 2. **定义数据源Bean** 创建配置类显式声明多个数据源: ```java @Configuration public class DataSourceConfig { @Primary @Bean @ConfigurationProperties("spring.datasource.hikari") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean @ConfigurationProperties("app.datasource.secondary") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } } ``` #### 三、HikariCP参数优化实践 1. **关键参数配置** 在`application.properties`中调整主数据源连接池: ```properties spring.datasource.hikari.connection-timeout=30000 spring.datasource.hikari.maximum-pool-size=20 spring.datasource.hikari.minimum-idle=5 spring.datasource.hikari.idle-timeout=600000 spring.datasource.hikari.max-lifetime=1800000 ``` 2. **监控建议** 添加以下配置开启监控: ```properties spring.datasource.hikari.pool-name=MainPool spring.datasource.hikari.register-mbeans=true ``` #### 四、MySQL最佳实践 1. **连接验证配置** 增加可靠性相关参数: ```properties spring.datasource.hikari.connection-test-query=SELECT 1 spring.datasource.hikari.validation-timeout=5000 ``` 2. **时区设置** 在JDBC URL中明确指定时区: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/main_db?serverTimezone=Asia/Shanghai&useSSL=false ``` #### 五、验证配置 1. **启动时检查日志** 观察控制台输出的HikariPool初始化信息: ``` HikariPool-1 - Starting... HikariPool-1 - Start completed ``` 2. **编写测试用例** 通过JUnit测试多数据源连通性: ```java @SpringBootTest class DataSourceTests { @Autowired @Qualifier("primaryDataSource") DataSource primaryDS; @Autowired @Qualifier("secondaryDataSource") DataSource secondaryDS; @Test void testConnection() throws SQLException { Assertions.assertNotNull(primaryDS.getConnection()); Assertions.assertNotNull(secondaryDS.getConnection()); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值