1 29 30 package com.caucho.sql; 31 32 import com.caucho.loader.EnvironmentLocal; 33 import com.caucho.util.L10N; 34 35 import javax.sql.DataSource ; 36 import java.util.HashMap ; 37 import java.util.logging.Logger ; 38 39 43 public class DatabaseManager { 44 protected static final Logger log 45 = Logger.getLogger(DatabaseManager.class.getName()); 46 private static final L10N L = new L10N(DatabaseManager.class); 47 48 private static final EnvironmentLocal<DatabaseManager> _localManager 49 = new EnvironmentLocal<DatabaseManager>(); 50 51 private final HashMap <String ,DBPool> _databaseMap 52 = new HashMap <String ,DBPool>(); 53 54 57 private DatabaseManager() 58 { 59 } 60 61 64 private static DatabaseManager getLocalManager() 65 { 66 synchronized (_localManager) { 67 DatabaseManager manager = _localManager.getLevel(); 68 69 if (manager == null) { 70 manager = new DatabaseManager(); 71 72 _localManager.set(manager); 73 } 74 75 return manager; 76 } 77 } 78 79 82 public static DataSource findDatabase(String driver, String url) 83 throws Exception 84 { 85 return getLocalManager().findDatabaseImpl(driver, url); 86 } 87 88 91 private DataSource findDatabaseImpl(String driverName, String url) 92 throws Exception 93 { 94 synchronized (_databaseMap) { 95 DBPool db = _databaseMap.get(url); 96 97 if (db == null) { 98 db = new DBPool(); 99 100 DriverConfig driver = db.createDriver(); 101 102 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 103 104 Class driverClass = Class.forName(driverName, false, loader); 105 106 driver.setType(driverClass); 107 driver.setURL(url); 108 109 db.init(); 110 111 _databaseMap.put(url, db); 112 } 113 114 return db; 115 } 116 } 117 } 118 119 | Popular Tags |