|
| 1 | +import java.io.IOException; |
| 2 | +import java.io.PrintWriter; |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.ResultSet; |
| 5 | +import java.sql.SQLException; |
| 6 | +import java.sql.Statement; |
| 7 | + |
| 8 | +import javax.naming.Context; |
| 9 | +import javax.naming.InitialContext; |
| 10 | +import javax.naming.NamingException; |
| 11 | +import javax.servlet.ServletException; |
| 12 | +import javax.servlet.annotation.WebServlet; |
| 13 | +import javax.servlet.http.HttpServlet; |
| 14 | +import javax.servlet.http.HttpServletRequest; |
| 15 | +import javax.servlet.http.HttpServletResponse; |
| 16 | + |
| 17 | +import oracle.ucp.admin.UniversalConnectionPoolManagerImpl; |
| 18 | +import oracle.ucp.jdbc.PoolDataSource; |
| 19 | + |
| 20 | +/** |
| 21 | + * Servlet implementation class UCPServlet |
| 22 | + */ |
| 23 | +@WebServlet("/UCPServlet") |
| 24 | +public class UCPServlet extends HttpServlet { |
| 25 | + private static final long serialVersionUID = 1L; |
| 26 | + |
| 27 | + /** |
| 28 | + * @see HttpServlet#HttpServlet() |
| 29 | + */ |
| 30 | + public UCPServlet() { |
| 31 | + super(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) |
| 36 | + */ |
| 37 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) |
| 38 | + throws ServletException, IOException { |
| 39 | + PrintWriter out = response.getWriter(); |
| 40 | + |
| 41 | + out.println("Servlet to test ATP using UCP"); |
| 42 | + Connection conn = null; |
| 43 | + try { |
| 44 | + // Get a context for the JNDI look up |
| 45 | + PoolDataSource pds = getPoolInstance(); |
| 46 | + conn = pds.getConnection(); |
| 47 | + |
| 48 | + // Prepare a statement to execute the SQL Queries. |
| 49 | + Statement statement = conn.createStatement(); |
| 50 | + // Create table EMP |
| 51 | + statement.executeUpdate("create table EMP(EMPLOYEEID NUMBER," |
| 52 | + + "EMPLOYEENAME VARCHAR2 (20))"); |
| 53 | + out.println("New table EMP is created"); |
| 54 | + // Insert some records into the table EMP |
| 55 | + statement.executeUpdate("insert into EMP values(1, 'Jennifer Jones')"); |
| 56 | + statement.executeUpdate("insert into EMP values(2, 'Alex Debouir')"); |
| 57 | + out.println("Two records are inserted."); |
| 58 | + |
| 59 | + // Update a record on EMP table. |
| 60 | + statement.executeUpdate("update EMP set EMPLOYEENAME='Alex Deborie'" |
| 61 | + + " where EMPLOYEEID=2"); |
| 62 | + out.println("One record is updated."); |
| 63 | + |
| 64 | + // Verify the table EMP |
| 65 | + ResultSet resultSet = statement.executeQuery("select * from EMP"); |
| 66 | + out.println("\nNew table EMP contains:"); |
| 67 | + out.println("EMPLOYEEID" + " " + "EMPLOYEENAME"); |
| 68 | + out.println("--------------------------"); |
| 69 | + while (resultSet.next()) { |
| 70 | + out.println(resultSet.getInt(1) + " " + resultSet.getString(2)); |
| 71 | + } |
| 72 | + out.println("\nSuccessfully tested a connection to ATP using UCP"); |
| 73 | + } |
| 74 | + catch (Exception e) { |
| 75 | + response.setStatus(500); |
| 76 | + response.setHeader("Exception", e.toString()); |
| 77 | + out.print("\n Web Request failed"); |
| 78 | + out.print("\n "+e.toString()); |
| 79 | + e.printStackTrace(); |
| 80 | + } |
| 81 | + finally { |
| 82 | + // Clean-up after everything |
| 83 | + try (Statement statement = conn.createStatement()) { |
| 84 | + statement.execute("drop table EMP"); |
| 85 | + conn.close(); |
| 86 | + } |
| 87 | + catch (SQLException e) { |
| 88 | + System.out.println("UCPServlet - " |
| 89 | + + "doSQLWork()- SQLException occurred : " + e.getMessage()); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /* Get the appropriate datasource */ |
| 95 | + private PoolDataSource getPoolInstance() throws NamingException { |
| 96 | + Context ctx; |
| 97 | + ctx = new InitialContext(); |
| 98 | + Context envContext = (Context) ctx.lookup("java:/comp/env"); |
| 99 | + |
| 100 | + // Look up a data source |
| 101 | + javax.sql.DataSource ds |
| 102 | + = (javax.sql.DataSource) envContext.lookup ("tomcat/UCP_atp"); |
| 103 | + PoolDataSource pds=(PoolDataSource)ds; |
| 104 | + |
| 105 | + return pds; |
| 106 | + } |
| 107 | + |
| 108 | + public void destroy() { |
| 109 | + try { |
| 110 | + |
| 111 | + UniversalConnectionPoolManagerImpl.getUniversalConnectionPoolManager() |
| 112 | + .destroyConnectionPool(getPoolInstance().getConnectionPoolName()); |
| 113 | + System.out.println("Pool Destroyed"); |
| 114 | + } catch (Exception e) { |
| 115 | + System.out.println("destroy pool got Exception:"); |
| 116 | + e.printStackTrace(); |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) |
| 123 | + */ |
| 124 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments