1 8 package org.apache.avalon.excalibur.datasource; 9 10 import java.sql.Connection ; 11 import java.sql.SQLException ; 12 import javax.naming.Context ; 13 import javax.naming.InitialContext ; 14 import org.apache.avalon.framework.configuration.Configuration; 15 import org.apache.avalon.framework.configuration.ConfigurationException; 16 import org.apache.avalon.framework.logger.AbstractLogEnabled; 17 import org.apache.avalon.framework.logger.LogKitLogger; 18 import org.apache.avalon.framework.logger.Loggable; 19 import org.apache.avalon.excalibur.datasource.DataSourceComponent; 20 import com.informix.jdbcx.IfxConnectionPoolDataSource; 21 import com.informix.jdbcx.IfxDataSource; 22 23 51 public class InformixDataSource 52 extends AbstractLogEnabled 53 implements DataSourceComponent, Loggable 54 { 55 private IfxDataSource m_dataSource; 56 private boolean m_autocommit; 57 private static boolean INIT_FACTORY = false; 58 59 64 public InformixDataSource() 65 { 66 if ( ! InformixDataSource.INIT_FACTORY ) 67 { 68 System.setProperty( Context.INITIAL_CONTEXT_FACTORY, 69 "org.apache.avalon.excalibur.naming.memory.MemoryInitialContextFactory" ); 70 } 71 } 72 73 public void setLogger( final org.apache.log.Logger logger ) 74 { 75 enableLogging( new LogKitLogger( logger ) ); 76 } 77 78 81 public Connection getConnection() throws SQLException 82 { 83 Connection conn = m_dataSource.getConnection(); 84 85 if ( conn.getAutoCommit() != m_autocommit ) 86 { 87 conn.setAutoCommit(m_autocommit); 88 } 89 90 return conn; 91 } 92 93 96 public void configure(Configuration conf) throws ConfigurationException 97 { 98 Configuration poolController = conf.getChild("pool-controller"); 99 String dbname = conf.getChild("dbname").getValue("ifx"); 100 IfxConnectionPoolDataSource pooledDataSource = new IfxConnectionPoolDataSource(); 101 m_autocommit = conf.getChild("autocommit").getValueAsBoolean(true); 102 103 pooledDataSource.setIfxCPMInitPoolSize(poolController.getAttributeAsInteger("init", 5)); 104 pooledDataSource.setIfxCPMMinPoolSize(poolController.getAttributeAsInteger("min", 5)); 105 pooledDataSource.setIfxCPMMaxPoolSize(poolController.getAttributeAsInteger("max", 10)); 106 pooledDataSource.setIfxCPMServiceInterval(100); 107 pooledDataSource.setServerName(conf.getChild("servername").getValue()); 108 pooledDataSource.setDatabaseName(conf.getChild("dbname").getValue()); 109 pooledDataSource.setIfxIFXHOST(conf.getChild("host").getValue()); 110 pooledDataSource.setPortNumber(conf.getChild("host").getAttributeAsInteger("port")); 111 pooledDataSource.setUser(conf.getChild("user").getValue()); 112 pooledDataSource.setPassword(conf.getChild("password").getValue()); 113 114 try 115 { 116 Context context = new InitialContext (); 117 118 context.bind(dbname + "pool", pooledDataSource); 119 120 m_dataSource = new IfxDataSource(); 121 m_dataSource.setDataSourceName(dbname + "pool"); 122 m_dataSource.setServerName(conf.getChild("servername").getValue()); 123 m_dataSource.setDatabaseName(conf.getChild("dbname").getValue()); 124 m_dataSource.setIfxIFXHOST(conf.getChild("host").getValue()); 125 m_dataSource.setPortNumber(conf.getChild("host").getAttributeAsInteger("port")); 126 m_dataSource.setUser(conf.getChild("user").getValue()); 127 m_dataSource.setPassword(conf.getChild("password").getValue()); 128 129 context.bind(dbname, m_dataSource); 130 } 131 catch (Exception e) 132 { 133 if (getLogger().isErrorEnabled()) 134 { 135 getLogger().error("There was an error trying to bind the connection pool", e); 136 } 137 throw new ConfigurationException("There was an error trying to bind the connection pool", e); 138 } 139 } 140 } 141 | Popular Tags |