Problem Description:
Use Springboot connection database, start error: Testwhileidle is true, Validationquery not set. But does not affect the system use, the database and so on all access is normal.
The Application.properties data source is configured as follows:
Spring.datasource.username=root
spring.datasource.password=
Spring.datasource.driver-class-name=org.mariadb.jdbc.driver
Spring.datasource.initial-size=1
spring.datasource.maximum-pool-size=10
spring.datasource.connection-timeout=5000
Problem Analysis:
Check the meaning of this sentence: The idle time to detect is open, but the detection of the query statement is not set. The general meaning is that when the database does not receive the request, the database connection is detected and the database is checked to see if it is still attached. Check that the database is disconnected requires a SQL statement to be sent. The error is that the SQL statement is not set.
So there are two ways to solve the problem:
1, set the connection detection when idle.
2, turn on idle connection detection, and set the detection of SQL statements.
First looked up the data, said Testwhileidle default is False, this is actually true, the whole code search for a bit, also did not see settings Testwhileidle place, presumably should be version is different, the new version of the jar package is set to True by default.
Well, regardless of these, since all are database settings, you should be able to configure the data source when you add these configuration, the Web search for the configuration of the wording, as follows:
Spring.datasource.test-while-idle=true
Spring.datasource.validation-query=select 1
But the discovery still did not work, still error.
Some incomprehensible, the same is the configuration of the data source, why the above connection URL, user name, password can take effect, the following will not take effect?
Search again for answers, found to have introduced:
springboot1.4 Cancel Spring.datasource.type
In the new version, Validationquery does not have automatic injection, then you need to declare the datasource bean manually. As for how to write, you can read the above article.
The main is to manually inject Druiddatasource, declare a configuration class, the data source values are injected into, return Druiddatasource.
Start again, the system does not error!
Finally, we looked at the Druiddatasource source code,
public static final String default_validation_query = null;
Sure enough, druiddatasource default testwhileidle=true, and Validationquery is set to empty.
Summarize:
1, first to understand the error, understand the meaning of the wrong.
2, from the error, analyze the cause of the error, for these can find what solutions.
3, re-search information, online information are too chaotic, version inconsistency, the environment is not the same, there are many factors, need to experiment.
4, look at the source code is also a very good solution to the problem.
Springboot Connection Database error Testwhileidle is true, Validationquery not set