1 8 package org.apache.avalon.excalibur.datasource; 9 10 import java.sql.Connection ; 11 import java.sql.SQLException ; 12 13 import org.apache.avalon.excalibur.datasource.DataSourceComponent; 14 import org.apache.avalon.excalibur.datasource.JdbcConnectionFactory; 15 import org.apache.avalon.excalibur.datasource.NoAvailableConnectionException; 16 import org.apache.avalon.framework.activity.Disposable; 17 import org.apache.avalon.framework.configuration.Configuration; 18 import org.apache.avalon.framework.configuration.ConfigurationException; 19 import org.apache.avalon.framework.logger.AbstractLogEnabled; 20 21 55 public class ResourceLimitingJdbcDataSource 56 extends AbstractLogEnabled 57 implements DataSourceComponent, Disposable 58 { 59 private boolean m_configured; 60 private boolean m_disposed; 61 protected ResourceLimitingJdbcConnectionPool m_pool; 62 63 66 public ResourceLimitingJdbcDataSource() {} 67 68 71 80 public Connection getConnection() 81 throws SQLException 82 { 83 if ( !m_configured ) throw new IllegalStateException ( "Not Configured" ); 84 if ( m_disposed ) throw new IllegalStateException ( "Already Disposed" ); 85 86 Connection connection; 87 try 88 { 89 connection = (Connection )m_pool.get(); 90 } 91 catch ( SQLException e ) 92 { 93 if (getLogger().isWarnEnabled()) 94 { 95 getLogger().warn("Could not return Connection", e); 96 } 97 98 throw e; 99 } 100 catch ( Exception e ) 101 { 102 if ( getLogger().isWarnEnabled() ) 103 { 104 getLogger().warn( "Could not return Connection", e ); 105 } 106 107 throw new NoAvailableConnectionException( e.getMessage() ); 108 } 109 110 return connection; 111 } 112 113 116 123 public void configure(Configuration configuration) throws ConfigurationException { 124 if (m_configured) throw new IllegalStateException ("Already Configured"); 125 126 final String driver = configuration.getChild( "driver" ).getValue( "" ); 127 final String dburl = configuration.getChild( "dburl" ).getValue( null ); 128 final String user = configuration.getChild( "user" ).getValue( null ); 129 final String passwd = configuration.getChild( "password" ).getValue( null ); 130 131 final Configuration controller = configuration.getChild( "pool-controller" ); 132 String keepAlive = controller.getChild( "keep-alive" ).getValue( "SELECT 1" ); 133 final boolean disableKeepAlive = controller.getChild( "keep-alive" ).getAttributeAsBoolean( "disable", false ); 134 135 final int max = controller.getAttributeAsInteger( "max", 3 ); 136 final boolean blocking = controller.getAttributeAsBoolean( "blocking", true ); 137 final long timeout = controller.getAttributeAsLong ( "timeout", -1 ); 138 final long trimInterval = controller.getAttributeAsLong ( "trim-interval", 60000 ); 139 final boolean oradb = controller.getAttributeAsBoolean( "oradb", false ); 140 141 final boolean autoCommit = configuration.getChild("auto-commit").getValueAsBoolean(true); 142 final String connectionClass = controller.getAttribute( "connection-class", null ); 144 145 final int l_max; 146 147 if ( ! "".equals(driver) ) 149 { 150 if (getLogger().isDebugEnabled()) 151 { 152 getLogger().debug("Loading new driver: " + driver); 153 } 154 155 try 156 { 157 Class.forName( driver, true, Thread.currentThread().getContextClassLoader() ); 158 } 159 catch (ClassNotFoundException cnfe) 160 { 161 if (getLogger().isWarnEnabled()) 162 { 163 getLogger().warn( "Could not load driver: " + driver, cnfe ); 164 } 165 } 166 } 167 168 if( max < 1 ) 170 { 171 if (getLogger().isWarnEnabled()) 172 { 173 getLogger().warn( "Maximum number of connections specified must be at least 1." ); 174 } 175 176 l_max = 1; 177 } 178 else 179 { 180 l_max = max; 181 } 182 183 if (disableKeepAlive) 185 { 186 keepAlive = null; 187 } 188 189 if (oradb) 193 { 194 keepAlive = "SELECT 1 FROM DUAL"; 195 196 if (getLogger().isWarnEnabled()) 197 { 198 getLogger().warn("The oradb attribute is deprecated, please use the" + 199 "keep-alive element instead."); 200 } 201 } 202 203 204 final JdbcConnectionFactory factory = new JdbcConnectionFactory 205 ( dburl, user, passwd, autoCommit, keepAlive, connectionClass ); 206 207 factory.enableLogging( getLogger() ); 208 209 try 210 { 211 m_pool = new ResourceLimitingJdbcConnectionPool 212 (factory, l_max, true, blocking, timeout, trimInterval, autoCommit ); 213 m_pool.enableLogging( getLogger() ); 214 } 215 catch (Exception e) 216 { 217 if (getLogger().isDebugEnabled()) 218 { 219 getLogger().debug("Error configuring ResourceLimitingJdbcDataSource", e); 220 } 221 222 throw new ConfigurationException("Error configuring ResourceLimitingJdbcDataSource", e); 223 } 224 225 m_configured = true; 226 } 227 228 231 237 public void dispose() { 238 m_disposed = true; 239 m_pool.dispose(); 240 m_pool = null; 241 } 242 } 243 244 | Popular Tags |