Skip to content

Commit aafe8ef

Browse files
committed
added "lenientFallback" flag to AbstractRoutingDataSource (SPR-6809)
1 parent f0bb45a commit aafe8ef

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,13 +43,14 @@ public abstract class AbstractRoutingDataSource extends AbstractDataSource imple
4343

4444
private Object defaultTargetDataSource;
4545

46+
private boolean lenientFallback = true;
47+
4648
private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
4749

4850
private Map<Object, DataSource> resolvedDataSources;
4951

5052
private DataSource resolvedDefaultDataSource;
5153

52-
5354
/**
5455
* Specify the map of target DataSources, with the lookup key as key.
5556
* The mapped value can either be a corresponding {@link javax.sql.DataSource}
@@ -66,7 +67,7 @@ public void setTargetDataSources(Map<Object, Object> targetDataSources) {
6667

6768
/**
6869
* Specify the default target DataSource, if any.
69-
* The mapped value can either be a corresponding {@link javax.sql.DataSource}
70+
* <p>The mapped value can either be a corresponding {@link javax.sql.DataSource}
7071
* instance or a data source name String (to be resolved via a
7172
* {@link #setDataSourceLookup DataSourceLookup}).
7273
* <p>This DataSource will be used as target if none of the keyed
@@ -77,6 +78,23 @@ public void setDefaultTargetDataSource(Object defaultTargetDataSource) {
7778
this.defaultTargetDataSource = defaultTargetDataSource;
7879
}
7980

81+
/**
82+
* Specify whether to apply a lenient fallback to the default DataSource
83+
* if no specific DataSource could be found for the current lookup key.
84+
* <p>Default is "true", accepting lookup keys without a corresponding entry
85+
* in the target DataSource map - simply falling back to the default DataSource
86+
* in that case.
87+
* <p>Switch this flag to "false" if you would prefer the fallback to only apply
88+
* if the lookup key was <code>null</code>. Lookup keys without a DataSource
89+
* entry will then lead to an IllegalStateException.
90+
* @see #setTargetDataSources
91+
* @see #setDefaultTargetDataSource
92+
* @see #determineCurrentLookupKey()
93+
*/
94+
public void setLenientFallback(boolean lenientFallback) {
95+
this.lenientFallback = lenientFallback;
96+
}
97+
8098
/**
8199
* Set the DataSourceLookup implementation to use for resolving data source
82100
* name Strings in the {@link #setTargetDataSources targetDataSources} map.
@@ -146,7 +164,7 @@ protected DataSource determineTargetDataSource() {
146164
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
147165
Object lookupKey = determineCurrentLookupKey();
148166
DataSource dataSource = this.resolvedDataSources.get(lookupKey);
149-
if (dataSource == null) {
167+
if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
150168
dataSource = this.resolvedDefaultDataSource;
151169
}
152170
if (dataSource == null) {

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/IsolationLevelDataSourceRouter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2006 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -103,11 +103,11 @@ public class IsolationLevelDataSourceRouter extends AbstractRoutingDataSource {
103103
@Override
104104
protected Object resolveSpecifiedLookupKey(Object lookupKey) {
105105
if (lookupKey instanceof Integer) {
106-
return (Integer) lookupKey;
106+
return lookupKey;
107107
}
108108
else if (lookupKey instanceof String) {
109109
String constantName = (String) lookupKey;
110-
if (constantName == null || !constantName.startsWith(DefaultTransactionDefinition.PREFIX_ISOLATION)) {
110+
if (!constantName.startsWith(DefaultTransactionDefinition.PREFIX_ISOLATION)) {
111111
throw new IllegalArgumentException("Only isolation constants allowed");
112112
}
113113
return constants.asNumber(constantName);

0 commit comments

Comments
 (0)