springboot配置连接rds_Spring Boot JPA - 配置自动重新连接

I have a nice little spring boot jpa web application. It is deployed on amazon beanstalk and uses an amazon rds for persisting data. It is however not used that often and there for fails after a while with this kind of exception:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 79,870,633 milliseconds ago.

The last packet sent successfully to the server was 79,870,634 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.

I am not sure how to configure this setting and can not find information on it on http://spring.io (a very good site though). Anybody have some ideas or pointers to information ?

解决方案

I assume that boot is configuring the DataSource for you. In this case, and since you are using MySQL, you can add the following to your application.properties

spring.datasource.testOnBorrow=true

spring.datasource.validationQuery=SELECT 1

Note that the use of autoReconnect is not recommended:

The use of this feature is not recommended, because it has side effects related to session state and data consistency when applications don't handle SQLExceptions properly, and is only designed to be used when you are unable to configure your application to handle SQLExceptions resulting from dead and stale connections properly.

Spring Boot是一个快速开发框架,它支持各种类型的数据库连接。在应用程序中连接RDS数据库是很常见的,因为RDS是阿里云提供的一个高性价比的数据库解决方案。下面是连接RDS数据库的详细步骤。 1.创建RDS实例并获取连接字符串 首先,我们需要在阿里云控制台创建一个RDS实例。在创建过程中,我们需要生成一个用户名和密码,并选择一个数据库引擎类型。创建RDS实例后,我们需要获取连接字符串,该字符串由RDS实例的IP地址、端口号、用户名和密码组成。 2.配置Spring Boot的application.properties 打开Spring Boot应用程序的application.properties文件,添加以下配置信息: ``` spring.datasource.url=jdbc:mysql://[RDS实例的IP地址]:[端口号]/[要连接的数据库名]?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false&maxReconnects=10 spring.datasource.username=[用户名] spring.datasource.password=[密码] spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 注意,需要将URL中的“[RDS实例的IP地址]”替换为实际的IP地址,“[端口号]”替换为实际的端口号,“[要连接的数据库名]”替换为实际的数据库名。 3.测试数据库连接 在application.properties中配置连接信息后,我们需要测试数据库连接是否有效。可以在代码中添加以下测试代码: ``` @Autowired private DataSource dataSource; @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) { return args -> { System.out.println("Datasource: " + dataSource); }; } ``` 此代码会在应用程序启动时测试数据库连接,并在控制台输出数据源信息。 4.进行数据库操作 完成测试后,我们可以使用Spring BootJPA、MyBatis等框架进行数据库操作了。使用这些框架可以大幅度提高开发效率,同时避免手动书写SQL语句的繁琐和易错。 总之,连接RDS数据库只需要简单的配置即可,Spring Boot的便利性和强大性使得应用程序的开发变得更加高效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值