(window.webpackJsonp=window.webpackJsonp||[]).push([[47],{475:function(e,t,a){"use strict";a.r(t);var o=a(56),n=Object(o.a)({},(function(){var e=this,t=e.$createElement,a=e._self._c||t;return a("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[a("h1",{attrs:{id:"data"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#data"}},[e._v("#")]),e._v(" Data")]),e._v(" "),a("p",[e._v("Spring Boot integrates with a number of data technologies, both SQL and NoSQL.")]),e._v(" "),a("h1",{attrs:{id:"_1-sql-databases"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-sql-databases"}},[e._v("#")]),e._v(" 1. SQL Databases")]),e._v(" "),a("hr"),e._v(" "),a("p",[e._v("The "),a("a",{attrs:{href:"https://spring.io/projects/spring-framework",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Framework"),a("OutboundLink")],1),e._v(" provides extensive support for working with SQL databases, from direct JDBC access using "),a("code",[e._v("JdbcTemplate")]),e._v(" to complete “object relational mapping” technologies such as Hibernate."),a("a",{attrs:{href:"https://spring.io/projects/spring-data",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Data"),a("OutboundLink")],1),e._v(" provides an additional level of functionality: creating "),a("code",[e._v("Repository")]),e._v(" implementations directly from interfaces and using conventions to generate queries from your method names.")]),e._v(" "),a("h3",{attrs:{id:"_1-1-configure-a-datasource"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-1-configure-a-datasource"}},[e._v("#")]),e._v(" 1.1. Configure a DataSource")]),e._v(" "),a("p",[e._v("Java’s "),a("code",[e._v("javax.sql.DataSource")]),e._v(" interface provides a standard method of working with database connections.\nTraditionally, a 'DataSource' uses a "),a("code",[e._v("URL")]),e._v(" along with some credentials to establish a database connection.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("See "),a("RouterLink",{attrs:{to:"/en/spring-boot/howto.html#howto.data-access.configure-custom-datasource"}},[e._v("the “How-to” section")]),e._v(" for more advanced examples, typically to take full control over the configuration of the DataSource.")],1)])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_1-1-1-embedded-database-support"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-1-1-embedded-database-support"}},[e._v("#")]),e._v(" 1.1.1. Embedded Database Support")]),e._v(" "),a("p",[e._v("It is often convenient to develop applications by using an in-memory embedded database.\nObviously, in-memory databases do not provide persistent storage.\nYou need to populate your database when your application starts and be prepared to throw away data when your application ends.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("The “How-to” section includes a "),a("RouterLink",{attrs:{to:"/en/spring-boot/howto.html#howto.data-initialization"}},[e._v("section on how to initialize a database")]),e._v(".")],1)])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("Spring Boot can auto-configure embedded "),a("a",{attrs:{href:"https://www.h2database.com",target:"_blank",rel:"noopener noreferrer"}},[e._v("H2"),a("OutboundLink")],1),e._v(", "),a("a",{attrs:{href:"http://hsqldb.org/",target:"_blank",rel:"noopener noreferrer"}},[e._v("HSQL"),a("OutboundLink")],1),e._v(", and "),a("a",{attrs:{href:"https://db.apache.org/derby/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Derby"),a("OutboundLink")],1),e._v(" databases.\nYou need not provide any connection URLs.\nYou need only include a build dependency to the embedded database that you want to use.\nIf there are multiple embedded databases on the classpath, set the "),a("code",[e._v("spring.datasource.embedded-database-connection")]),e._v(" configuration property to control which one is used.\nSetting the property to "),a("code",[e._v("none")]),e._v(" disables auto-configuration of an embedded database.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("If you are using this feature in your tests, you may notice that the same database is reused by your whole test suite regardless of the number of application contexts that you use."),a("br"),e._v("If you want to make sure that each context has a separate embedded database, you should set "),a("code",[e._v("spring.datasource.generate-unique-name")]),e._v(" to "),a("code",[e._v("true")]),e._v(".")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("For example, the typical POM dependencies would be as follows:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("\n org.springframework.boot\n spring-boot-starter-data-jpa\n\n\n org.hsqldb\n hsqldb\n runtime\n\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("You need a dependency on "),a("code",[e._v("spring-jdbc")]),e._v(" for an embedded database to be auto-configured."),a("br"),e._v("In this example, it is pulled in transitively through "),a("code",[e._v("spring-boot-starter-data-jpa")]),e._v(".")])])]),e._v(" "),a("tbody")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("If, for whatever reason, you do configure the connection URL for an embedded database, take care to ensure that the database’s automatic shutdown is disabled."),a("br"),e._v("If you use H2, you should use "),a("code",[e._v("DB_CLOSE_ON_EXIT=FALSE")]),e._v(" to do so."),a("br"),e._v("If you use HSQLDB, you should ensure that "),a("code",[e._v("shutdown=true")]),e._v(" is not used."),a("br"),e._v("Disabling the database’s automatic shutdown lets Spring Boot control when the database is closed, thereby ensuring that it happens once access to the database is no longer needed.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_1-1-2-connection-to-a-production-database"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-1-2-connection-to-a-production-database"}},[e._v("#")]),e._v(" 1.1.2. Connection to a Production Database")]),e._v(" "),a("p",[e._v("Production database connections can also be auto-configured by using a pooling "),a("code",[e._v("DataSource")]),e._v(".")]),e._v(" "),a("h4",{attrs:{id:"_1-1-3-datasource-configuration"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-1-3-datasource-configuration"}},[e._v("#")]),e._v(" 1.1.3. DataSource Configuration")]),e._v(" "),a("p",[e._v("DataSource configuration is controlled by external configuration properties in "),a("code",[e._v("spring.datasource.*")]),e._v(".\nFor example, you might declare the following section in "),a("code",[e._v("application.properties")]),e._v(":")]),e._v(" "),a("p",[e._v("Properties")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("spring.datasource.url=jdbc:mysql://localhost/test\nspring.datasource.username=dbuser\nspring.datasource.password=dbpass\n")])])]),a("p",[e._v("Yaml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('spring:\n datasource:\n url: "jdbc:mysql://localhost/test"\n username: "dbuser"\n password: "dbpass"\n')])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("You should at least specify the URL by setting the "),a("code",[e._v("spring.datasource.url")]),e._v(" property."),a("br"),e._v("Otherwise, Spring Boot tries to auto-configure an embedded database.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("Spring Boot can deduce the JDBC driver class for most databases from the URL."),a("br"),e._v("If you need to specify a specific class, you can use the "),a("code",[e._v("spring.datasource.driver-class-name")]),e._v(" property.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("For a pooling "),a("code",[e._v("DataSource")]),e._v(" to be created, we need to be able to verify that a valid "),a("code",[e._v("Driver")]),e._v(" class is available, so we check for that before doing anything."),a("br"),e._v("In other words, if you set "),a("code",[e._v("spring.datasource.driver-class-name=com.mysql.jdbc.Driver")]),e._v(", then that class has to be loadable.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("See "),a("a",{attrs:{href:"https://github.com/spring-projects/spring-boot/tree/v2.6.4/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java",target:"_blank",rel:"noopener noreferrer"}},[a("code",[e._v("DataSourceProperties")]),a("OutboundLink")],1),e._v(" for more of the supported options.\nThese are the standard options that work regardless of "),a("RouterLink",{attrs:{to:"/en/spring-boot/features.html#data.sql.datasource.connection-pool"}},[e._v("the actual implementation")]),e._v(".\nIt is also possible to fine-tune implementation-specific settings by using their respective prefix ("),a("code",[e._v("spring.datasource.hikari.*")]),e._v(", "),a("code",[e._v("spring.datasource.tomcat.*")]),e._v(", "),a("code",[e._v("spring.datasource.dbcp2.*")]),e._v(", and "),a("code",[e._v("spring.datasource.oracleucp.*")]),e._v(").\nSee the documentation of the connection pool implementation you are using for more details.")],1),e._v(" "),a("p",[e._v("For instance, if you use the "),a("a",{attrs:{href:"https://tomcat.apache.org/tomcat-9.0-doc/jdbc-pool.html#Common_Attributes",target:"_blank",rel:"noopener noreferrer"}},[e._v("Tomcat connection pool"),a("OutboundLink")],1),e._v(", you could customize many additional settings, as shown in the following example:")]),e._v(" "),a("p",[e._v("Properties")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("spring.datasource.tomcat.max-wait=10000\nspring.datasource.tomcat.max-active=50\nspring.datasource.tomcat.test-on-borrow=true\n")])])]),a("p",[e._v("Yaml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("spring:\n datasource:\n tomcat:\n max-wait: 10000\n max-active: 50\n test-on-borrow: true\n")])])]),a("p",[e._v("This will set the pool to wait 10000ms before throwing an exception if no connection is available, limit the maximum number of connections to 50 and validate the connection before borrowing it from the pool.")]),e._v(" "),a("h4",{attrs:{id:"_1-1-4-supported-connection-pools"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-1-4-supported-connection-pools"}},[e._v("#")]),e._v(" 1.1.4. Supported Connection Pools")]),e._v(" "),a("p",[e._v("Spring Boot uses the following algorithm for choosing a specific implementation:")]),e._v(" "),a("ol",[a("li",[a("p",[e._v("We prefer "),a("a",{attrs:{href:"https://github.com/brettwooldridge/HikariCP",target:"_blank",rel:"noopener noreferrer"}},[e._v("HikariCP"),a("OutboundLink")],1),e._v(" for its performance and concurrency.\nIf HikariCP is available, we always choose it.")])]),e._v(" "),a("li",[a("p",[e._v("Otherwise, if the Tomcat pooling "),a("code",[e._v("DataSource")]),e._v(" is available, we use it.")])]),e._v(" "),a("li",[a("p",[e._v("Otherwise, if "),a("a",{attrs:{href:"https://commons.apache.org/proper/commons-dbcp/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Commons DBCP2"),a("OutboundLink")],1),e._v(" is available, we use it.")])]),e._v(" "),a("li",[a("p",[e._v("If none of HikariCP, Tomcat, and DBCP2 are available and if Oracle UCP is available, we use it.")])])]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("If you use the "),a("code",[e._v("spring-boot-starter-jdbc")]),e._v(" or "),a("code",[e._v("spring-boot-starter-data-jpa")]),e._v(" “starters”, you automatically get a dependency to "),a("code",[e._v("HikariCP")]),e._v(".")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("You can bypass that algorithm completely and specify the connection pool to use by setting the "),a("code",[e._v("spring.datasource.type")]),e._v(" property.\nThis is especially important if you run your application in a Tomcat container, as "),a("code",[e._v("tomcat-jdbc")]),e._v(" is provided by default.")]),e._v(" "),a("p",[e._v("Additional connection pools can always be configured manually, using "),a("code",[e._v("DataSourceBuilder")]),e._v(".\nIf you define your own "),a("code",[e._v("DataSource")]),e._v(" bean, auto-configuration does not occur.\nThe following connection pools are supported by "),a("code",[e._v("DataSourceBuilder")]),e._v(":")]),e._v(" "),a("ul",[a("li",[a("p",[e._v("HikariCP")])]),e._v(" "),a("li",[a("p",[e._v("Tomcat pooling "),a("code",[e._v("Datasource")])])]),e._v(" "),a("li",[a("p",[e._v("Commons DBCP2")])]),e._v(" "),a("li",[a("p",[e._v("Oracle UCP & "),a("code",[e._v("OracleDataSource")])])]),e._v(" "),a("li",[a("p",[e._v("Spring Framework’s "),a("code",[e._v("SimpleDriverDataSource")])])]),e._v(" "),a("li",[a("p",[e._v("H2 "),a("code",[e._v("JdbcDataSource")])])]),e._v(" "),a("li",[a("p",[e._v("PostgreSQL "),a("code",[e._v("PGSimpleDataSource")])])])]),e._v(" "),a("h4",{attrs:{id:"_1-1-5-connection-to-a-jndi-datasource"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-1-5-connection-to-a-jndi-datasource"}},[e._v("#")]),e._v(" 1.1.5. Connection to a JNDI DataSource")]),e._v(" "),a("p",[e._v("If you deploy your Spring Boot application to an Application Server, you might want to configure and manage your DataSource by using your Application Server’s built-in features and access it by using JNDI.")]),e._v(" "),a("p",[e._v("The "),a("code",[e._v("spring.datasource.jndi-name")]),e._v(" property can be used as an alternative to the "),a("code",[e._v("spring.datasource.url")]),e._v(", "),a("code",[e._v("spring.datasource.username")]),e._v(", and "),a("code",[e._v("spring.datasource.password")]),e._v(" properties to access the "),a("code",[e._v("DataSource")]),e._v(" from a specific JNDI location.\nFor example, the following section in "),a("code",[e._v("application.properties")]),e._v(" shows how you can access a JBoss AS defined "),a("code",[e._v("DataSource")]),e._v(":")]),e._v(" "),a("p",[e._v("Properties")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("spring.datasource.jndi-name=java:jboss/datasources/customers\n")])])]),a("p",[e._v("Yaml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('spring:\n datasource:\n jndi-name: "java:jboss/datasources/customers"\n')])])]),a("h3",{attrs:{id:"_1-2-using-jdbctemplate"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-2-using-jdbctemplate"}},[e._v("#")]),e._v(" 1.2. Using JdbcTemplate")]),e._v(" "),a("p",[e._v("Spring’s "),a("code",[e._v("JdbcTemplate")]),e._v(" and "),a("code",[e._v("NamedParameterJdbcTemplate")]),e._v(" classes are auto-configured, and you can "),a("code",[e._v("@Autowire")]),e._v(" them directly into your own beans, as shown in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("import org.springframework.jdbc.core.JdbcTemplate;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class MyBean {\n\n private final JdbcTemplate jdbcTemplate;\n\n public MyBean(JdbcTemplate jdbcTemplate) {\n this.jdbcTemplate = jdbcTemplate;\n }\n\n public void doSomething() {\n this.jdbcTemplate ...\n }\n\n}\n\n")])])]),a("p",[e._v("You can customize some properties of the template by using the "),a("code",[e._v("spring.jdbc.template.*")]),e._v(" properties, as shown in the following example:")]),e._v(" "),a("p",[e._v("Properties")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("spring.jdbc.template.max-rows=500\n")])])]),a("p",[e._v("Yaml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("spring:\n jdbc:\n template:\n max-rows: 500\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("The "),a("code",[e._v("NamedParameterJdbcTemplate")]),e._v(" reuses the same "),a("code",[e._v("JdbcTemplate")]),e._v(" instance behind the scenes."),a("br"),e._v("If more than one "),a("code",[e._v("JdbcTemplate")]),e._v(" is defined and no primary candidate exists, the "),a("code",[e._v("NamedParameterJdbcTemplate")]),e._v(" is not auto-configured.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h3",{attrs:{id:"_1-3-jpa-and-spring-data-jpa"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-3-jpa-and-spring-data-jpa"}},[e._v("#")]),e._v(" 1.3. JPA and Spring Data JPA")]),e._v(" "),a("p",[e._v("The Java Persistence API is a standard technology that lets you “map” objects to relational databases.\nThe "),a("code",[e._v("spring-boot-starter-data-jpa")]),e._v(" POM provides a quick way to get started.\nIt provides the following key dependencies:")]),e._v(" "),a("ul",[a("li",[a("p",[e._v("Hibernate: One of the most popular JPA implementations.")])]),e._v(" "),a("li",[a("p",[e._v("Spring Data JPA: Helps you to implement JPA-based repositories.")])]),e._v(" "),a("li",[a("p",[e._v("Spring ORM: Core ORM support from the Spring Framework.")])])]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("We do not go into too many details of JPA or "),a("a",{attrs:{href:"https://spring.io/projects/spring-data",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Data"),a("OutboundLink")],1),e._v(" here."),a("br"),e._v("You can follow the "),a("a",{attrs:{href:"https://spring.io/guides/gs/accessing-data-jpa/",target:"_blank",rel:"noopener noreferrer"}},[e._v("“Accessing Data with JPA”"),a("OutboundLink")],1),e._v(" guide from "),a("a",{attrs:{href:"https://spring.io",target:"_blank",rel:"noopener noreferrer"}},[e._v("spring.io"),a("OutboundLink")],1),e._v(" and read the "),a("a",{attrs:{href:"https://spring.io/projects/spring-data-jpa",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Data JPA"),a("OutboundLink")],1),e._v(" and "),a("a",{attrs:{href:"https://hibernate.org/orm/documentation/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Hibernate"),a("OutboundLink")],1),e._v(" reference documentation.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_1-3-1-entity-classes"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-3-1-entity-classes"}},[e._v("#")]),e._v(" 1.3.1. Entity Classes")]),e._v(" "),a("p",[e._v("Traditionally, JPA “Entity” classes are specified in a "),a("code",[e._v("persistence.xml")]),e._v(" file.\nWith Spring Boot, this file is not necessary and “Entity Scanning” is used instead.\nBy default, all packages below your main configuration class (the one annotated with "),a("code",[e._v("@EnableAutoConfiguration")]),e._v(" or "),a("code",[e._v("@SpringBootApplication")]),e._v(") are searched.")]),e._v(" "),a("p",[e._v("Any classes annotated with "),a("code",[e._v("@Entity")]),e._v(", "),a("code",[e._v("@Embeddable")]),e._v(", or "),a("code",[e._v("@MappedSuperclass")]),e._v(" are considered.\nA typical entity class resembles the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("import java.io.Serializable;\n\nimport javax.persistence.Column;\nimport javax.persistence.Entity;\nimport javax.persistence.GeneratedValue;\nimport javax.persistence.Id;\n\n@Entity\npublic class City implements Serializable {\n\n @Id\n @GeneratedValue\n private Long id;\n\n @Column(nullable = false)\n private String name;\n\n @Column(nullable = false)\n private String state;\n\n // ... additional members, often include @OneToMany mappings\n\n protected City() {\n // no-args constructor required by JPA spec\n // this one is protected since it should not be used directly\n }\n\n public City(String name, String state) {\n this.name = name;\n this.state = state;\n }\n\n public String getName() {\n return this.name;\n }\n\n public String getState() {\n return this.state;\n }\n\n // ... etc\n\n}\n\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("You can customize entity scanning locations by using the "),a("code",[e._v("@EntityScan")]),e._v(" annotation."),a("br"),e._v("See the “"),a("RouterLink",{attrs:{to:"/en/spring-boot/howto.html#howto.data-access.separate-entity-definitions-from-spring-configuration"}},[e._v("howto.html")]),e._v("” how-to.")],1)])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_1-3-2-spring-data-jpa-repositories"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-3-2-spring-data-jpa-repositories"}},[e._v("#")]),e._v(" 1.3.2. Spring Data JPA Repositories")]),e._v(" "),a("p",[a("a",{attrs:{href:"https://spring.io/projects/spring-data-jpa",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Data JPA"),a("OutboundLink")],1),e._v(" repositories are interfaces that you can define to access data.\nJPA queries are created automatically from your method names.\nFor example, a "),a("code",[e._v("CityRepository")]),e._v(" interface might declare a "),a("code",[e._v("findAllByState(String state)")]),e._v(" method to find all the cities in a given state.")]),e._v(" "),a("p",[e._v("For more complex queries, you can annotate your method with Spring Data’s "),a("a",{attrs:{href:"https://docs.spring.io/spring-data/jpa/docs/2.6.2/api/org/springframework/data/jpa/repository/Query.html",target:"_blank",rel:"noopener noreferrer"}},[a("code",[e._v("Query")]),a("OutboundLink")],1),e._v(" annotation.")]),e._v(" "),a("p",[e._v("Spring Data repositories usually extend from the "),a("a",{attrs:{href:"https://docs.spring.io/spring-data/commons/docs/2.6.2/api/org/springframework/data/repository/Repository.html",target:"_blank",rel:"noopener noreferrer"}},[a("code",[e._v("Repository")]),a("OutboundLink")],1),e._v(" or "),a("a",{attrs:{href:"https://docs.spring.io/spring-data/commons/docs/2.6.2/api/org/springframework/data/repository/CrudRepository.html",target:"_blank",rel:"noopener noreferrer"}},[a("code",[e._v("CrudRepository")]),a("OutboundLink")],1),e._v(" interfaces.\nIf you use auto-configuration, repositories are searched from the package containing your main configuration class (the one annotated with "),a("code",[e._v("@EnableAutoConfiguration")]),e._v(" or "),a("code",[e._v("@SpringBootApplication")]),e._v(") down.")]),e._v(" "),a("p",[e._v("The following example shows a typical Spring Data repository interface definition:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("import org.springframework.boot.docs.data.sql.jpaandspringdata.entityclasses.City;\nimport org.springframework.data.domain.Page;\nimport org.springframework.data.domain.Pageable;\nimport org.springframework.data.repository.Repository;\n\npublic interface CityRepository extends Repository {\n\n Page findAll(Pageable pageable);\n\n City findByNameAndStateAllIgnoringCase(String name, String state);\n\n}\n\n")])])]),a("p",[e._v("Spring Data JPA repositories support three different modes of bootstrapping: default, deferred, and lazy.\nTo enable deferred or lazy bootstrapping, set the "),a("code",[e._v("spring.data.jpa.repositories.bootstrap-mode")]),e._v(" property to "),a("code",[e._v("deferred")]),e._v(" or "),a("code",[e._v("lazy")]),e._v(" respectively.\nWhen using deferred or lazy bootstrapping, the auto-configured "),a("code",[e._v("EntityManagerFactoryBuilder")]),e._v(" will use the context’s "),a("code",[e._v("AsyncTaskExecutor")]),e._v(", if any, as the bootstrap executor.\nIf more than one exists, the one named "),a("code",[e._v("applicationTaskExecutor")]),e._v(" will be used.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("When using deferred or lazy bootstrapping, make sure to defer any access to the JPA infrastructure after the application context bootstrap phase."),a("br"),e._v("You can use "),a("code",[e._v("SmartInitializingSingleton")]),e._v(" to invoke any initialization that requires the JPA infrastructure."),a("br"),e._v("For JPA components (such as converters) that are created as Spring beans, use "),a("code",[e._v("ObjectProvider")]),e._v(" to delay the resolution of dependencies, if any.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("We have barely scratched the surface of Spring Data JPA."),a("br"),e._v("For complete details, see the "),a("a",{attrs:{href:"https://docs.spring.io/spring-data/jpa/docs/2.6.2/reference/html",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Data JPA reference documentation"),a("OutboundLink")],1),e._v(".")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_1-3-3-spring-data-envers-repositories"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-3-3-spring-data-envers-repositories"}},[e._v("#")]),e._v(" 1.3.3. Spring Data Envers Repositories")]),e._v(" "),a("p",[e._v("If "),a("a",{attrs:{href:"https://spring.io/projects/spring-data-envers",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Data Envers"),a("OutboundLink")],1),e._v(" is available, JPA repositories are auto-configured to support typical Envers queries.")]),e._v(" "),a("p",[e._v("To use Spring Data Envers, make sure your repository extends from "),a("code",[e._v("RevisionRepository")]),e._v(" as show in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("import org.springframework.boot.docs.data.sql.jpaandspringdata.entityclasses.Country;\nimport org.springframework.data.domain.Page;\nimport org.springframework.data.domain.Pageable;\nimport org.springframework.data.repository.Repository;\nimport org.springframework.data.repository.history.RevisionRepository;\n\npublic interface CountryRepository extends RevisionRepository, Repository {\n\n Page findAll(Pageable pageable);\n\n}\n\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("For more details, check the "),a("a",{attrs:{href:"https://docs.spring.io/spring-data/envers/docs/2.6.2/reference/html/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Data Envers reference documentation"),a("OutboundLink")],1),e._v(".")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_1-3-4-creating-and-dropping-jpa-databases"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-3-4-creating-and-dropping-jpa-databases"}},[e._v("#")]),e._v(" 1.3.4. Creating and Dropping JPA Databases")]),e._v(" "),a("p",[e._v("By default, JPA databases are automatically created "),a("strong",[e._v("only")]),e._v(" if you use an embedded database (H2, HSQL, or Derby).\nYou can explicitly configure JPA settings by using "),a("code",[e._v("spring.jpa.*")]),e._v(" properties.\nFor example, to create and drop tables you can add the following line to your "),a("code",[e._v("application.properties")]),e._v(":")]),e._v(" "),a("p",[e._v("Properties")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("spring.jpa.hibernate.ddl-auto=create-drop\n")])])]),a("p",[e._v("Yaml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('spring:\n jpa:\n hibernate.ddl-auto: "create-drop"\n')])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("Hibernate’s own internal property name for this (if you happen to remember it better) is "),a("code",[e._v("hibernate.hbm2ddl.auto")]),e._v("."),a("br"),e._v("You can set it, along with other Hibernate native properties, by using "),a("code",[e._v("spring.jpa.properties.*")]),e._v(" (the prefix is stripped before adding them to the entity manager)."),a("br"),e._v("The following line shows an example of setting JPA properties for Hibernate:")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("Properties")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("spring.jpa.properties.hibernate[globally_quoted_identifiers]=true\n")])])]),a("p",[e._v("Yaml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('spring:\n jpa:\n properties:\n hibernate:\n "globally_quoted_identifiers": "true"\n')])])]),a("p",[e._v("The line in the preceding example passes a value of "),a("code",[e._v("true")]),e._v(" for the "),a("code",[e._v("hibernate.globally_quoted_identifiers")]),e._v(" property to the Hibernate entity manager.")]),e._v(" "),a("p",[e._v("By default, the DDL execution (or validation) is deferred until the "),a("code",[e._v("ApplicationContext")]),e._v(" has started.\nThere is also a "),a("code",[e._v("spring.jpa.generate-ddl")]),e._v(" flag, but it is not used if Hibernate auto-configuration is active, because the "),a("code",[e._v("ddl-auto")]),e._v(" settings are more fine-grained.")]),e._v(" "),a("h4",{attrs:{id:"_1-3-5-open-entitymanager-in-view"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-3-5-open-entitymanager-in-view"}},[e._v("#")]),e._v(" 1.3.5. Open EntityManager in View")]),e._v(" "),a("p",[e._v("If you are running a web application, Spring Boot by default registers "),a("a",{attrs:{href:"https://docs.spring.io/spring-framework/docs/5.3.16/javadoc-api/org/springframework/orm/jpa/support/OpenEntityManagerInViewInterceptor.html",target:"_blank",rel:"noopener noreferrer"}},[a("code",[e._v("OpenEntityManagerInViewInterceptor")]),a("OutboundLink")],1),e._v(" to apply the “Open EntityManager in View” pattern, to allow for lazy loading in web views.\nIf you do not want this behavior, you should set "),a("code",[e._v("spring.jpa.open-in-view")]),e._v(" to "),a("code",[e._v("false")]),e._v(" in your "),a("code",[e._v("application.properties")]),e._v(".")]),e._v(" "),a("h3",{attrs:{id:"_1-4-spring-data-jdbc"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-4-spring-data-jdbc"}},[e._v("#")]),e._v(" 1.4. Spring Data JDBC")]),e._v(" "),a("p",[e._v("Spring Data includes repository support for JDBC and will automatically generate SQL for the methods on "),a("code",[e._v("CrudRepository")]),e._v(".\nFor more advanced queries, a "),a("code",[e._v("@Query")]),e._v(" annotation is provided.")]),e._v(" "),a("p",[e._v("Spring Boot will auto-configure Spring Data’s JDBC repositories when the necessary dependencies are on the classpath.\nThey can be added to your project with a single dependency on "),a("code",[e._v("spring-boot-starter-data-jdbc")]),e._v(".\nIf necessary, you can take control of Spring Data JDBC’s configuration by adding the "),a("code",[e._v("@EnableJdbcRepositories")]),e._v(" annotation or a "),a("code",[e._v("JdbcConfiguration")]),e._v(" subclass to your application.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("For complete details of Spring Data JDBC, see the "),a("a",{attrs:{href:"https://docs.spring.io/spring-data/jdbc/docs/2.3.2/reference/html/",target:"_blank",rel:"noopener noreferrer"}},[e._v("reference documentation"),a("OutboundLink")],1),e._v(".")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h3",{attrs:{id:"_1-5-using-h2-s-web-console"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-5-using-h2-s-web-console"}},[e._v("#")]),e._v(" 1.5. Using H2’s Web Console")]),e._v(" "),a("p",[e._v("The "),a("a",{attrs:{href:"https://www.h2database.com",target:"_blank",rel:"noopener noreferrer"}},[e._v("H2 database"),a("OutboundLink")],1),e._v(" provides a "),a("a",{attrs:{href:"https://www.h2database.com/html/quickstart.html#h2_console",target:"_blank",rel:"noopener noreferrer"}},[e._v("browser-based console"),a("OutboundLink")],1),e._v(" that Spring Boot can auto-configure for you.\nThe console is auto-configured when the following conditions are met:")]),e._v(" "),a("ul",[a("li",[a("p",[e._v("You are developing a servlet-based web application.")])]),e._v(" "),a("li",[a("p",[a("code",[e._v("com.h2database:h2")]),e._v(" is on the classpath.")])]),e._v(" "),a("li",[a("p",[e._v("You are using "),a("RouterLink",{attrs:{to:"/en/spring-boot/using.html#using.devtools"}},[e._v("Spring Boot’s developer tools")]),e._v(".")],1)])]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("If you are not using Spring Boot’s developer tools but would still like to make use of H2’s console, you can configure the "),a("code",[e._v("spring.h2.console.enabled")]),e._v(" property with a value of "),a("code",[e._v("true")]),e._v(".")])])]),e._v(" "),a("tbody")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("The H2 console is only intended for use during development, so you should take care to ensure that "),a("code",[e._v("spring.h2.console.enabled")]),e._v(" is not set to "),a("code",[e._v("true")]),e._v(" in production.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_1-5-1-changing-the-h2-console-s-path"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-5-1-changing-the-h2-console-s-path"}},[e._v("#")]),e._v(" 1.5.1. Changing the H2 Console’s Path")]),e._v(" "),a("p",[e._v("By default, the console is available at "),a("code",[e._v("/h2-console")]),e._v(".\nYou can customize the console’s path by using the "),a("code",[e._v("spring.h2.console.path")]),e._v(" property.")]),e._v(" "),a("h3",{attrs:{id:"_1-6-using-jooq"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-6-using-jooq"}},[e._v("#")]),e._v(" 1.6. Using jOOQ")]),e._v(" "),a("p",[e._v("jOOQ Object Oriented Querying ("),a("a",{attrs:{href:"https://www.jooq.org/",target:"_blank",rel:"noopener noreferrer"}},[e._v("jOOQ"),a("OutboundLink")],1),e._v(") is a popular product from "),a("a",{attrs:{href:"https://www.datageekery.com/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Data Geekery"),a("OutboundLink")],1),e._v(" which generates Java code from your database and lets you build type-safe SQL queries through its fluent API.\nBoth the commercial and open source editions can be used with Spring Boot.")]),e._v(" "),a("h4",{attrs:{id:"_1-6-1-code-generation"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-6-1-code-generation"}},[e._v("#")]),e._v(" 1.6.1. Code Generation")]),e._v(" "),a("p",[e._v("In order to use jOOQ type-safe queries, you need to generate Java classes from your database schema.\nYou can follow the instructions in the "),a("a",{attrs:{href:"https://www.jooq.org/doc/3.14.15/manual-single-page/#jooq-in-7-steps-step3",target:"_blank",rel:"noopener noreferrer"}},[e._v("jOOQ user manual"),a("OutboundLink")],1),e._v(".\nIf you use the "),a("code",[e._v("jooq-codegen-maven")]),e._v(" plugin and you also use the "),a("code",[e._v("spring-boot-starter-parent")]),e._v(" “parent POM”, you can safely omit the plugin’s "),a("code",[e._v("")]),e._v(" tag.\nYou can also use Spring Boot-defined version variables (such as "),a("code",[e._v("h2.version")]),e._v(") to declare the plugin’s database dependency.\nThe following listing shows an example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("\n org.jooq\n jooq-codegen-maven\n \n ...\n \n \n \n com.h2database\n h2\n ${h2.version}\n \n \n \n \n org.h2.Driver\n jdbc:h2:~/yourdatabase\n \n \n ...\n \n \n\n")])])]),a("h4",{attrs:{id:"_1-6-2-using-dslcontext"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-6-2-using-dslcontext"}},[e._v("#")]),e._v(" 1.6.2. Using DSLContext")]),e._v(" "),a("p",[e._v("The fluent API offered by jOOQ is initiated through the "),a("code",[e._v("org.jooq.DSLContext")]),e._v(" interface.\nSpring Boot auto-configures a "),a("code",[e._v("DSLContext")]),e._v(" as a Spring Bean and connects it to your application "),a("code",[e._v("DataSource")]),e._v(".\nTo use the "),a("code",[e._v("DSLContext")]),e._v(", you can inject it, as shown in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("import java.util.GregorianCalendar;\nimport java.util.List;\n\nimport org.jooq.DSLContext;\n\nimport org.springframework.stereotype.Component;\n\nimport static org.springframework.boot.docs.data.sql.jooq.dslcontext.Tables.AUTHOR;\n\n@Component\npublic class MyBean {\n\n private final DSLContext create;\n\n public MyBean(DSLContext dslContext) {\n this.create = dslContext;\n }\n\n}\n\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("The jOOQ manual tends to use a variable named "),a("code",[e._v("create")]),e._v(" to hold the "),a("code",[e._v("DSLContext")]),e._v(".")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("You can then use the "),a("code",[e._v("DSLContext")]),e._v(" to construct your queries, as shown in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("public List authorsBornAfter1980() {\n return this.create.selectFrom(AUTHOR)\n .where(AUTHOR.DATE_OF_BIRTH.greaterThan(new GregorianCalendar(1980, 0, 1)))\n .fetch(AUTHOR.DATE_OF_BIRTH);\n\n")])])]),a("h4",{attrs:{id:"_1-6-3-jooq-sql-dialect"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-6-3-jooq-sql-dialect"}},[e._v("#")]),e._v(" 1.6.3. jOOQ SQL Dialect")]),e._v(" "),a("p",[e._v("Unless the "),a("code",[e._v("spring.jooq.sql-dialect")]),e._v(" property has been configured, Spring Boot determines the SQL dialect to use for your datasource.\nIf Spring Boot could not detect the dialect, it uses "),a("code",[e._v("DEFAULT")]),e._v(".")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("Spring Boot can only auto-configure dialects supported by the open source version of jOOQ.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_1-6-4-customizing-jooq"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-6-4-customizing-jooq"}},[e._v("#")]),e._v(" 1.6.4. Customizing jOOQ")]),e._v(" "),a("p",[e._v("More advanced customizations can be achieved by defining your own "),a("code",[e._v("DefaultConfigurationCustomizer")]),e._v(" bean that will be invoked prior to creating the "),a("code",[e._v("org.jooq.Configuration")]),e._v(" "),a("code",[e._v("@Bean")]),e._v(".\nThis takes precedence to anything that is applied by the auto-configuration.")]),e._v(" "),a("p",[e._v("You can also create your own "),a("code",[e._v("org.jooq.Configuration")]),e._v(" "),a("code",[e._v("@Bean")]),e._v(" if you want to take complete control of the jOOQ configuration.")]),e._v(" "),a("h3",{attrs:{id:"_1-7-using-r2dbc"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-7-using-r2dbc"}},[e._v("#")]),e._v(" 1.7. Using R2DBC")]),e._v(" "),a("p",[e._v("The Reactive Relational Database Connectivity ("),a("a",{attrs:{href:"https://r2dbc.io",target:"_blank",rel:"noopener noreferrer"}},[e._v("R2DBC"),a("OutboundLink")],1),e._v(") project brings reactive programming APIs to relational databases.\nR2DBC’s "),a("code",[e._v("io.r2dbc.spi.Connection")]),e._v(" provides a standard method of working with non-blocking database connections.\nConnections are provided by using a "),a("code",[e._v("ConnectionFactory")]),e._v(", similar to a "),a("code",[e._v("DataSource")]),e._v(" with jdbc.")]),e._v(" "),a("p",[a("code",[e._v("ConnectionFactory")]),e._v(" configuration is controlled by external configuration properties in "),a("code",[e._v("spring.r2dbc.*")]),e._v(".\nFor example, you might declare the following section in "),a("code",[e._v("application.properties")]),e._v(":")]),e._v(" "),a("p",[e._v("Properties")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("spring.r2dbc.url=r2dbc:postgresql://localhost/test\nspring.r2dbc.username=dbuser\nspring.r2dbc.password=dbpass\n")])])]),a("p",[e._v("Yaml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('spring:\n r2dbc:\n url: "r2dbc:postgresql://localhost/test"\n username: "dbuser"\n password: "dbpass"\n')])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("You do not need to specify a driver class name, since Spring Boot obtains the driver from R2DBC’s Connection Factory discovery.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("At least the url should be provided."),a("br"),e._v("Information specified in the URL takes precedence over individual properties, that is "),a("code",[e._v("name")]),e._v(", "),a("code",[e._v("username")]),e._v(", "),a("code",[e._v("password")]),e._v(" and pooling options.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("The “How-to” section includes a "),a("RouterLink",{attrs:{to:"/en/spring-boot/howto.html#howto.data-initialization.using-basic-sql-scripts"}},[e._v("section on how to initialize a database")]),e._v(".")],1)])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("To customize the connections created by a "),a("code",[e._v("ConnectionFactory")]),e._v(", that is, set specific parameters that you do not want (or cannot) configure in your central database configuration, you can use a "),a("code",[e._v("ConnectionFactoryOptionsBuilderCustomizer")]),e._v(" "),a("code",[e._v("@Bean")]),e._v(".\nThe following example shows how to manually override the database port while the rest of the options is taken from the application configuration:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("import io.r2dbc.spi.ConnectionFactoryOptions;\n\nimport org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryOptionsBuilderCustomizer;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n\n@Configuration(proxyBeanMethods = false)\npublic class MyR2dbcConfiguration {\n\n @Bean\n public ConnectionFactoryOptionsBuilderCustomizer connectionFactoryPortCustomizer() {\n return (builder) -> builder.option(ConnectionFactoryOptions.PORT, 5432);\n }\n\n}\n\n")])])]),a("p",[e._v("The following examples show how to set some PostgreSQL connection options:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('import java.util.HashMap;\nimport java.util.Map;\n\nimport io.r2dbc.postgresql.PostgresqlConnectionFactoryProvider;\n\nimport org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryOptionsBuilderCustomizer;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n\n@Configuration(proxyBeanMethods = false)\npublic class MyPostgresR2dbcConfiguration {\n\n @Bean\n public ConnectionFactoryOptionsBuilderCustomizer postgresCustomizer() {\n Map options = new HashMap<>();\n options.put("lock_timeout", "30s");\n options.put("statement_timeout", "60s");\n return (builder) -> builder.option(PostgresqlConnectionFactoryProvider.OPTIONS, options);\n }\n\n}\n\n')])])]),a("p",[e._v("When a "),a("code",[e._v("ConnectionFactory")]),e._v(" bean is available, the regular JDBC "),a("code",[e._v("DataSource")]),e._v(" auto-configuration backs off.\nIf you want to retain the JDBC "),a("code",[e._v("DataSource")]),e._v(" auto-configuration, and are comfortable with the risk of using the blocking JDBC API in a reactive application, add "),a("code",[e._v("@Import(DataSourceAutoConfiguration.class)")]),e._v(" on a "),a("code",[e._v("@Configuration")]),e._v(" class in your application to re-enable it.")]),e._v(" "),a("h4",{attrs:{id:"_1-7-1-embedded-database-support"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-7-1-embedded-database-support"}},[e._v("#")]),e._v(" 1.7.1. Embedded Database Support")]),e._v(" "),a("p",[e._v("Similarly to "),a("RouterLink",{attrs:{to:"/en/spring-boot/features.html#data.sql.datasource.embedded"}},[e._v("the JDBC support")]),e._v(", Spring Boot can automatically configure an embedded database for reactive usage.\nYou need not provide any connection URLs.\nYou need only include a build dependency to the embedded database that you want to use, as shown in the following example:")],1),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("\n io.r2dbc\n r2dbc-h2\n runtime\n\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("If you are using this feature in your tests, you may notice that the same database is reused by your whole test suite regardless of the number of application contexts that you use."),a("br"),e._v("If you want to make sure that each context has a separate embedded database, you should set "),a("code",[e._v("spring.r2dbc.generate-unique-name")]),e._v(" to "),a("code",[e._v("true")]),e._v(".")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_1-7-2-using-databaseclient"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-7-2-using-databaseclient"}},[e._v("#")]),e._v(" 1.7.2. Using DatabaseClient")]),e._v(" "),a("p",[e._v("A "),a("code",[e._v("DatabaseClient")]),e._v(" bean is auto-configured, and you can "),a("code",[e._v("@Autowire")]),e._v(" it directly into your own beans, as shown in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('import java.util.Map;\n\nimport reactor.core.publisher.Flux;\n\nimport org.springframework.r2dbc.core.DatabaseClient;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class MyBean {\n\n private final DatabaseClient databaseClient;\n\n public MyBean(DatabaseClient databaseClient) {\n this.databaseClient = databaseClient;\n }\n\n // ...\n\n public Flux