1 8 package org.apache.avalon.excalibur.datasource; 9 10 import java.sql.Connection ; 11 import java.sql.SQLException ; 12 import org.apache.avalon.framework.activity.Disposable; 13 import org.apache.avalon.framework.configuration.Configuration; 14 import org.apache.avalon.framework.configuration.ConfigurationException; 15 import org.apache.avalon.framework.logger.AbstractLogEnabled; 16 import org.apache.avalon.framework.logger.LogKitLogger; 17 import org.apache.avalon.framework.logger.Loggable; 18 import org.apache.avalon.excalibur.pool.DefaultPoolController; 19 20 41 public class JdbcDataSource 42 extends AbstractLogEnabled 43 implements DataSourceComponent, Disposable, Loggable 44 { 45 protected JdbcConnectionPool m_pool; 46 47 public void setLogger( final org.apache.log.Logger logger ) 48 { 49 enableLogging( new LogKitLogger( logger ) ); 50 } 51 52 62 public void configure( final Configuration configuration ) 63 throws ConfigurationException 64 { 65 if( null == m_pool ) 66 { 67 final String driver = configuration.getChild( "driver" ).getValue(""); 68 final String dburl = configuration.getChild( "dburl" ).getValue(); 69 final String user = configuration.getChild( "user" ).getValue( null ); 70 final String passwd = configuration.getChild( "password" ).getValue( null ); 71 final Configuration controller = configuration.getChild( "pool-controller" ); 72 String keepAlive = controller.getChild( "keep-alive" ).getValue( "SELECT 1" ); 73 final boolean disableKeepAlive = controller.getChild( "keep-alive" ).getAttributeAsBoolean( "disable", false ); 74 75 final int min = controller.getAttributeAsInteger( "min", 1 ); 76 final int max = controller.getAttributeAsInteger( "max", 3 ); 77 final long timeout = controller.getAttributeAsLong( "timeout", -1 ); 78 final boolean autoCommit = configuration.getChild("auto-commit").getValueAsBoolean(true); 79 final boolean oradb = controller.getAttributeAsBoolean( "oradb", false ); 80 final String connectionClass = controller.getAttribute( "connection-class", null ); 82 83 final int l_max; 84 final int l_min; 85 86 if ( ! "".equals(driver) ) 88 { 89 if (getLogger().isDebugEnabled()) 90 { 91 getLogger().debug("Loading new driver: " + driver); 92 } 93 94 try 95 { 96 Class.forName( driver, true, Thread.currentThread().getContextClassLoader() ); 97 } 98 catch (ClassNotFoundException cnfe) 99 { 100 if (getLogger().isWarnEnabled()) 101 { 102 getLogger().warn( "Could not load driver: " + driver, cnfe ); 103 } 104 } 105 } 106 107 if ( min < 1 ) 109 { 110 if (getLogger().isWarnEnabled()) 111 { 112 getLogger().warn( "Minumum number of connections specified must be at least 1." ); 113 } 114 115 l_min = 1; 116 } 117 else 118 { 119 l_min = min; 120 } 121 122 if( max < 1 ) 123 { 124 if (getLogger().isWarnEnabled()) 125 { 126 getLogger().warn( "Maximum number of connections specified must be at least 1." ); 127 } 128 129 l_max = 1; 130 } 131 else 132 { 133 if ( max < min ) 134 { 135 if (getLogger().isWarnEnabled()) 136 { 137 getLogger().warn( "Maximum number of connections specified must be " + 138 "more than the minimum number of connections." ); 139 } 140 141 l_max = min + 1; 142 } 143 else 144 { 145 l_max = max; 146 } 147 } 148 149 if (disableKeepAlive) 151 { 152 keepAlive = null; 153 } 154 155 if (oradb) 159 { 160 keepAlive = "SELECT 1 FROM DUAL"; 161 162 if (getLogger().isWarnEnabled()) 163 { 164 getLogger().warn("The oradb attribute is deprecated, please use the" + 165 "keep-alive element instead."); 166 } 167 } 168 169 170 final JdbcConnectionFactory factory = 171 new JdbcConnectionFactory( dburl, user, passwd, autoCommit, keepAlive, connectionClass ); 172 final DefaultPoolController poolController = new DefaultPoolController(l_max / 4); 173 174 factory.enableLogging( getLogger() ); 175 176 try 177 { 178 m_pool = new JdbcConnectionPool( factory, poolController, l_min, l_max, autoCommit ); 179 m_pool.enableLogging( getLogger() ); 180 m_pool.setTimeout( timeout ); 181 m_pool.initialize(); 182 } 183 catch (Exception e) 184 { 185 if (getLogger().isDebugEnabled()) 186 { 187 getLogger().debug("Error configuring JdbcDataSource", e); 188 } 189 190 throw new ConfigurationException("Error configuring JdbcDataSource", e); 191 } 192 } 193 } 194 195 196 public Connection getConnection() 197 throws SQLException 198 { 199 try 200 { 201 return (Connection ) m_pool.get(); 202 } 203 catch( final SQLException se ) 204 { 205 if (getLogger().isWarnEnabled()) 206 { 207 getLogger().warn( "Could not return Connection", se ); 208 } 209 210 throw se; 212 } 213 catch( final Exception e ) 214 { 215 if (getLogger().isWarnEnabled()) 216 { 217 getLogger().warn( "Could not return Connection", e ); 218 } 219 220 throw new NoAvailableConnectionException( e.getMessage() ); 221 } 222 } 223 224 225 public void dispose() 226 { 227 m_pool.dispose(); 228 m_pool = null; 229 } 230 } 231 | Popular Tags |