1 24 25 package edu.rice.rubbos.servlets; 26 27 import java.io.FileInputStream ; 28 import java.io.FileNotFoundException ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.sql.Connection ; 32 import java.sql.DriverManager ; 33 import java.sql.SQLException ; 34 import java.util.EmptyStackException ; 35 import java.util.Properties ; 36 import java.util.Stack ; 37 38 import javax.servlet.ServletException ; 39 import javax.servlet.UnavailableException ; 40 import javax.servlet.http.HttpServlet ; 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 44 48 public abstract class RubbosHttpServlet extends HttpServlet 49 { 50 51 private static final boolean enablePooling = true; 52 53 private Stack freeConnections = null; 54 private int poolSize; 55 private int currentConn = 0; 56 private Properties dbProperties = null; 57 58 public abstract int getPoolSize(); 60 61 public void init() throws ServletException 62 { 63 InputStream in = null; 64 poolSize = getPoolSize(); 65 try 66 { 67 dbProperties = new Properties (); 69 in = new FileInputStream (Config.DatabaseProperties); 70 dbProperties.load(in); 71 Class.forName(dbProperties.getProperty("datasource.classname")); 73 74 freeConnections = new Stack (); 75 initializeConnections(); 76 } 77 catch (FileNotFoundException f) 78 { 79 throw new UnavailableException ( 80 "Couldn't find file mysql.properties: " + f + "<br>"); 81 } 82 catch (IOException io) 83 { 84 throw new UnavailableException ( 85 "Cannot open read mysql.properties: " + io + "<br>"); 86 } 87 catch (ClassNotFoundException c) 88 { 89 throw new UnavailableException ( 90 "Couldn't load database driver: " + c + "<br>"); 91 } 92 catch (SQLException s) 93 { 94 throw new UnavailableException ( 95 "Couldn't get database connection: " + s + "<br>"); 96 } 97 finally 98 { 99 try 100 { 101 if (in != null) 102 in.close(); 103 } 104 catch (Exception e) 105 { 106 } 107 } 108 } 109 110 117 public synchronized void initializeConnections() throws SQLException 118 { 119 if (enablePooling) 120 { 121 for (int i = 0; i < poolSize; i++) 122 { 123 freeConnections.push( 125 DriverManager.getConnection( 126 dbProperties.getProperty("datasource.url"), 127 dbProperties.getProperty("datasource.username"), 128 dbProperties.getProperty("datasource.password"))); 129 } 130 } 131 } 132 133 151 152 156 private void closeConnection(Connection connection) 157 { 158 try 159 { 160 connection.close(); 161 } 162 catch (Exception e) 163 { 164 165 } 166 } 167 168 174 public synchronized Connection getConnection() 175 { 176 if (enablePooling) 177 { 178 try 179 { 180 while (freeConnections.isEmpty()) 182 { 183 try 184 { 185 wait(); 186 } 187 catch (InterruptedException e) 188 { 189 System.out.println("Connection pool wait interrupted."); 190 } 191 } 192 193 Connection c = (Connection ) freeConnections.pop(); 194 return c; 195 } 196 197 catch (EmptyStackException e) 198 { 199 System.out.println("Out of connections."); 200 return null; 201 } 202 } 203 else 204 { 205 try 206 { 207 return DriverManager.getConnection( 208 dbProperties.getProperty("datasource.url"), 209 dbProperties.getProperty("datasource.username"), 210 dbProperties.getProperty("datasource.password")); 211 } 212 catch (SQLException ex) 213 { 214 ex.printStackTrace(); 215 return null; 216 } 217 } 218 } 219 220 225 public synchronized void releaseConnection(Connection c) 226 { 227 if (enablePooling) 228 { 229 boolean mustNotify = freeConnections.isEmpty(); 230 freeConnections.push(c); 231 if (mustNotify) 233 notifyAll(); 234 } 235 else 236 { 237 closeConnection(c); 238 } 239 } 240 241 246 public synchronized void finalizeConnections() throws SQLException 247 { 248 if (enablePooling) 249 { 250 Connection c = null; 251 while (!freeConnections.isEmpty()) 252 { 253 c = (Connection ) freeConnections.pop(); 254 c.close(); 255 } 256 } 257 } 258 259 public void doGet(HttpServletRequest request, HttpServletResponse response) 260 throws IOException , ServletException 261 { 262 263 } 264 265 public void doPost(HttpServletRequest request, HttpServletResponse response) 266 throws IOException , ServletException 267 { 268 269 } 270 271 274 public void destroy() 275 { 276 try 277 { 278 finalizeConnections(); 279 } 280 catch (SQLException e) 281 { 282 } 283 } 284 285 } 286 | Popular Tags |