This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Recently, I checked a problem with Spring Data JPA. In fact, it is not a problem at the framework level, but rather configuration. Since I haven’t used spring Data JPA much before (more myBatis or my own ORM component), I expected to document the configuration of Spring Data JPA through this article.

This article starts from the configuration source, first intuitively understand the Spring Data JPA own configuration, and then will be combined with specific case to describe the role of each configuration; It also gives a brief introduction to configurations like Spring. Datasource.*, so don’t be confused.

Divided into three parts, one, two, and a comprehensive practice, this part is about JDBC configuration

JDBC configuration and JPA configuration

Datasource configuration (url, class-drive-name, etc.) is under Spring Data JPA. On the other hand, url and class-drive-name are JDBC configurations, not ORM configurations, so they should be. Let’s take a look at JpaProperties and HibernateProperties and JDBC configuration, respectively.

JDBC configuration

In spring’s JDBC code module, the JDBC configuration includes two:

  • JdbcProperties: the prefix forspring.jdbc.*
  • DataSourceProperties: the prefix forspring.datasource.*

JdbcProperties

Here is JdbcProperties, which may seem a little strange to these configurations

@ConfigurationProperties(prefix = "spring.jdbc")
public class JdbcProperties {
   private final Template template = new Template();
   public static class Template {
      private int fetchSize = -1;
      private int maxRows = -1;
      @DurationUnit(ChronoUnit.SECONDS)
      private Duration queryTimeout;
Copy the code

Based on code analysis, the JdbcProperties configuration is currently only used in the JdbcTemplate; That is, if you don’t use JdbcTemplate in your project, then spring.jdbc.template.* is useless. Question: If you use JdbcTemplate and set queryTimeout to -1, does it always time out? 0? The answer is definitely no;

These JDBC parameters serve the underlying Statement from the code. The execution process and principles are not detailed here. The JDBC setQueryTimeout() bug and Query execution was interrupted

  • FetchSize: When querying from a database or result set, the specified fetchSize is pulled each time and the loop is completed.
  • MaxSize: the underlyingStatementObject generated byResultSetThe maximum number of rows an object can contain, after which data will be discarded; This parameter should be set to avoid memory overload if the result set is very large.

DataSourceProperties

This one is easy to understand, data source configuration, common urls, driverClassName are all configured here.

@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties implements BeanClassLoaderAware.InitializingBean {
   private ClassLoader classLoader;
   // datasource name. If embedded database is used, default name is "testdb"
   private String name;
    // Whether to create a random datasource name
   private boolean generateUniqueName = true;
   // The fully qualified name of the concrete Connection Pool implementation class
   private Class<? extends DataSource> type;
   // The fully qualified name of the concrete JDBC Driver implementation class
   private String driverClassName;
   /** * JDBC URL of the database. */
   private String url;

   /** * Login username of the database. */
   private String username;

   /** * Login password of the database. */
   private String password;

   /** * The JNDI location of the data source. (Never used) */
   private String jndiName;
   // Initialize the mode, such as whether to use available DDL and DML scripts
   private DataSourceInitializationMode initializationMode = DataSourceInitializationMode.EMBEDDED;

   SQL or * data-${platform}.sql). */
   private String platform = "all";
   
   // Schema (DDL) Resource location of the script.
   private List<String> schema;
   private String schemaUsername;
   private String schemaPassword;
   
   // Data (DML) Script resource location.
   private List<String> data;
   private String dataUsername;
   private String dataPassword;
   
   // Whether to stop when an error occurs while initializing the database.
   private boolean continueOnError = false;
   
   // Statement delimiter in SQL initialization scripts.
   private String separator = ";";
   
   // Encoding format
   private Charset sqlScriptEncoding;
Copy the code

In a typical project, the common configuration is as follows, which is probably essential for any Spring configuration data source:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/glmapper? createDatabaseIfNotExist=true&xxx=xxx
Copy the code

Configure the initial DDL and DML

spring.datasource.schema=schema.sql
spring.datasource.data=data.sql
Copy the code

If platform is specified in the configuration

spring.datasource.platform=test
Copy the code

Then the DDL and DML for configuration initialization need to be changed to

spring.datasource.schema=schema-test.sql
spring.datasource.data=data-test.sql
Copy the code

The resource cannot be found.

Invalid value 'schema.sql' for configuration property 'spring.datasource.schema' (originating from 'class path resource [application.properties] - 7:26'). Validation failed for the following reason:

The specified resource does not exist.
Copy the code

summary

This article mainly introduces the Spring Data JPA configuration section about JDBC configuration, if any questions welcome to correct.