From 5bcf5c6f7cac0ff5f1a7ba78ad9d7b7d2df7fb52 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 24 Jul 2023 11:21:07 +0200 Subject: [PATCH 1/2] Clarify DataAccessException/ScriptException declarations for R2DBC Closes gh-30932 --- .../r2dbc/connection/init/DatabasePopulator.java | 10 +++++----- .../init/ResourceDatabasePopulator.java | 4 ++-- .../r2dbc/connection/init/ScriptUtils.java | 14 +++++++------- .../r2dbc/core/ConnectionAccessor.java | 15 +++++++++------ .../r2dbc/core/DatabaseClient.java | 6 +++--- .../r2dbc/core/DefaultDatabaseClient.java | 5 ++--- 6 files changed, 28 insertions(+), 26 deletions(-) diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/DatabasePopulator.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/DatabasePopulator.java index 467a25549cf..8adf9831b30 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/DatabasePopulator.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/DatabasePopulator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -20,7 +20,6 @@ import io.r2dbc.spi.Connection; import io.r2dbc.spi.ConnectionFactory; import reactor.core.publisher.Mono; -import org.springframework.dao.DataAccessException; import org.springframework.r2dbc.connection.ConnectionFactoryUtils; import org.springframework.util.Assert; @@ -44,17 +43,18 @@ public interface DatabasePopulator { * already configured and ready to use, must not be {@code null} * @return {@link Mono} that initiates script execution and is * notified upon completion - * @throws ScriptException in all other error cases + * @throws ScriptException in case of any errors */ - Mono populate(Connection connection) throws ScriptException; + Mono populate(Connection connection); /** * Execute the given {@link DatabasePopulator} against the given {@link ConnectionFactory}. * @param connectionFactory the {@link ConnectionFactory} to execute against * @return {@link Mono} that initiates {@link DatabasePopulator#populate(Connection)} * and is notified upon completion + * @throws ScriptException in case of any errors */ - default Mono populate(ConnectionFactory connectionFactory) throws DataAccessException { + default Mono populate(ConnectionFactory connectionFactory) { Assert.notNull(connectionFactory, "ConnectionFactory must not be null"); return Mono.usingWhen(ConnectionFactoryUtils.getConnection(connectionFactory), // this::populate, // diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ResourceDatabasePopulator.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ResourceDatabasePopulator.java index a6aeac31364..86dc2f4fcf2 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ResourceDatabasePopulator.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ResourceDatabasePopulator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2023 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. @@ -260,7 +260,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator { @Override - public Mono populate(Connection connection) throws ScriptException { + public Mono populate(Connection connection) { Assert.notNull(connection, "Connection must not be null"); return Flux.fromIterable(this.scripts).concatMap(resource -> { EncodedResource encodedScript = new EncodedResource(resource, this.sqlScriptEncoding); diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptUtils.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptUtils.java index c374d68c05a..18437ddca93 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptUtils.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -125,7 +125,7 @@ public abstract class ScriptUtils { * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection */ - public static Mono executeSqlScript(Connection connection, Resource resource) throws ScriptException { + public static Mono executeSqlScript(Connection connection, Resource resource) { return executeSqlScript(connection, new EncodedResource(resource)); } @@ -149,7 +149,7 @@ public abstract class ScriptUtils { * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection */ - public static Mono executeSqlScript(Connection connection, EncodedResource resource) throws ScriptException { + public static Mono executeSqlScript(Connection connection, EncodedResource resource) { return executeSqlScript(connection, resource, DefaultDataBufferFactory.sharedInstance, false, false, DEFAULT_COMMENT_PREFIXES, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER); @@ -189,7 +189,7 @@ public abstract class ScriptUtils { public static Mono executeSqlScript(Connection connection, EncodedResource resource, DataBufferFactory dataBufferFactory, boolean continueOnError, boolean ignoreFailedDrops, String commentPrefix, @Nullable String separator, String blockCommentStartDelimiter, - String blockCommentEndDelimiter) throws ScriptException { + String blockCommentEndDelimiter) { return executeSqlScript(connection, resource, dataBufferFactory, continueOnError, ignoreFailedDrops, new String[] { commentPrefix }, separator, @@ -230,7 +230,7 @@ public abstract class ScriptUtils { public static Mono executeSqlScript(Connection connection, EncodedResource resource, DataBufferFactory dataBufferFactory, boolean continueOnError, boolean ignoreFailedDrops, String[] commentPrefixes, @Nullable String separator, String blockCommentStartDelimiter, - String blockCommentEndDelimiter) throws ScriptException { + String blockCommentEndDelimiter) { if (logger.isDebugEnabled()) { logger.debug("Executing SQL script from " + resource); @@ -365,7 +365,7 @@ public abstract class ScriptUtils { */ static boolean containsStatementSeparator(EncodedResource resource, String script, String separator, String[] commentPrefixes, String blockCommentStartDelimiter, - String blockCommentEndDelimiter) throws ScriptException { + String blockCommentEndDelimiter) { boolean inSingleQuote = false; boolean inDoubleQuote = false; @@ -448,7 +448,7 @@ public abstract class ScriptUtils { */ static List splitSqlScript(EncodedResource resource, String script, String separator, String[] commentPrefixes, String blockCommentStartDelimiter, - String blockCommentEndDelimiter) throws ScriptException { + String blockCommentEndDelimiter) { Assert.hasText(script, "'script' must not be null or empty"); Assert.notNull(separator, "'separator' must not be null"); diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/core/ConnectionAccessor.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/core/ConnectionAccessor.java index 9f794ce6710..d3424708960 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/core/ConnectionAccessor.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/core/ConnectionAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2023 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. @@ -22,8 +22,6 @@ import io.r2dbc.spi.Connection; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import org.springframework.dao.DataAccessException; - /** * Interface declaring methods that accept callback {@link Function} * to operate within the scope of a {@link Connection}. @@ -31,13 +29,16 @@ import org.springframework.dao.DataAccessException; * close the connection as the connections may be pooled or be * subject to other kinds of resource management. * - *

Callback functions are responsible for creating a + *

Callback functions are responsible for creating a * {@link org.reactivestreams.Publisher} that defines the scope of how * long the allocated {@link Connection} is valid. Connections are * released after the publisher terminates. * + *

This serves as a base interface for {@link DatabaseClient}. + * * @author Mark Paluch * @since 5.3 + * @see DatabaseClient */ public interface ConnectionAccessor { @@ -49,8 +50,9 @@ public interface ConnectionAccessor { * {@link Function} closure, otherwise resources may get defunct. * @param action the callback object that specifies the connection action * @return the resulting {@link Mono} + * @throws org.springframework.dao.DataAccessException in case of any errors */ - Mono inConnection(Function> action) throws DataAccessException; + Mono inConnection(Function> action); /** * Execute a callback {@link Function} within a {@link Connection} scope. @@ -60,7 +62,8 @@ public interface ConnectionAccessor { * {@link Function} closure, otherwise resources may get defunct. * @param action the callback object that specifies the connection action * @return the resulting {@link Flux} + * @throws org.springframework.dao.DataAccessException in case of any errors */ - Flux inConnectionMany(Function> action) throws DataAccessException; + Flux inConnectionMany(Function> action); } diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/core/DatabaseClient.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/core/DatabaseClient.java index 1086ef49339..f0884d07f27 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/core/DatabaseClient.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/core/DatabaseClient.java @@ -37,9 +37,9 @@ import org.springframework.r2dbc.core.binding.BindMarkersFactory; import org.springframework.util.Assert; /** - * A non-blocking, reactive client for performing database calls with - * Reactive Streams back pressure. Provides a higher level, common API over - * R2DBC client libraries. + * A non-blocking, reactive client for performing database calls with Reactive Streams + * back pressure. Provides a higher level, common API over R2DBC client libraries. + * Propagates {@link org.springframework.dao.DataAccessException} variants for errors. * *

Use the static factory method {@link #create(ConnectionFactory)} or obtain * a {@linkplain DatabaseClient#builder() builder} to create an instance. diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/core/DefaultDatabaseClient.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/core/DefaultDatabaseClient.java index 18c485be1c9..a781664d804 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/core/DefaultDatabaseClient.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/core/DefaultDatabaseClient.java @@ -47,7 +47,6 @@ import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import org.springframework.dao.DataAccessException; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.lang.Nullable; import org.springframework.r2dbc.connection.ConnectionFactoryUtils; @@ -108,7 +107,7 @@ class DefaultDatabaseClient implements DatabaseClient { } @Override - public Mono inConnection(Function> action) throws DataAccessException { + public Mono inConnection(Function> action) { Assert.notNull(action, "Callback object must not be null"); Mono connectionMono = getConnection().map( connection -> new ConnectionCloseHolder(connection, this::closeConnection)); @@ -130,7 +129,7 @@ class DefaultDatabaseClient implements DatabaseClient { } @Override - public Flux inConnectionMany(Function> action) throws DataAccessException { + public Flux inConnectionMany(Function> action) { Assert.notNull(action, "Callback object must not be null"); Mono connectionMono = getConnection().map( connection -> new ConnectionCloseHolder(connection, this::closeConnection)); From fdf1418dfbbdd7c06945d6223594c509bd194294 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 24 Jul 2023 11:21:13 +0200 Subject: [PATCH 2/2] Polishing --- .../DefaultListableBeanFactoryTests.java | 74 +++++++++---------- .../dao/support/DataAccessUtils.java | 3 +- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index b9ed5deebe5..76962198046 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -1370,10 +1370,10 @@ class DefaultListableBeanFactoryTests { lbf.registerBeanDefinition("rod2", bd2); lbf.setParameterNameDiscoverer(new DefaultParameterNameDiscoverer()); - assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> - lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false)) - .withMessageContaining("rod") - .withMessageContaining("rod2"); + assertThatExceptionOfType(UnsatisfiedDependencyException.class) + .isThrownBy(() -> lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false)) + .withMessageContaining("rod") + .withMessageContaining("rod2"); } @Test @@ -1441,11 +1441,11 @@ class DefaultListableBeanFactoryTests { bd2.setDependsOn("tb1"); lbf.registerBeanDefinition("tb2", bd2); - assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> - lbf.preInstantiateSingletons()) - .withMessageContaining("Circular") - .withMessageContaining("'tb2'") - .withMessageContaining("'tb1'"); + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(() -> lbf.preInstantiateSingletons()) + .withMessageContaining("Circular") + .withMessageContaining("'tb2'") + .withMessageContaining("'tb1'"); } @Test @@ -1460,11 +1460,11 @@ class DefaultListableBeanFactoryTests { bd3.setDependsOn("tb1"); lbf.registerBeanDefinition("tb3", bd3); - assertThatExceptionOfType(BeanCreationException.class).isThrownBy( - lbf::preInstantiateSingletons) - .withMessageContaining("Circular") - .withMessageContaining("'tb3'") - .withMessageContaining("'tb1'"); + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(lbf::preInstantiateSingletons) + .withMessageContaining("Circular") + .withMessageContaining("'tb3'") + .withMessageContaining("'tb1'"); } @Test @@ -1607,10 +1607,10 @@ class DefaultListableBeanFactoryTests { lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); - assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() -> - lbf.getBean(TestBean.class)) - .withMessageContaining("Multiple beans found with the same priority") - .withMessageContaining("5"); // conflicting priority + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class) + .isThrownBy(() -> lbf.getBean(TestBean.class)) + .withMessageContaining("Multiple beans found with the same priority") + .withMessageContaining("5"); // conflicting priority } @Test @@ -1815,9 +1815,9 @@ class DefaultListableBeanFactoryTests { lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); - assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() -> - lbf.getBean(ConstructorDependency.class, 42)) - .withMessageContaining("more than one 'primary'"); + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class) + .isThrownBy(() -> lbf.getBean(ConstructorDependency.class, 42)) + .withMessageContaining("more than one 'primary'"); } @Test @@ -2004,10 +2004,10 @@ class DefaultListableBeanFactoryTests { lbf.registerBeanDefinition("test", bd); lbf.registerBeanDefinition("spouse", bd2); - assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> - lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true)) - .withMessageContaining("test") - .withMessageContaining("spouse"); + assertThatExceptionOfType(UnsatisfiedDependencyException.class) + .isThrownBy(() -> lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true)) + .withMessageContaining("test") + .withMessageContaining("spouse"); } @Test @@ -2071,10 +2071,10 @@ class DefaultListableBeanFactoryTests { lbf.registerBeanDefinition("test", bd); lbf.registerBeanDefinition("spouse", bd2); - assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> - lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true)) - .withCauseExactlyInstanceOf(NoUniqueBeanDefinitionException.class) - .withMessageContaining("5"); + assertThatExceptionOfType(UnsatisfiedDependencyException.class) + .isThrownBy(() -> lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true)) + .withCauseExactlyInstanceOf(NoUniqueBeanDefinitionException.class) + .withMessageContaining("5"); } @Test @@ -2337,20 +2337,20 @@ class DefaultListableBeanFactoryTests { void beanDefinitionWithInterface() { lbf.registerBeanDefinition("test", new RootBeanDefinition(ITestBean.class)); - assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> - lbf.getBean("test")) - .withMessageContaining("interface") - .satisfies(ex -> assertThat(ex.getBeanName()).isEqualTo("test")); + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(() -> lbf.getBean("test")) + .withMessageContaining("interface") + .satisfies(ex -> assertThat(ex.getBeanName()).isEqualTo("test")); } @Test void beanDefinitionWithAbstractClass() { lbf.registerBeanDefinition("test", new RootBeanDefinition(AbstractBeanFactory.class)); - assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> - lbf.getBean("test")) - .withMessageContaining("abstract") - .satisfies(ex -> assertThat(ex.getBeanName()).isEqualTo("test")); + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(() -> lbf.getBean("test")) + .withMessageContaining("abstract") + .satisfies(ex -> assertThat(ex.getBeanName()).isEqualTo("test")); } @Test diff --git a/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java b/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java index 20720b36e19..7f7df7776a0 100644 --- a/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java +++ b/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java @@ -29,7 +29,8 @@ import org.springframework.util.NumberUtils; /** * Miscellaneous utility methods for DAO implementations. - * Useful with any data access technology. + * + *

Useful with any data access technology. * * @author Juergen Hoeller * @since 1.0.2