diff --git a/framework-docs/framework-docs.gradle b/framework-docs/framework-docs.gradle index 04b9d03fdb1..67f6d18dcf3 100644 --- a/framework-docs/framework-docs.gradle +++ b/framework-docs/framework-docs.gradle @@ -60,12 +60,15 @@ repositories { dependencies { api(project(":spring-context")) + api(project(":spring-jdbc")) api(project(":spring-jms")) api(project(":spring-web")) api("org.jetbrains.kotlin:kotlin-stdlib") api("jakarta.jms:jakarta.jms-api") api("jakarta.servlet:jakarta.servlet-api") + api("org.apache.commons:commons-dbcp2:2.11.0") + api("com.mchange:c3p0:0.9.5.5") implementation(project(":spring-core-test")) implementation("org.assertj:assertj-core") diff --git a/framework-docs/modules/ROOT/pages/data-access/jdbc/connections.adoc b/framework-docs/modules/ROOT/pages/data-access/jdbc/connections.adoc index a448b8121f4..1c3913bd4c3 100644 --- a/framework-docs/modules/ROOT/pages/data-access/jdbc/connections.adoc +++ b/framework-docs/modules/ROOT/pages/data-access/jdbc/connections.adoc @@ -46,47 +46,9 @@ To configure a `DriverManagerDataSource`: for the correct value.) . Provide a username and a password to connect to the database. -The following example shows how to configure a `DriverManagerDataSource` in Java: +The following example shows how to configure a `DriverManagerDataSource`: -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes",role="primary"] ----- - DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); - dataSource.setUrl("jdbc:hsqldb:hsql://localhost:"); - dataSource.setUsername("sa"); - dataSource.setPassword(""); ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] ----- - val dataSource = DriverManagerDataSource().apply { - setDriverClassName("org.hsqldb.jdbcDriver") - url = "jdbc:hsqldb:hsql://localhost:" - username = "sa" - password = "" - } ----- -====== - -The following example shows the corresponding XML configuration: - -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - ----- +include-code::./DriverManagerDataSourceConfiguration[tag=dataSourceBean,indent=0] The next two examples show the basic connectivity and configuration for DBCP and C3P0. To learn about more options that help control the pooling features, see the product @@ -94,32 +56,11 @@ documentation for the respective connection pooling implementations. The following example shows DBCP configuration: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - ----- +include-code::./BasicDataSourceConfiguration[tag=dataSourceBean,indent=0] The following example shows C3P0 configuration: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - ----- - +include-code::./ComboPooledDataSourceConfiguration[tag=dataSourceBean,indent=0] [[jdbc-DataSourceUtils]] == Using `DataSourceUtils` diff --git a/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/BasicDataSourceConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/BasicDataSourceConfiguration.java new file mode 100644 index 00000000000..56edfda55b3 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/BasicDataSourceConfiguration.java @@ -0,0 +1,39 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.dataaccess.jdbc.jdbcdatasource; + +import org.apache.commons.dbcp2.BasicDataSource; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +class BasicDataSourceConfiguration { + + // tag::dataSourceBean[] + @Bean(destroyMethod = "close") + BasicDataSource dataSource() { + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); + dataSource.setUrl("jdbc:hsqldb:hsql://localhost:"); + dataSource.setUsername("sa"); + dataSource.setPassword(""); + return dataSource; + } + // end::dataSourceBean[] + +} diff --git a/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/ComboPooledDataSourceConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/ComboPooledDataSourceConfiguration.java new file mode 100644 index 00000000000..ece4a521fa9 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/ComboPooledDataSourceConfiguration.java @@ -0,0 +1,40 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.docs.dataaccess.jdbc.jdbcdatasource; + +import java.beans.PropertyVetoException; + +import com.mchange.v2.c3p0.ComboPooledDataSource; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +class ComboPooledDataSourceConfiguration { + + // tag::dataSourceBean[] + @Bean(destroyMethod = "close") + ComboPooledDataSource dataSource() throws PropertyVetoException { + ComboPooledDataSource dataSource = new ComboPooledDataSource(); + dataSource.setDriverClass("org.hsqldb.jdbcDriver"); + dataSource.setJdbcUrl("jdbc:hsqldb:hsql://localhost:"); + dataSource.setUser("sa"); + dataSource.setPassword(""); + return dataSource; + } + // end::dataSourceBean[] + +} diff --git a/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/DriverManagerDataSourceConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/DriverManagerDataSourceConfiguration.java new file mode 100644 index 00000000000..d93e00bd221 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/DriverManagerDataSourceConfiguration.java @@ -0,0 +1,38 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.dataaccess.jdbc.jdbcdatasource; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.DriverManagerDataSource; + +@Configuration +class DriverManagerDataSourceConfiguration { + + // tag::dataSourceBean[] + @Bean + DriverManagerDataSource dataSource() { + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); + dataSource.setUrl("jdbc:hsqldb:hsql://localhost:"); + dataSource.setUsername("sa"); + dataSource.setPassword(""); + return dataSource; + } + // end::dataSourceBean[] + +} diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/BasicDataSourceConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/BasicDataSourceConfiguration.kt new file mode 100644 index 00000000000..6473fc28267 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/BasicDataSourceConfiguration.kt @@ -0,0 +1,21 @@ +package org.springframework.docs.dataaccess.jdbc.jdbcdatasource + +import org.apache.commons.dbcp2.BasicDataSource +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + +@Configuration +class BasicDataSourceConfiguration { + + // tag::dataSourceBean[] + @Bean(destroyMethod = "close") + fun dataSource(): BasicDataSource { + val dataSource = BasicDataSource() + dataSource.driverClassName = "org.hsqldb.jdbcDriver" + dataSource.url = "jdbc:hsqldb:hsql://localhost:" + dataSource.username = "sa" + dataSource.password = "" + return dataSource + } + // end::dataSourceBean[] +} diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/ComboPooledDataSourceConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/ComboPooledDataSourceConfiguration.kt new file mode 100644 index 00000000000..73072815cd0 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/ComboPooledDataSourceConfiguration.kt @@ -0,0 +1,22 @@ +package org.springframework.docs.dataaccess.jdbc.jdbcdatasource + +import com.mchange.v2.c3p0.ComboPooledDataSource +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + +@Configuration +internal class ComboPooledDataSourceConfiguration { + + // tag::dataSourceBean[] + @Bean(destroyMethod = "close") + fun dataSource(): ComboPooledDataSource { + val dataSource = ComboPooledDataSource() + dataSource.driverClass = "org.hsqldb.jdbcDriver" + dataSource.jdbcUrl = "jdbc:hsqldb:hsql://localhost:" + dataSource.user = "sa" + dataSource.password = "" + return dataSource + } + // end::dataSourceBean[] + +} diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/DriverManagerDataSourceConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/DriverManagerDataSourceConfiguration.kt new file mode 100644 index 00000000000..08959feeac2 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/DriverManagerDataSourceConfiguration.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2002-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.dataaccess.jdbc.jdbcdatasource + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.jdbc.datasource.DriverManagerDataSource + +@Configuration +class DriverManagerDataSourceConfiguration { + + // tag::dataSourceBean[] + @Bean + fun dataSource() = DriverManagerDataSource().apply { + setDriverClassName("org.hsqldb.jdbcDriver") + url = "jdbc:hsqldb:hsql://localhost:" + username = "sa" + password = "" + } + // end::dataSourceBean[] + +} diff --git a/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/BasicDataSourceConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/BasicDataSourceConfiguration.xml new file mode 100644 index 00000000000..01d9d5e2b5d --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/BasicDataSourceConfiguration.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/ComboPooledDataSourceConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/ComboPooledDataSourceConfiguration.xml new file mode 100644 index 00000000000..c931cd3383c --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/ComboPooledDataSourceConfiguration.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/DriverManagerDataSourceConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/DriverManagerDataSourceConfiguration.xml new file mode 100644 index 00000000000..5adf7095470 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcdatasource/DriverManagerDataSourceConfiguration.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + +