Announcement Announcement Module
Collapse
No announcement yet.
datasource that uses jndi but fallsback to properties? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • datasource that uses jndi but fallsback to properties?

    Hi All,

    On a project at a previous organization I remember seeing datasources configured that looked datasources up via jndi but fell back on properties provided by a file if the jndi lookup failed.

    I've been searching the documentation for this but so far no luck. Any pointers would be apperciated.

    Thanks,
    James

  • #2
    You will have to use a custom datasource factory. Here is some sample code to get you started

    Code:
    import java.util.Properties;
    
    import javax.sql.DataSource;
    
    import org.springframework.beans.factory.FactoryBean;
    
    public class CustomDataSourceFactory implements FactoryBean<DataSource>{
    
    	private Properties jndiDataSourceProperties=null;
    	private DataSource fallbackDataSource=null;
    	private DataSource jndiDataSource=null;
    	
    	public DataSource getObject() throws Exception {
    		DataSource datasource=null;
    		try
    		{
    			datasource=getJndiDataSource(jndiDataSourceProperties);
    			if(datasource==null)	
    				return getFallbackDataSource();
    			}
    		catch(Exception e)
    		{
    			
    		}
    		return datasource;
    	}
    
    	private DataSource getFallbackDataSource() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	private DataSource getJndiDataSource(Properties jndiDataSourceProperties2) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    	public Properties getJndiDataSourceProperties() {
    		return jndiDataSourceProperties;
    	}
    
    	public void setJndiDataSourceProperties(Properties jndiDataSourceProperties) {
    		this.jndiDataSourceProperties = jndiDataSourceProperties;
    	}
    
    	public void setFallbackDataSource(DataSource fallbackDataSource) {
    		this.fallbackDataSource = fallbackDataSource;
    	}
    
    	
    
    	public Class getObjectType() {
    		return DataSource.class;
    	}
    
    	public boolean isSingleton() {
    		return false;
    	}
    
    }

    Comment

    Working...
    X