Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Spring boot datasource

Shulou Source: shulou.com Published: 2022-06-03 03:46:20 09月24日 Update

Application-database.properties

# if there is an error when initializing the database, whether to continue to start. Spring.datasource.continue-on-error=false#jdbc driver. Automatic detection through uri by default. Whether the db name spring.datasource.name=test# used by spring.datasource.driver-class-name=com.mysql.jdbc.Driver# randomly generates the db name spring.datasource.generate-unique-name=false#jdbc url. Urispring.datasource.url=# database connection username for database connection spring.datasource.username=# data connection password username for spring.datasource.password=#DML (if the database specifically sets the corresponding username and password) password for spring.datasource.data-username=#DML (if the database specifically sets the corresponding username and password) username for spring.datasource.data-password=#DDL (if the database is specifically set The password of spring.datasource.schema-username=#DDL (if the database specifically sets the corresponding username and password) the fully qualified name of spring.datasource.schema-password=# Connection pool. The default automatically detects the classpathspring.datasource.type=com.zaxxer.hikari.HikariDataSource#sql script character spring.datasource.sql-script-encoding=UTF-8#sql script separator, which defaults to a semicolon. Spring.datasource.separator=;#dbcp2 connection parameter spring.datasource.dbcp2.*#hikari connection parameter spring.datasource.hikari.*#tomcat connection parameter spring.datasource.tomcat.*

Source code-DataSourceProperties

Load the corresponding spring.datasource configuration parameters.

@ ConfigurationProperties (prefix = "spring.datasource") public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean {private ClassLoader classLoader; / * * Name of the datasource. Default to "testdb" when using an embedded database. * / private String name; / * * Whether to generate a random datasource name. * / private boolean generateUniqueName; / * * Fully qualified name of the connection pool implementation to use. By default, it * is auto-detected from the classpath. * / private Class dataSourceClass = DataSourceBuilder .findType (context.getClassLoader ()); return (dataSourceClass = = null? Null: dataSourceClass.getClassLoader ();}} / * * {@ link Condition} to detect when an embedded {@ link DataSource} type can be used. * If a pooled {@ link DataSource} is available, it will always be preferred to an * {@ code EmbeddedDatabase}. * / static class EmbeddedDatabaseCondition extends SpringBootCondition {private final SpringBootCondition pooledCondition = new PooledDataSourceCondition (); @ Override public ConditionOutcome getMatchOutcome (ConditionContext context, AnnotatedTypeMetadata metadata) {ConditionMessage.Builder message = ConditionMessage .forCondition ("EmbeddedDataSource") If (anyMatches (context, metadata, this.pooledCondition)) {return ConditionOutcome .noMatch (message.foundExactly ("supported pooled data source"));} EmbeddedDatabaseType type = EmbeddedDatabaseConnection .get (context.getClassLoader ()) .getType () If (type = = null) {return ConditionOutcome .noMatch (message.didNotFind ("embedded database") .atAll ());} return ConditionOutcome.match (message.found ("embedded database") .items (type));}

test

@ RunWith (SpringRunner.class) @ SpringBootTestpublic class AppContextTest {@ Qualifier ("dataSource") @ Autowired private DataSource dataSource; @ Testpublic void dataSourceTest () throws SQLException {Connection connection = null; try {connection = dataSource.getConnection (); int transactionIsolation= connection.getTransactionIsolation (); String schema= connection.getSchema (); System.out.println ("transactionIsolation=" + transactionIsolation+ "; schema=" + schema) } catch (SQLException e) {e.printStackTrace ();} finally {if (null! = connection) connection.close ();}}

Spring boot defaults to HikariDataSource database connection pooling.

Druid database connection pool: https://blog.51cto.com/881206524/2121687

Tags: Data database password user user name parameter name source code script detection semicolon character time error test generation assembly configuration Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Apple vpn Linux MariaDB