1 package edu.rice.rubis.servlets; 2 3 import java.io.IOException ; 4 import java.sql.Connection ; 5 import java.sql.SQLException ; 6 7 import javax.naming.Context ; 8 import javax.naming.InitialContext ; 9 import javax.naming.NamingException ; 10 import javax.servlet.ServletException ; 11 import javax.servlet.http.HttpServlet ; 12 import javax.servlet.http.HttpServletRequest ; 13 import javax.servlet.http.HttpServletResponse ; 14 import javax.sql.DataSource ; 15 16 20 public abstract class RubisHttpServlet2 extends HttpServlet 21 { 22 private DataSource ds = null; 23 24 25 public void init() throws ServletException 26 { 27 try 28 { 29 Context ctx = new InitialContext (); 30 if (ctx == null) 31 throw new Exception ("Boom - No Context"); 32 33 ds = (DataSource ) ctx.lookup("java:comp/env/jdbc/RUBiS"); 34 } 35 catch (NamingException e) 36 { 37 e.printStackTrace(); 38 throw new ServletException (e); 39 } 40 catch (Exception e) 41 { 42 e.printStackTrace(); 43 throw new ServletException (e); 44 } 45 } 46 47 51 public void closeConnection(Connection connection) 52 { 53 try 54 { 55 connection.close(); 56 } 57 catch (Exception e) 58 { 59 } 60 } 61 62 68 public synchronized Connection getConnection() 69 { 70 if (ds != null) 71 try 72 { 73 return ds.getConnection(); 74 } 75 catch (SQLException e) 76 { 77 e.printStackTrace(); 78 return null; 79 } 80 else 81 { 82 System.out.println("ERROR: No datasource available"); 83 return null; 84 } 85 } 86 87 92 public synchronized void releaseConnection(Connection c) 93 { 94 try 95 { 96 c.close(); 97 } 98 catch (SQLException e) 99 { 100 e.printStackTrace(); 101 } 102 } 103 public void doGet(HttpServletRequest request, HttpServletResponse response) 104 throws IOException , ServletException 105 { 106 107 } 108 109 public void doPost(HttpServletRequest request, HttpServletResponse response) 110 throws IOException , ServletException 111 { 112 113 } 114 115 } 116 | Popular Tags |