11/*
2- * To change this template, choose Tools | Templates
3- * and open the template in the editor.
2+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+ *
4+ * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+ *
6+ * The contents of this file are subject to the terms of either the GNU
7+ * General Public License Version 2 only ("GPL") or the Common Development
8+ * and Distribution License("CDDL") (collectively, the "License"). You
9+ * may not use this file except in compliance with the License. You can
10+ * obtain a copy of the License at
11+ * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+ * or packager/legal/LICENSE.txt. See the License for the specific
13+ * language governing permissions and limitations under the License.
14+ *
15+ * When distributing the software, include this License Header Notice in each
16+ * file and include the License file at packager/legal/LICENSE.txt.
17+ *
18+ * GPL Classpath Exception:
19+ * Oracle designates this particular file as subject to the "Classpath"
20+ * exception as provided by Oracle in the GPL Version 2 section of the License
21+ * file that accompanied this code.
22+ *
23+ * Modifications:
24+ * If applicable, add the following below the License Header, with the fields
25+ * enclosed by brackets [] replaced by your own identifying information:
26+ * "Portions Copyright [year] [name of copyright owner]"
27+ *
28+ * Contributor(s):
29+ * If you wish your version of this file to be governed by only the CDDL or
30+ * only the GPL Version 2, indicate your decision by adding "[Contributor]
31+ * elects to include this software in this distribution under the [CDDL or GPL
32+ * Version 2] license." If you don't indicate a single choice of license, a
33+ * recipient has the option to distribute your version of this file under
34+ * either the CDDL, the GPL Version 2 or to extend the choice of license to
35+ * its licensees as provided above. However, if you add GPL Version 2 code
36+ * and therefore, elected the GPL Version 2 license, then the option applies
37+ * only if the new code is made subject to such option by the copyright
38+ * holder.
439 */
540package org .javaee7 .jta .tx .exception ;
641
1449import javax .servlet .http .HttpServlet ;
1550import javax .servlet .http .HttpServletRequest ;
1651import javax .servlet .http .HttpServletResponse ;
17- import javax .transaction .HeuristicMixedException ;
18- import javax .transaction .HeuristicRollbackException ;
19- import javax .transaction .NotSupportedException ;
20- import javax .transaction .RollbackException ;
21- import javax .transaction .SystemException ;
22- import javax .transaction .Transaction ;
2352
2453/**
25- *
26- * @author arungup
54+ * @author Arun Gupta
2755 */
28- @ WebServlet (name = "TestServlet" , urlPatterns = {"/TestServlet" })
56+ @ WebServlet (urlPatterns = {"/TestServlet" })
2957public class TestServlet extends HttpServlet {
3058
31- @ Inject MyDataBean dataBean ;
32-
33- @ Inject MyLogicBean logicBean ;
59+ @ Inject EmployeeBean bean ;
3460
3561 /**
3662 * Processes requests for both HTTP
@@ -49,27 +75,37 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
4975 out .println ("<!DOCTYPE html>" );
5076 out .println ("<html>" );
5177 out .println ("<head>" );
52- out .println ("<title>Servlet TestServlet </title>" );
78+ out .println ("<title>Checked and Runtime Exception in JTA Transactions </title>" );
5379 out .println ("</head>" );
5480 out .println ("<body>" );
55- out .println ("<h1>Servlet TestServlet at " + request .getContextPath () + "</h1>" );
56- dataBean .setValue (10 );
57- out .println ("Bean value before throwChecked: " + dataBean .getValue () + "<br>" );
81+ out .println ("<h1>Checked and Runtime Exception in JTA Transactions</h1>" );
82+ out .println ("<h2>List employees (before checked exception)</h2>" );
83+ out .println ("<ol>" );
84+ for (Employee e : bean .getEmployees ()) {
85+ out .println ("<li>" + e );
86+ }
87+ out .println ("</ol>" );
88+ out .println ("Number of employees is 7 ?" );
89+
5890 try {
59- logicBean .throwChecked ();
60- } catch (Exception ex ) {
61- Logger .getLogger (TestServlet .class .getName ()).log (Level .SEVERE , null , ex );
91+ bean .addAndThrowChecked ();
92+ } catch (Exception ex ) { }
93+
94+ out .println ("<h2>List employees (after checked and before runtime exception)</h2>" );
95+ out .println ("<ol>" );
96+ for (Employee e : bean .getEmployees ()) {
97+ out .println ("<li>" + e );
6298 }
63- out .println ("Bean value after throwChecked: " + dataBean .getValue () + "<p>" );
99+ out .println ("</ol>" );
100+ out .println ("Number of employees is 8 (new employee is Priya) ?" );
64101
65- out .println ("Bean value before throwRuntime: " + dataBean .getValue () + "<br>" );
66- try {
67- logicBean .throwRuntime ();
68- } catch (Exception ex ) {
69- Logger .getLogger (TestServlet .class .getName ()).log (Level .SEVERE , null , ex );
102+ out .println ("<h2>List employees (after runtime exception)</h2>" );
103+ out .println ("<ol>" );
104+ for (Employee e : bean .getEmployees ()) {
105+ out .println ("<li>" + e );
70106 }
71- out .println ("Bean value after throwRuntime: " + dataBean . getValue () );
72- out .println ("<br><br>Not sure what the right checks should be ? ?" );
107+ out .println ("</ol>" );
108+ out .println ("Number of employees is 8 (no new employee is added because of runtime exception) ?" );
73109 out .println ("</body>" );
74110 out .println ("</html>" );
75111 }
0 commit comments