1 18 package freecs.auth.sqlConnectionPool; 19 20 import java.sql.*; 21 import freecs.Server; 22 import freecs.auth.SQLAuthenticator; 23 import freecs.core.CanceledRequestException; 24 import freecs.core.ConnectionBuffer; 25 26 public class ConnectionPool { 27 28 final SQLAuthenticator authenticator; 29 final DbProperties dbProps; 30 final PoolElement pool[]; 31 volatile int p = 0, idCnt=0; 32 33 39 public ConnectionPool (SQLAuthenticator authenticator, DbProperties dbProps) { 40 this.authenticator = authenticator; 41 this.dbProps = dbProps; 42 pool = new PoolElement[dbProps.poolsize]; 43 if (Server.TRACE_CREATE_AND_FINALIZE) { 44 Server.log (this, "++++++++++++++++++++++++++++++++++++++++CREATE", Server.MSG_STATE, Server.LVL_VERY_VERBOSE); 45 } 46 } 47 48 51 public void shutdown () { 52 synchronized (this.pool) { 53 for (int i = 0; i < pool.length; i++) { 54 if (pool[i] == null) { 55 continue; 56 } 57 pool[i].cleanup (); 58 pool[i]=null; 59 } 60 } 61 } 62 63 64 69 private PoolElement createPoolElement () throws Exception { 70 idCnt++; 71 if (idCnt == Integer.MAX_VALUE) { 72 idCnt = 0; 73 } 74 Connection con = DriverManager.getConnection (dbProps.url, dbProps.conProps); 75 return new PoolElement (this, con, dbProps, idCnt); 76 } 77 78 public PoolElement getPoolElement (int retrys, ConnectionBuffer cb) throws Exception { 79 for (int i = 0; i<retrys; i++) { 81 if ((cb != null 82 && !cb.isValid()) 83 || Thread.currentThread().isInterrupted()) 84 throw new CanceledRequestException ("ConnectionBuffer has been invalidated"); 85 86 PoolElement el=null; 87 try { 88 el = this.getPoolElement(cb); 89 if (el!=null) return el; 91 } catch (CanceledRequestException cre) { 92 throw cre; 93 } catch (Exception e) { 94 Server.debug(Thread.currentThread() , this.toString() + "getPoolElement: ", e, Server.MSG_AUTH, Server.LVL_MAJOR); 95 el = null; 96 } 97 } 98 throw new Exception ("Unable to get available PoolElement"); 99 } 100 101 106 private PoolElement getPoolElement (ConnectionBuffer cb) throws Exception { 107 for (int i = 0; i < dbProps.poolsize; i++) { 108 if ((cb != null 109 && !cb.isValid()) 110 || Thread.currentThread().isInterrupted()) 111 throw new CanceledRequestException ("ConnectionBuffer has been invalidated"); 112 p++; 113 if (p > dbProps.poolsize - 1) 114 p=0; 115 synchronized (this.pool) { 116 if (pool[p]==null) { 117 pool[p] = createPoolElement(); 118 } 119 switch (pool[p].grab()) { 120 case PoolElement.INVALID: 121 pool[p].cleanup(); 122 pool[p]=createPoolElement(); 123 case PoolElement.IDLE: 124 return pool[p]; 125 case PoolElement.ACTIVE: 126 continue; 127 } 128 } 129 } 130 return null; 131 } 132 133 public int size() { 134 return pool.length; 135 } 136 137 public void finalize() { 138 if (Server.TRACE_CREATE_AND_FINALIZE) 139 Server.log(this, "----------------------------------------FINALIZED", Server.MSG_STATE, Server.LVL_VERY_VERBOSE); 140 } 141 142 private ConnectionPool() throws Exception { throw new Exception ("not instatiable without arguments"); } 143 } | Popular Tags |