1 package org.apache.torque.dsfactory; 2 3 21 22 import javax.sql.ConnectionPoolDataSource ; 23 import javax.sql.DataSource ; 24 25 import org.apache.commons.configuration.Configuration; 26 27 import org.apache.commons.dbcp.datasources.SharedPoolDataSource; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 import org.apache.torque.Torque; 33 import org.apache.torque.TorqueException; 34 35 42 public class SharedPoolDataSourceFactory 43 extends AbstractDataSourceFactory 44 { 45 46 47 private static Log log 48 = LogFactory.getLog(SharedPoolDataSourceFactory.class); 49 50 51 private SharedPoolDataSource ds = null; 52 53 56 public DataSource getDataSource() 57 { 58 return ds; 59 } 60 61 64 public void initialize(Configuration configuration) throws TorqueException 65 { 66 super.initialize(configuration); 67 68 ConnectionPoolDataSource cpds = initCPDS(configuration); 69 SharedPoolDataSource dataSource = initJdbc2Pool(configuration); 70 dataSource.setConnectionPoolDataSource(cpds); 71 this.ds = dataSource; 72 } 73 74 81 private SharedPoolDataSource initJdbc2Pool(Configuration configuration) 82 throws TorqueException 83 { 84 log.debug("Starting initJdbc2Pool"); 85 SharedPoolDataSource dataSource = new SharedPoolDataSource(); 86 Configuration c = Torque.getConfiguration(); 87 88 if (c == null || c.isEmpty()) 89 { 90 log.warn("Global Configuration not set," 91 + " no Default pool data source configured!"); 92 } 93 else 94 { 95 Configuration conf = c.subset(DEFAULT_POOL_KEY); 96 applyConfiguration(conf, dataSource); 97 } 98 99 Configuration conf = configuration.subset(POOL_KEY); 100 applyConfiguration(conf, dataSource); 101 return dataSource; 102 } 103 104 105 109 public void close() throws TorqueException 110 { 111 try 112 { 113 ds.close(); 114 } 115 catch (Exception e) 116 { 117 log.error("Exception caught during close()", e); 118 throw new TorqueException(e); 119 } 120 ds = null; 121 } 122 } 123 | Popular Tags |