Skip to content

Commit 863e466

Browse files
author
Arun Gupta
committed
Shows transaction commit for checked exception and rollback for runtime exception
1 parent f2afd5f commit 863e466

8 files changed

Lines changed: 156 additions & 48 deletions

File tree

jta/tx-exception/src/main/java/org/javaee7/jta/tx/exception/MyDataBean.java renamed to jta/tx-exception/src/main/java/org/javaee7/jta/tx/exception/Employee.java

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* and Distribution License("CDDL") (collectively, the "License"). You
99
* may not use this file except in compliance with the License. You can
1010
* obtain a copy of the License at
11-
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
1212
* or packager/legal/LICENSE.txt. See the License for the specific
1313
* language governing permissions and limitations under the License.
1414
*
@@ -40,20 +40,58 @@
4040
package org.javaee7.jta.tx.exception;
4141

4242
import java.io.Serializable;
43-
import javax.enterprise.context.SessionScoped;
43+
import javax.persistence.Column;
44+
import javax.persistence.Entity;
45+
import javax.persistence.Id;
46+
import javax.persistence.NamedQueries;
47+
import javax.persistence.NamedQuery;
48+
import javax.persistence.Table;
4449

4550
/**
4651
* @author Arun Gupta
4752
*/
48-
@SessionScoped
49-
public class MyDataBean implements Serializable {
50-
int value;
53+
@Entity
54+
@Table(name="EMPLOYEE_SCHEMA_JTA")
55+
@NamedQueries({
56+
@NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")
57+
})
58+
public class Employee implements Serializable {
59+
private static final long serialVersionUID = 1L;
60+
@Id
61+
private int id;
62+
63+
@Column(length=50)
64+
private String name;
65+
66+
public Employee() { }
67+
68+
public Employee(String name) {
69+
this.name = name;
70+
}
71+
72+
public Employee(int id, String name) {
73+
this.id = id;
74+
this.name = name;
75+
}
76+
77+
public int getId() {
78+
return id;
79+
}
5180

52-
public int getValue() {
53-
return value;
81+
public void setId(int id) {
82+
this.id = id;
5483
}
5584

56-
public void setValue(int value) {
57-
this.value = value;
85+
public String getName() {
86+
return name;
87+
}
88+
89+
public void setName(String name) {
90+
this.name = name;
91+
}
92+
93+
@Override
94+
public String toString() {
95+
return name;
5896
}
5997
}

jta/tx-exception/src/main/java/org/javaee7/jta/tx/exception/MyLogicBean.java renamed to jta/tx-exception/src/main/java/org/javaee7/jta/tx/exception/EmployeeBean.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,32 @@
3939
*/
4040
package org.javaee7.jta.tx.exception;
4141

42-
import javax.inject.Inject;
42+
import java.util.List;
43+
import javax.persistence.EntityManager;
44+
import javax.persistence.PersistenceContext;
4345
import javax.transaction.Transactional;
4446

4547
/**
4648
* @author Arun Gupta
4749
*/
48-
public class MyLogicBean {
49-
@Inject MyDataBean myBean;
50+
public class EmployeeBean {
51+
@PersistenceContext
52+
EntityManager em;
5053

5154
@Transactional
52-
public void throwChecked() throws Exception {
53-
myBean.setValue(20);
55+
public void addAndThrowChecked() throws Exception {
56+
em.persist(new Employee(8, "Priya"));
5457
throw new Exception();
5558
}
5659

5760
@Transactional
58-
public void throwRuntime() {
59-
myBean.setValue(30);
61+
public void addAndThrowRuntime() {
62+
em.persist(new Employee(9, "Priya"));
6063
throw new RuntimeException();
64+
}
65+
66+
public List<Employee> getEmployees() {
67+
System.out.println("getEmployees");
68+
return em.createNamedQuery("Employee.findAll", Employee.class).getResultList();
6169
}
6270
}
63-

jta/tx-exception/src/main/java/org/javaee7/jta/tx/exception/TestServlet.java

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
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
*/
540
package org.javaee7.jta.tx.exception;
641

@@ -14,23 +49,14 @@
1449
import javax.servlet.http.HttpServlet;
1550
import javax.servlet.http.HttpServletRequest;
1651
import 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"})
2957
public 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
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE EMPLOYEE_SCHEMA_JTA ("ID" INTEGER not null primary key, "NAME" VARCHAR(50) not null)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE EMPLOYEE_SCHEMA_JTA
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
INSERT INTO EMPLOYEE_SCHEMA_JTA("ID", "NAME") VALUES (1, 'Penny')
2+
INSERT INTO EMPLOYEE_SCHEMA_JTA("ID", "NAME") VALUES (2, 'Sheldon')
3+
INSERT INTO EMPLOYEE_SCHEMA_JTA("ID", "NAME") VALUES (3, 'Amy')
4+
INSERT INTO EMPLOYEE_SCHEMA_JTA("ID", "NAME") VALUES (4, 'Leonard')
5+
INSERT INTO EMPLOYEE_SCHEMA_JTA("ID", "NAME") VALUES (5, 'Bernadette')
6+
INSERT INTO EMPLOYEE_SCHEMA_JTA("ID", "NAME") VALUES (6, 'Raj')
7+
INSERT INTO EMPLOYEE_SCHEMA_JTA("ID", "NAME") VALUES (7, 'Howard')
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence
3+
version="2.1"
4+
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
7+
<persistence-unit name="MyPU" transaction-type="JTA">
8+
<properties>
9+
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
10+
<property name="javax.persistence.schema-generation.create-source" value="script"/>
11+
<property name="javax.persistence.schema-generation.drop-source" value="script"/>
12+
<property name="javax.persistence.schema-generation.create-script-source" value="META-INF/create.sql"/>
13+
<property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/drop.sql"/>
14+
<property name="javax.persistence.sql-load-script-source" value="META-INF/load.sql"/>
15+
<property name="eclipselink.logging.level" value="FINE"/>
16+
</properties>
17+
</persistence-unit>
18+
</persistence>

jta/tx-exception/src/main/webapp/index.jsp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
<html>
4848
<head>
4949
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
50-
<title>Checked and Runtime Exceptions</title>
50+
<title>JTA : Checked and Runtime Exceptions</title>
5151
</head>
5252
<body>
53-
<h1>Checked and Runtime Exceptions</h1>
53+
<h1>JTA : Checked and Runtime Exceptions</h1>
5454
<a href="${pageContext.request.contextPath}/TestServlet"/>Call bean</a> that throws exceptions<br/>
5555
</body>
5656
</html>

0 commit comments

Comments
 (0)