1 6 7 package org.jfox.jdbc.datasource; 8 9 import java.io.PrintWriter ; 10 import java.sql.Connection ; 11 import java.sql.DriverManager ; 12 import java.sql.SQLException ; 13 import javax.naming.NamingException ; 14 import javax.naming.Reference ; 15 import javax.naming.Referenceable ; 16 import javax.naming.StringRefAddr ; 17 import javax.sql.DataSource ; 18 19 import org.jfox.ioc.common.AbstractService; 20 import org.jfox.ioc.ext.ActiveComponent; 21 import org.jfox.jndi.InitialContextHelper; 22 23 26 public class PoolDataSource extends AbstractService implements DataSource , Referenceable , ActiveComponent { 27 protected ConnectionPool pool = null; private String dbDriver; private String dbURL; private String user; private String password; private int initNum = 2; 33 private int maxRest = 10; 34 35 private String dsName; 36 37 38 47 public PoolDataSource(String dsName, String dbDriver, String dbURL, String user, String password) throws Exception { 48 this.dsName = dsName; 49 this.dbDriver = dbDriver; 50 this.dbURL = dbURL; 51 this.user = user; 52 this.password = password; 53 } 54 55 61 public Connection getConnection() throws SQLException { 62 try { 65 return pool.getConnection(); 66 } 67 catch(SQLException sqle) { 68 throw sqle; 69 } 70 catch(Exception e) { 71 e.printStackTrace(); 72 throw new SQLException (e.getMessage()); 73 } 74 } 75 76 85 public Connection getConnection(String username, String password) 86 throws SQLException { 87 return DriverManager.getConnection(dbURL, username, password); 88 89 } 90 91 97 public PrintWriter getLogWriter() throws SQLException { 98 return DriverManager.getLogWriter(); 99 } 100 101 105 public void setLogWriter(PrintWriter out) throws SQLException { 106 DriverManager.setLogWriter(out); 107 } 108 109 113 public void setLoginTimeout(int seconds) throws SQLException { 114 DriverManager.setLoginTimeout(seconds); 115 } 116 117 121 public int getLoginTimeout() throws SQLException { 122 return DriverManager.getLoginTimeout(); 123 } 124 125 131 public Reference getReference() throws NamingException { 132 Reference ref = new Reference (getClass().getName(), DataSourceObjectFactory.class.getName(), null); 133 ref.add(new StringRefAddr ("dsName", dsName)); 134 ref.add(new StringRefAddr ("dbDriver", dbDriver)); 135 ref.add(new StringRefAddr ("dbURL", dbURL)); 136 ref.add(new StringRefAddr ("user", user)); 137 ref.add(new StringRefAddr ("password", password)); 138 ref.add(new StringRefAddr ("initNum", "" + initNum)); 139 ref.add(new StringRefAddr ("maxRest", "" + maxRest)); 140 return ref; 141 } 142 143 public String getDbDriver() { 144 return dbDriver; 145 } 146 147 public String getDbURL() { 148 return dbURL; 149 } 150 151 public String getUser() { 152 return user; 153 } 154 155 public String getPassword() { 156 return password; 157 } 158 159 public int getMaxRest() { 160 return maxRest; 161 } 162 163 public int getInitNum() { 164 return initNum; 165 } 166 167 public void setInitNum(int initNum) { 168 this.initNum = initNum; 169 } 170 171 public void setMaxRest(int maxRest) { 172 this.maxRest = maxRest; 173 } 174 175 protected void doInit() throws Exception { 176 pool = new ConnectionPool(dbDriver, dbURL, user, password); 177 pool.setInitNum(initNum); 178 pool.setMaxRest(maxRest); 179 pool.init(); 180 } 181 182 public String getDsName() { 183 return dsName; 184 } 185 186 protected void doDestroy() throws Exception { 187 pool.destroy(); 188 DataSourceObjectFactory.removeDataSource(dsName); 189 } 190 191 protected void doStart() throws Exception { 192 InitialContextHelper.getInitialContext().bind(getDsName(), this); 193 } 194 195 protected void doStop() throws Exception { 196 InitialContextHelper.getInitialContext().unbind(getDsName()); 197 } 198 199 public void run() { 200 } 201 202 } 203 | Popular Tags |