KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > servlets > RubisHttpServlet2


1 package edu.rice.rubis.servlets;
2
3 import java.io.IOException JavaDoc;
4 import java.sql.Connection JavaDoc;
5 import java.sql.SQLException JavaDoc;
6
7 import javax.naming.Context JavaDoc;
8 import javax.naming.InitialContext JavaDoc;
9 import javax.naming.NamingException JavaDoc;
10 import javax.servlet.ServletException JavaDoc;
11 import javax.servlet.http.HttpServlet JavaDoc;
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
14 import javax.sql.DataSource JavaDoc;
15
16 /** This class is a replacement for RubisHttpServlet that does not provide connection
17  * pooling but relies on a datasource (such as the one provided by Tomcat) that
18  * provides pooling.
19  */

20 public abstract class RubisHttpServlet2 extends HttpServlet JavaDoc
21 {
22   private DataSource JavaDoc ds = null;
23
24   /** Initialize the datasource */
25   public void init() throws ServletException JavaDoc
26   {
27     try
28     {
29       Context JavaDoc ctx = new InitialContext JavaDoc();
30       if (ctx == null)
31         throw new Exception JavaDoc("Boom - No Context");
32
33       ds = (DataSource JavaDoc) ctx.lookup("java:comp/env/jdbc/RUBiS");
34     }
35     catch (NamingException JavaDoc e)
36     {
37       e.printStackTrace();
38       throw new ServletException JavaDoc(e);
39     }
40     catch (Exception JavaDoc e)
41     {
42       e.printStackTrace();
43       throw new ServletException JavaDoc(e);
44     }
45   }
46
47   /**
48   * Closes a <code>Connection</code>.
49   * @param connection to close
50   */

51   public void closeConnection(Connection JavaDoc connection)
52   {
53     try
54     {
55       connection.close();
56     }
57     catch (Exception JavaDoc e)
58     {
59     }
60   }
61
62   /**
63    * Gets a connection from the pool (round-robin)
64    *
65    * @return a <code>Connection</code> or
66    * null if no connection is available
67    */

68   public synchronized Connection JavaDoc getConnection()
69   {
70     if (ds != null)
71       try
72       {
73         return ds.getConnection();
74       }
75       catch (SQLException JavaDoc 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   /**
88   * Releases a connection to the pool.
89   *
90   * @param c the connection to release
91   */

92   public synchronized void releaseConnection(Connection JavaDoc c)
93   {
94     try
95     {
96       c.close();
97     }
98     catch (SQLException JavaDoc e)
99     {
100       e.printStackTrace();
101     }
102   }
103   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
104     throws IOException JavaDoc, ServletException JavaDoc
105   {
106
107   }
108
109   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
110     throws IOException JavaDoc, ServletException JavaDoc
111   {
112
113   }
114
115 }
116
Popular Tags