blob: 02fa081d8449fef48ea2fcf205a6b8ece4137b60 [file] [log] [blame]
Vinay Vishal57171472018-09-18 20:22:00 +05301/*
2 * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17package com.sun.s1asdev.jdbc.onlygetconnectionservlet.servlet;
18
Gaurav Guptaeea72b52020-05-05 18:13:58 +053019import jakarta.servlet.*;
20import jakarta.servlet.http.*;
Gaurav Guptac2a128d2020-05-01 20:56:15 +053021import jakarta.transaction.*;
Vinay Vishal57171472018-09-18 20:22:00 +053022import javax.sql.*;
23import java.sql.*;
24import java.io.*;
25import javax.naming.InitialContext;
26
27/**
28 * Collection of getConnection tests using a servlet
David Matějčekf4dc06a2021-05-17 12:10:57 +020029 *
Vinay Vishal57171472018-09-18 20:22:00 +053030 * @author aditya.gore@sun.com
David Matějčekf4dc06a2021-05-17 12:10:57 +020031 */
Vinay Vishal57171472018-09-18 20:22:00 +053032
33public class OnlyGetConnectionServlet extends HttpServlet {
34
35 private DataSource ds;
36 private PrintWriter out;
37 private UserTransaction utx;
38
David Matějčekf4dc06a2021-05-17 12:10:57 +020039 public void doGet( HttpServletRequest req, HttpServletResponse resp )
Vinay Vishal57171472018-09-18 20:22:00 +053040 throws IOException, ServletException
41 {
David Matějčekf4dc06a2021-05-17 12:10:57 +020042System.out.println(" @@@@ in doGet");
Vinay Vishal57171472018-09-18 20:22:00 +053043 out = resp.getWriter();
44 writeHeader();
45
46 try {
47 InitialContext ctx = new InitialContext();
48 ds = (DataSource) ctx.lookup("java:comp/env/jdbc/onlygetconnectionservlet");
49 utx = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
50 } catch(Exception e) {
51 e.printStackTrace( out );
52 return;
53 }
54
55// out.println("-----Test1----");
56// test1();
57// out.println("--------------");
58// out.println("-----Test1----");
59// test2();
60// out.println("--------------");
61 test2();
62
63 writeFooter();
64 }
65
66 private void test1() {
67 Connection con = null;
68 Statement stmt = null;
69 ResultSet rs = null;
70
71 try {
72 con = ds.getConnection();
73 stmt = con.createStatement();
74 rs = stmt.executeQuery("SELECT * FROM ONLYGETCONNECTION");
David Matějčekf4dc06a2021-05-17 12:10:57 +020075 out.println("test1 :: PASSED");
Vinay Vishal57171472018-09-18 20:22:00 +053076 } catch(Exception e) {
David Matějčekf4dc06a2021-05-17 12:10:57 +020077 e.printStackTrace( out );
Vinay Vishal57171472018-09-18 20:22:00 +053078 return;
79 } finally {
80 if ( rs != null ) { try { rs.close(); }catch( Exception e) {} }
81 if ( stmt != null ) { try { stmt.close(); }catch( Exception e) {} }
82 if ( con != null ) { try { con.close(); }catch( Exception e) {} }
83 }
84 }
85
86 private void test2() {
87 Connection con = null;
88 Statement stmt = null;
89 ResultSet rs = null;
90
91 try {
92 utx.begin();
93 con = ds.getConnection();
94 try {
David Matějčekf4dc06a2021-05-17 12:10:57 +020095 Thread.sleep( 5000 );
Vinay Vishal57171472018-09-18 20:22:00 +053096 } catch(Exception e) {
97 }
98 stmt = con.createStatement();
99 rs = stmt.executeQuery("SELECT * FROM ONLYGETCONNECTION");
100 utx.commit();
David Matějčekf4dc06a2021-05-17 12:10:57 +0200101 out.println("test2 :: PASSED");
Vinay Vishal57171472018-09-18 20:22:00 +0530102 } catch(Exception e) {
David Matějčekf4dc06a2021-05-17 12:10:57 +0200103 e.printStackTrace( out );
Vinay Vishal57171472018-09-18 20:22:00 +0530104 return;
105 } finally {
106 if ( rs != null ) { try { rs.close(); }catch( Exception e) {} }
107 if ( stmt != null ) { try { stmt.close(); }catch( Exception e) {} }
108 if ( con != null ) { try { con.close(); }catch( Exception e) {} }
109 }
110 }
111
112 private void writeHeader() {
113 out.println( "<html>" );
114 out.println( "<head><title>onlygetconnectionservlet results</title></head>");
115 out.println( "<body>");
116 }
117
118 private void writeFooter() {
119 out.println( "</body>");
120 out.println( "</html>");
121 }
122}