1 package org.smartlib.pool.core; 2 3 import org.apache.log4j.Logger; 4 5 import java.util.Map ; 6 import java.util.HashMap ; 7 import java.util.Set ; 8 import java.sql.Connection ; 9 10 17 public class MultiPoolImpl implements MultiPool, MultiPoolMonitor { 18 19 private static Logger logger = Logger.getLogger(MultiPoolImpl.class); 20 21 private Map poolHash = new HashMap (); 22 private PoolConfig config; 23 private String name; 24 private String [] poolNames; 25 private int lastPoolServed = -1; 26 27 private static ThreadLocal threadLocal = new ThreadLocal (); 29 30 MultiPoolImpl(PoolConfig config) throws ConnectionPoolException { 31 this.config = config; 32 this.name = config.getMultiPoolName(); 33 initialize(); 34 } 35 36 40 private void initialize() throws ConnectionPoolException { 41 logger.debug("Loading Multi Pool: " + name); 42 if (config.isExternalPooling()) { 43 if (logger.isDebugEnabled()) { 44 logger.debug("External pooling is switched ON"); 45 } 46 PoolConfig.ConnectionLoaderClass[] connectLoaderClasses = config.getConnectionLoaderClass(); 47 48 if (connectLoaderClasses != null && connectLoaderClasses.length !=0) { 49 for (int i=0; i<connectLoaderClasses.length; i++) { 50 if (logger.isDebugEnabled()) { 51 logger.debug("Loading Connection pool: " + connectLoaderClasses[i].getName()); 52 } 53 Pool pool = new ConnectionPool(config, connectLoaderClasses[i].getName()); 54 poolHash.put(connectLoaderClasses[i].getName(), pool); 55 } 56 } 57 } 58 else { 59 PoolConfig.ConnectionString[] connectionStrings = config.getConnectionString(); 60 logger.debug("External pooling is switched OFF"); 61 for (int i=0; i<connectionStrings.length; i++) { 62 if (logger.isDebugEnabled()) { 63 logger.debug("Loading Connection pool: " + connectionStrings[i].getName()); 64 } 65 Pool pool = new ConnectionPool(config, connectionStrings[i].getName()); 66 poolHash.put(connectionStrings[i].getName(), pool); 67 } 68 } 69 Set set = poolHash.keySet(); 70 poolNames = (String [])set.toArray(new String [set.size()]); 71 72 } 73 74 public Connection getConnection() throws ConnectionPoolException { 75 return getPool().getConnection(); 76 } 77 78 public Connection getConnection(String owner) throws ConnectionPoolException { 79 return getPool().getConnection(owner); 80 } 81 82 public void addConnectionLeakListener(ConnectionLeakListener cle) throws ConnectionPoolException { 83 for (int i=0; i<poolNames.length; i++) { 84 Pool pool = (Pool)poolHash.get(poolNames[i]); 85 pool.addConnectionLeakListener(cle); 86 } 87 } 88 89 public void removeConnectionLeakListener(ConnectionLeakListener cle) throws ConnectionPoolException { 90 for (int i=0; i<poolNames.length; i++) { 91 Pool pool = (Pool)poolHash.get(poolNames[i]); 92 pool.removeConnectionLeakListener(cle); 93 } 94 } 95 96 public void shutDown() { 97 for (int i=0; i<poolNames.length; i++) { 98 Pool pool = (Pool)poolHash.get(poolNames[i]); 99 pool.shutDown(); 100 } 101 } 102 103 public PoolMonitor[] getPoolMonitors() { 104 PoolMonitor[] monitors = new PoolMonitor[poolNames.length]; 105 for (int i=0; i<poolNames.length; i++) { 106 Pool pool = (Pool)poolHash.get(poolNames[i]); 107 monitors[i] = (PoolMonitor)pool; 108 } 109 return monitors; 110 } 111 112 public String getName() { 113 return name; 114 } 115 116 119 private Pool getPool() { 120 if (config.isThreadStickiness()) { 121 if (logger.isDebugEnabled()){ 122 logger.debug("Thread stickiness is configured"); 123 } 124 ThreadContext context = (ThreadContext)threadLocal.get(); 125 if (context != null) { 126 if (logger.isDebugEnabled()) { 127 logger.debug("Thread is already associated with pool: " + context.getPoolName()); 128 } 129 return (Pool)poolHash.get(context.getPoolName()); 130 } else { 131 if (logger.isDebugEnabled()) { 132 logger.debug("Thread is not associated with a pool"); 133 } 134 Pool p = getRandomPool(); 135 context = new ThreadContext(); 136 context.setPoolName(((PoolMonitor)p).getName()); 137 threadLocal.set(context); 138 if (logger.isDebugEnabled()) { 139 logger.debug("Thread now associated with a pool:" + ((PoolMonitor)p).getName()); 140 } 141 return p; 142 } 143 144 } 145 else { 146 if (logger.isDebugEnabled()){ 147 logger.debug("Thread stickiness is not configured"); 148 } 149 return getRandomPool(); 150 } 151 } 152 153 private Pool getRandomPool() { 154 if (lastPoolServed == poolNames.length - 1) 157 lastPoolServed = 0; 158 else 159 lastPoolServed++; 160 Pool p = (Pool)poolHash.get(poolNames[lastPoolServed]); 161 if (logger.isDebugEnabled()) { 162 logger.debug("Randomely Returning pool:" + ((PoolMonitor)p).getName()); 163 } 164 return p; 165 } 166 167 public static void main (String args[]) { 168 Double number = Math.random(); 169 int i = ((int)((Math.abs(number)*100)%(2))); 170 System.out.println("The number is: " + i); 171 } 172 173 174 } 175 | Popular Tags |