Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.sun.s1asdev.jdbc.onlygetconnectionservlet.servlet; |
| 18 | |
Gaurav Gupta | eea72b5 | 2020-05-05 18:13:58 +0530 | [diff] [blame] | 19 | import jakarta.servlet.*; |
| 20 | import jakarta.servlet.http.*; |
Gaurav Gupta | c2a128d | 2020-05-01 20:56:15 +0530 | [diff] [blame] | 21 | import jakarta.transaction.*; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 22 | import javax.sql.*; |
| 23 | import java.sql.*; |
| 24 | import java.io.*; |
| 25 | import javax.naming.InitialContext; |
| 26 | |
| 27 | /** |
| 28 | * Collection of getConnection tests using a servlet |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 29 | * |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 30 | * @author aditya.gore@sun.com |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 31 | */ |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 32 | |
| 33 | public class OnlyGetConnectionServlet extends HttpServlet { |
| 34 | |
| 35 | private DataSource ds; |
| 36 | private PrintWriter out; |
| 37 | private UserTransaction utx; |
| 38 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 39 | public void doGet( HttpServletRequest req, HttpServletResponse resp ) |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 40 | throws IOException, ServletException |
| 41 | { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 42 | System.out.println(" @@@@ in doGet"); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 43 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 75 | out.println("test1 :: PASSED"); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 76 | } catch(Exception e) { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 77 | e.printStackTrace( out ); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 78 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 95 | Thread.sleep( 5000 ); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 96 | } catch(Exception e) { |
| 97 | } |
| 98 | stmt = con.createStatement(); |
| 99 | rs = stmt.executeQuery("SELECT * FROM ONLYGETCONNECTION"); |
| 100 | utx.commit(); |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 101 | out.println("test2 :: PASSED"); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 102 | } catch(Exception e) { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 103 | e.printStackTrace( out ); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 104 | 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 | } |