1 16 17 package org.springframework.orm.ojb.support; 18 19 import java.sql.Connection ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import javax.sql.DataSource ; 24 25 import org.apache.ojb.broker.accesslayer.ConnectionFactoryManagedImpl; 26 import org.apache.ojb.broker.accesslayer.LookupException; 27 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor; 28 29 import org.springframework.beans.factory.BeanFactory; 30 31 58 public class LocalDataSourceConnectionFactory extends ConnectionFactoryManagedImpl { 59 60 63 protected static BeanFactory beanFactory; 64 65 69 private Map dataSources = new HashMap (); 70 71 public LocalDataSourceConnectionFactory() { 72 if (beanFactory == null) { 73 throw new IllegalStateException ("No BeanFactory found for configuration - " + 74 "LocalOjbConfigurer must be defined as Spring bean"); 75 } 76 } 77 78 public Connection lookupConnection(JdbcConnectionDescriptor jcd) throws LookupException { 79 try { 80 DataSource dataSource = null; 81 synchronized (this.dataSources) { 82 dataSource = (DataSource ) this.dataSources.get(jcd.getJcdAlias()); 83 if (dataSource == null) { 84 dataSource = getDataSource(jcd.getJcdAlias()); 85 this.dataSources.put(jcd.getJcdAlias(), dataSource); 86 } 87 } 88 return dataSource.getConnection(); 89 } 90 catch (Exception ex) { 91 throw new LookupException("Could not obtain connection from data source", ex); 92 } 93 } 94 95 102 protected DataSource getDataSource(String jcdAlias) { 103 return (DataSource ) beanFactory.getBean(jcdAlias, DataSource .class); 104 } 105 106 } 107 | Popular Tags |