1 24 package edu.rice.rubis.servlets; 25 26 import java.io.FileInputStream ; 27 import java.io.FileNotFoundException ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.sql.Connection ; 31 import java.sql.DriverManager ; 32 import java.sql.SQLException ; 33 import java.util.EmptyStackException ; 34 import java.util.Properties ; 35 import java.util.Stack ; 36 37 import javax.servlet.ServletException ; 38 import javax.servlet.UnavailableException ; 39 import javax.servlet.http.HttpServlet ; 40 41 45 public abstract class RubisHttpServlet extends HttpServlet 46 { 47 48 private static final boolean enablePooling = false; 49 50 private Stack freeConnections = null; 51 private int poolSize; 52 private Properties dbProperties = null; 53 54 public abstract int getPoolSize(); 56 57 public void init() throws ServletException 58 { 59 InputStream in = null; 60 poolSize = getPoolSize(); 61 try 62 { 63 dbProperties = new Properties (); 65 in = new FileInputStream (Config.DatabaseProperties); 66 dbProperties.load(in); 67 Class.forName(dbProperties.getProperty("datasource.classname")); 69 70 freeConnections = new Stack (); 71 initializeConnections(); 72 } 73 catch (FileNotFoundException f) 74 { 75 throw new UnavailableException ("Couldn't find file mysql.properties: " 76 + f + "<br>"); 77 } 78 catch (IOException io) 79 { 80 throw new UnavailableException ("Cannot open read mysql.properties: " + io 81 + "<br>"); 82 } 83 catch (ClassNotFoundException c) 84 { 85 throw new UnavailableException ("Couldn't load database driver: " + c 86 + "<br>"); 87 } 88 catch (SQLException s) 89 { 90 throw new UnavailableException ("Couldn't get database connection: " + s 91 + "<br>"); 92 } 93 finally 94 { 95 try 96 { 97 if (in != null) 98 in.close(); 99 } 100 catch (Exception e) 101 { 102 } 103 } 104 } 105 106 112 public synchronized void initializeConnections() throws SQLException 113 { 114 if (enablePooling) 115 for (int i = 0; i < poolSize; i++) 116 { 117 freeConnections.push( 119 DriverManager.getConnection( 120 dbProperties.getProperty("datasource.url"), 121 dbProperties.getProperty("datasource.username"), 122 dbProperties.getProperty("datasource.password"))); 123 } 124 125 } 126 127 132 public void closeConnection(Connection connection) 133 { 134 try 135 { 136 connection.close(); 137 } 138 catch (Exception e) 139 { 140 141 } 142 } 143 144 149 public synchronized Connection getConnection() 150 { 151 if (enablePooling) 152 { 153 try 154 { 155 while (freeConnections.isEmpty()) 157 { 158 try 159 { 160 wait(); 161 } 162 catch (InterruptedException e) 163 { 164 System.out.println("Connection pool wait interrupted."); 165 } 166 } 167 168 169 Connection c = (Connection ) freeConnections.pop(); 170 return c; 171 } 172 catch (EmptyStackException e) 173 { 174 System.out.println("Out of connections."); 175 return null; 176 } 177 } 178 else 179 { 180 try 181 { 182 return DriverManager.getConnection( 183 dbProperties.getProperty("datasource.url"), 184 dbProperties.getProperty("datasource.username"), 185 dbProperties.getProperty("datasource.password")); 186 } 187 catch (SQLException ex) 188 { 189 return null; 190 } 191 } 192 } 193 194 199 public synchronized void releaseConnection(Connection c) 200 { 201 if (enablePooling) 202 { 203 boolean mustNotify = freeConnections.isEmpty(); 204 freeConnections.push(c); 205 if (mustNotify) 207 notifyAll(); 208 } 209 else 210 { 211 closeConnection(c); 212 } 213 214 } 215 216 221 public synchronized void finalizeConnections() throws SQLException 222 { 223 if (enablePooling) 224 { 225 Connection c = null; 226 while (!freeConnections.isEmpty()) 227 { 228 c = (Connection ) freeConnections.pop(); 229 c.close(); 230 } 231 } 232 } 233 234 237 public void destroy() 238 { 239 try 240 { 241 finalizeConnections(); 242 } 243 catch (SQLException e) 244 { 245 } 246 } 247 248 } | Popular Tags |