From 4561a6a9c39b758d12750f92eaa8beb6c7ffeb69 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 9 Feb 2010 15:39:54 +0000 Subject: [PATCH] added "lenientFallback" flag to AbstractRoutingDataSource (SPR-6809) git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2938 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../lookup/AbstractRoutingDataSource.java | 26 ++++++++++++++++--- .../IsolationLevelDataSourceRouter.java | 6 ++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java index e22859961db..02071e9fcfd 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 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. @@ -43,13 +43,14 @@ public abstract class AbstractRoutingDataSource extends AbstractDataSource imple private Object defaultTargetDataSource; + private boolean lenientFallback = true; + private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); private Map resolvedDataSources; private DataSource resolvedDefaultDataSource; - /** * Specify the map of target DataSources, with the lookup key as key. * The mapped value can either be a corresponding {@link javax.sql.DataSource} @@ -66,7 +67,7 @@ public abstract class AbstractRoutingDataSource extends AbstractDataSource imple /** * Specify the default target DataSource, if any. - * The mapped value can either be a corresponding {@link javax.sql.DataSource} + *

The mapped value can either be a corresponding {@link javax.sql.DataSource} * instance or a data source name String (to be resolved via a * {@link #setDataSourceLookup DataSourceLookup}). *

This DataSource will be used as target if none of the keyed @@ -77,6 +78,23 @@ public abstract class AbstractRoutingDataSource extends AbstractDataSource imple this.defaultTargetDataSource = defaultTargetDataSource; } + /** + * Specify whether to apply a lenient fallback to the default DataSource + * if no specific DataSource could be found for the current lookup key. + *

Default is "true", accepting lookup keys without a corresponding entry + * in the target DataSource map - simply falling back to the default DataSource + * in that case. + *

Switch this flag to "false" if you would prefer the fallback to only apply + * if the lookup key was null. Lookup keys without a DataSource + * entry will then lead to an IllegalStateException. + * @see #setTargetDataSources + * @see #setDefaultTargetDataSource + * @see #determineCurrentLookupKey() + */ + public void setLenientFallback(boolean lenientFallback) { + this.lenientFallback = lenientFallback; + } + /** * Set the DataSourceLookup implementation to use for resolving data source * name Strings in the {@link #setTargetDataSources targetDataSources} map. @@ -146,7 +164,7 @@ public abstract class AbstractRoutingDataSource extends AbstractDataSource imple Assert.notNull(this.resolvedDataSources, "DataSource router not initialized"); Object lookupKey = determineCurrentLookupKey(); DataSource dataSource = this.resolvedDataSources.get(lookupKey); - if (dataSource == null) { + if (dataSource == null && (this.lenientFallback || lookupKey == null)) { dataSource = this.resolvedDefaultDataSource; } if (dataSource == null) { diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/IsolationLevelDataSourceRouter.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/IsolationLevelDataSourceRouter.java index c9d99065fe3..f194ca56266 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/IsolationLevelDataSourceRouter.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/IsolationLevelDataSourceRouter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2006 the original author or authors. + * Copyright 2002-2010 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. @@ -103,11 +103,11 @@ public class IsolationLevelDataSourceRouter extends AbstractRoutingDataSource { @Override protected Object resolveSpecifiedLookupKey(Object lookupKey) { if (lookupKey instanceof Integer) { - return (Integer) lookupKey; + return lookupKey; } else if (lookupKey instanceof String) { String constantName = (String) lookupKey; - if (constantName == null || !constantName.startsWith(DefaultTransactionDefinition.PREFIX_ISOLATION)) { + if (!constantName.startsWith(DefaultTransactionDefinition.PREFIX_ISOLATION)) { throw new IllegalArgumentException("Only isolation constants allowed"); } return constants.asNumber(constantName);