1 8 package org.apache.avalon.excalibur.datasource; 9 10 import org.apache.avalon.framework.logger.AbstractLogEnabled; 11 import org.apache.avalon.excalibur.pool.ObjectFactory; 12 13 import java.lang.reflect.Constructor ; 14 import java.sql.DriverManager ; 15 import java.sql.Connection ; 16 17 24 public class JdbcConnectionFactory extends AbstractLogEnabled implements ObjectFactory 25 { 26 private final String m_dburl; 27 private final String m_username; 28 private final String m_password; 29 private final boolean m_autoCommit; 30 private final String m_keepAlive; 31 private final Class m_class; 32 private final static String DEFAULT_KEEPALIVE = "SELECT 1"; 33 private final static String ORACLE_KEEPALIVE = JdbcConnectionFactory.DEFAULT_KEEPALIVE + " FROM DUAL"; 34 private Connection m_firstConnection; 35 36 40 public JdbcConnectionFactory( final String url, 41 final String username, 42 final String password, 43 final boolean autoCommit, 44 final boolean oradb ) 45 { 46 this(url, username, password, autoCommit, oradb, null); 47 } 48 49 53 public JdbcConnectionFactory( final String url, 54 final String username, 55 final String password, 56 final boolean autoCommit, 57 final boolean oradb, 58 final String connectionClass) 59 { 60 this(url, username, password, autoCommit, (oradb) ? JdbcConnectionFactory.ORACLE_KEEPALIVE : JdbcConnectionFactory.DEFAULT_KEEPALIVE, connectionClass); 61 } 62 63 76 public JdbcConnectionFactory( final String url, 77 final String username, 78 final String password, 79 final boolean autoCommit, 80 final String keepAlive, 81 final String connectionClass) 82 { 83 this.m_dburl = url; 84 this.m_username = username; 85 this.m_password = password; 86 this.m_autoCommit = autoCommit; 87 this.m_keepAlive = keepAlive; 88 89 Class clazz = null; 90 91 try 92 { 93 if( null == m_username ) 94 { 95 m_firstConnection = DriverManager.getConnection( m_dburl ); 96 } 97 else 98 { 99 m_firstConnection = DriverManager.getConnection( m_dburl, m_username, m_password ); 100 } 101 102 String className = connectionClass; 103 if ( null == className ) 104 { 105 try 106 { 107 java.lang.reflect.Method meth = m_firstConnection.getClass().getMethod("getHoldability", new Class [] {}); 108 className = "org.apache.avalon.excalibur.datasource.Jdbc3Connection"; 109 } 110 catch (Exception e) 111 { 112 className = "org.apache.avalon.excalibur.datasource.JdbcConnection"; 113 } 114 } 115 116 clazz = Thread.currentThread().getContextClassLoader().loadClass( className ); 117 } 118 catch (Exception e) 119 { 120 } 122 123 this.m_class = clazz; 124 } 125 126 public Object newInstance() throws Exception 127 { 128 AbstractJdbcConnection jdbcConnection = null; 129 Connection connection = m_firstConnection; 130 131 if ( null == connection ) 132 { 133 if( null == m_username ) 134 { 135 connection = DriverManager.getConnection( m_dburl ); 136 } 137 else 138 { 139 connection = DriverManager.getConnection( m_dburl, m_username, m_password ); 140 } 141 } 142 else 143 { 144 m_firstConnection = null; 145 } 146 147 if ( null != this.m_class ) 148 { 149 try 150 { 151 Class [] paramTypes = new Class [] { Connection .class, String .class }; 152 Object [] params = new Object [] { connection, this.m_keepAlive }; 153 154 Constructor constructor = m_class.getConstructor( paramTypes ); 155 jdbcConnection = (AbstractJdbcConnection) constructor.newInstance( params ); 156 } 157 catch ( Exception e ) 158 { 159 try 160 { 161 boolean oracleKeepAlive = ( m_keepAlive != null ) && m_keepAlive.equalsIgnoreCase( JdbcConnectionFactory.ORACLE_KEEPALIVE ); 163 164 Class [] paramTypes = new Class [] { Connection .class, boolean.class }; 165 Object [] params = new Object [] { connection, new Boolean ( oracleKeepAlive ) }; 166 167 Constructor constructor = m_class.getConstructor( paramTypes ); 168 jdbcConnection = (AbstractJdbcConnection) constructor.newInstance( params ); 169 } 170 catch ( Exception ie ) 171 { 172 if ( getLogger().isDebugEnabled() ) 173 { 174 getLogger().debug("Exception in JdbcConnectionFactory.newInstance:", ie); 175 } 176 177 throw new NoValidConnectionException(ie.getMessage()); 178 } 179 } 180 } 181 else 182 { 183 throw new NoValidConnectionException("No valid JdbcConnection class available"); 184 } 185 186 jdbcConnection.enableLogging(getLogger()); 187 188 if (jdbcConnection.getAutoCommit() != m_autoCommit) { 190 jdbcConnection.setAutoCommit(m_autoCommit); 191 } 192 193 if (getLogger().isDebugEnabled()) 194 { 195 getLogger().debug( "JdbcConnection object created" ); 196 } 197 198 return jdbcConnection; 199 } 200 201 public Class getCreatedClass() 202 { 203 return m_class; 204 } 205 206 public void decommission(Object object) throws Exception 207 { 208 if (object instanceof AbstractJdbcConnection) { 209 ((AbstractJdbcConnection) object).dispose(); 210 } 211 } 212 } 213 | Popular Tags |