added "lenientFallback" flag to AbstractRoutingDataSource (SPR-6809)
This commit is contained in:
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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 Object defaultTargetDataSource;
|
||||||
|
|
||||||
|
private boolean lenientFallback = true;
|
||||||
|
|
||||||
private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
|
private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
|
||||||
|
|
||||||
private Map<Object, DataSource> resolvedDataSources;
|
private Map<Object, DataSource> resolvedDataSources;
|
||||||
|
|
||||||
private DataSource resolvedDefaultDataSource;
|
private DataSource resolvedDefaultDataSource;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify the map of target DataSources, with the lookup key as key.
|
* Specify the map of target DataSources, with the lookup key as key.
|
||||||
* The mapped value can either be a corresponding {@link javax.sql.DataSource}
|
* 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.
|
* Specify the default target DataSource, if any.
|
||||||
* The mapped value can either be a corresponding {@link javax.sql.DataSource}
|
* <p>The mapped value can either be a corresponding {@link javax.sql.DataSource}
|
||||||
* instance or a data source name String (to be resolved via a
|
* instance or a data source name String (to be resolved via a
|
||||||
* {@link #setDataSourceLookup DataSourceLookup}).
|
* {@link #setDataSourceLookup DataSourceLookup}).
|
||||||
* <p>This DataSource will be used as target if none of the keyed
|
* <p>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;
|
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.
|
||||||
|
* <p>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.
|
||||||
|
* <p>Switch this flag to "false" if you would prefer the fallback to only apply
|
||||||
|
* if the lookup key was <code>null</code>. 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
|
* Set the DataSourceLookup implementation to use for resolving data source
|
||||||
* name Strings in the {@link #setTargetDataSources targetDataSources} map.
|
* 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");
|
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
|
||||||
Object lookupKey = determineCurrentLookupKey();
|
Object lookupKey = determineCurrentLookupKey();
|
||||||
DataSource dataSource = this.resolvedDataSources.get(lookupKey);
|
DataSource dataSource = this.resolvedDataSources.get(lookupKey);
|
||||||
if (dataSource == null) {
|
if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
|
||||||
dataSource = this.resolvedDefaultDataSource;
|
dataSource = this.resolvedDefaultDataSource;
|
||||||
}
|
}
|
||||||
if (dataSource == null) {
|
if (dataSource == null) {
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -103,11 +103,11 @@ public class IsolationLevelDataSourceRouter extends AbstractRoutingDataSource {
|
|||||||
@Override
|
@Override
|
||||||
protected Object resolveSpecifiedLookupKey(Object lookupKey) {
|
protected Object resolveSpecifiedLookupKey(Object lookupKey) {
|
||||||
if (lookupKey instanceof Integer) {
|
if (lookupKey instanceof Integer) {
|
||||||
return (Integer) lookupKey;
|
return lookupKey;
|
||||||
}
|
}
|
||||||
else if (lookupKey instanceof String) {
|
else if (lookupKey instanceof String) {
|
||||||
String constantName = (String) lookupKey;
|
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");
|
throw new IllegalArgumentException("Only isolation constants allowed");
|
||||||
}
|
}
|
||||||
return constants.asNumber(constantName);
|
return constants.asNumber(constantName);
|
||||||
|
|||||||
Reference in New Issue
Block a user