1 23 24 package org.webdocwf.util.loader; 25 26 import java.io.*; 27 import java.util.*; 28 import java.sql.*; 29 30 35 public class DBConnectionManager { 36 private Vector drivers=new Vector(); 37 private Hashtable pools=new Hashtable(); 38 private static DBConnectionManager instance; private Hashtable openConnections=new Hashtable(); 40 private Vector allConnectons=new Vector(); 41 42 public String loaderJobPath=""; 43 public String connectinPrefix=""; 44 public boolean fileSystemDatabase=false; 45 46 47 51 public DBConnectionManager(Vector allConnections) { 52 this.allConnectons=allConnections; 53 init(); 54 } 55 56 57 61 public void setLoaderJobPath(String loaderJob){ 62 File file=new File(loaderJob); 63 this.loaderJobPath=file.getAbsoluteFile().getParent()+System.getProperty("file.separator"); 64 } 65 66 70 public void setConnectionPrefix(String prefix){ 71 this.connectinPrefix=prefix; 72 } 73 74 78 public void setParsePermission(boolean doParse){ 79 this.fileSystemDatabase=doParse; 80 } 81 82 85 private void init(){ 86 87 loadDrivers(allConnectons); 88 createPools(allConnectons); 89 } 90 91 private void loadDrivers(Vector allConnectons){ 92 if(allConnectons.size()!=0){ 93 for(int i=0;i<allConnectons.size();i=i+4){ 94 if(i>3){ 95 if(allConnectons.get(i).toString().equalsIgnoreCase(allConnectons.get(i-4).toString())){ 96 }else{ 98 try{ 99 Driver driver=(Driver)Class.forName(allConnectons.get(i).toString()).newInstance(); 100 DriverManager.registerDriver(driver); 101 drivers.add(driver); 102 }catch(Exception e){ 103 e.getMessage(); 104 } 105 } 106 }else{ 107 try{ 108 Driver driver=(Driver)Class.forName(allConnectons.get(i).toString()).newInstance(); 109 DriverManager.registerDriver(driver); 110 drivers.add(driver); 111 }catch(Exception e){ 112 e.getMessage(); 113 } 114 } 115 } 116 } 117 } 118 119 private void createPools(Vector allConnectons){ 120 for(int i=0;i<allConnectons.size();i=i+4){ 121 String url=allConnectons.get(i+1).toString(); 122 if(url.indexOf("jdbc:microsoft:sqlserver")!=-1) { 123 if(url.indexOf("SelectMethod")==-1) { 124 url = url+";SelectMethod=cursor"; 125 } 126 } 127 String poolName=url; 128 String user=allConnectons.get(i+2).toString(); 129 String password=allConnectons.get(i+3).toString(); 130 boolean check=false; 131 if(pools.size()>0){ 132 check=pools.containsKey(poolName); 133 } 134 if(!check){ 135 DBConnectionPool pool=new DBConnectionPool(poolName,url,user,password); 136 pools.put(poolName,pool); 137 } 138 } 139 } 140 141 146 public Connection getConnection(String name){ 147 DBConnectionPool pool=(DBConnectionPool)pools.get(name); 148 if(pool!=null){ 149 return pool.getConnection(); 150 } 151 return null; 152 } 153 154 158 public void release(String exception){ 159 Enumeration allPools=pools.elements(); 160 while(allPools.hasMoreElements()){ 161 DBConnectionPool pool=(DBConnectionPool)allPools.nextElement(); 162 pool.release(exception); 163 } 164 Enumeration allDrivers=drivers.elements(); 165 while(allDrivers.hasMoreElements()){ 166 Driver driver=(Driver)allDrivers.nextElement(); 167 try{ 168 DriverManager.deregisterDriver(driver); 169 }catch(Exception e){ 170 e.getMessage(); 171 } 172 } 173 } 174 175 private class DBConnectionPool{ 176 private String name=null; 177 private String url=null; 178 private String user=null; 179 private String password=null; 180 181 182 189 public DBConnectionPool(String name,String url,String user,String password){ 190 this.name=name; 191 this.url=url; 192 this.user=user; 193 this.password=password; 194 195 } 196 197 202 public Connection getConnection(){ 203 Connection conn=null; 204 if(openConnections.size()>0){ 205 conn=(Connection)openConnections.get(name); 206 if(conn!=null){ 207 return conn; 208 } 209 conn=newConnections(); 210 }else{ 211 conn=newConnections(); 212 } 213 return conn; 214 } 215 216 private Connection newConnections(){ 217 Connection conn=null; 218 url=Utils.getAbsolutePathFromDatabaseURL(connectinPrefix,loaderJobPath, 219 url,fileSystemDatabase); 220 try{ 221 if(user==null){ 222 conn=DriverManager.getConnection(url); 223 openConnections.put(name,conn); 224 }else{ 225 conn=DriverManager.getConnection(url,user,password); 226 openConnections.put(name,conn); 227 } 228 229 }catch(Exception e){ 230 e.printStackTrace(); 231 } 232 return conn; 233 } 234 235 239 public void release(String exception){ 240 Connection conn=(Connection)openConnections.get(name); 241 try{ 242 if(!conn.isClosed()){ 243 if(exception.equalsIgnoreCase("false")) 244 conn.commit(); 245 conn.close(); 246 } 247 openConnections.remove(name); 248 }catch(Exception e){ 249 e.getMessage(); 250 } 251 } 252 253 } 254 } | Popular Tags |