Skip to content

Commit f70f5a9

Browse files
committed
Adding a new sample to demonstrate unsynchronized PC
1 parent ef3b243 commit f70f5a9

10 files changed

Lines changed: 393 additions & 1 deletion

File tree

jpa/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@
1919
<module>entitygraph</module>
2020
<module>listeners</module>
2121
<module>multiple-pu</module>
22-
<module>schema-gen-metadata</module>
2322
<module>storedprocedure</module>
2423
<module>jndi-context</module>
2524
<module>locking-optimistic</module>
2625
<module>locking-pessimistic</module>
2726
<module>pu-typesafe</module>
27+
<module>schema-gen-metadata</module>
2828
<module>schema-gen-scripts</module>
2929
<module>schema-gen-scripts-external</module>
3030
<module>schema-gen-scripts-generate</module>
3131
<module>native-sql</module>
3232
<module>native-sql-resultset-mapping</module>
33+
<module>unsynchronized-pc</module>
3334
</modules>
3435

3536
</project>

jpa/unsynchronized-pc/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.jpa</groupId>
6+
<artifactId>jpa-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<artifactId>unsynchronized-pc</artifactId>
12+
<packaging>war</packaging>
13+
</project>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
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+
* http://glassfish.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.
39+
*/
40+
package org.javaee7.jpa.unsynchronized.pc;
41+
42+
import java.io.Serializable;
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;
49+
50+
/**
51+
* @author Arun Gupta
52+
*/
53+
@Entity
54+
@Table(name="EMPLOYEE_SCHEMA_UNSYNCHRONIZED_PC")
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(int id, String name) {
69+
this.id = id;
70+
this.name = name;
71+
}
72+
73+
public Employee(String name) {
74+
this.name = name;
75+
}
76+
77+
public int getId() {
78+
return id;
79+
}
80+
81+
public void setId(int id) {
82+
this.id = id;
83+
}
84+
85+
public String getName() {
86+
return name;
87+
}
88+
89+
public void setName(String name) {
90+
this.name = name;
91+
}
92+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
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+
* http://glassfish.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.
39+
*/
40+
package org.javaee7.jpa.unsynchronized.pc;
41+
42+
import java.util.List;
43+
import javax.ejb.Stateless;
44+
import javax.persistence.EntityManager;
45+
import javax.persistence.PersistenceContext;
46+
import javax.persistence.SynchronizationType;
47+
48+
/**
49+
* @author Arun Gupta
50+
*/
51+
@Stateless
52+
public class EmployeeBean {
53+
54+
@PersistenceContext(synchronization = SynchronizationType.UNSYNCHRONIZED)
55+
EntityManager em;
56+
57+
public void persistWithoutJoin(Employee e) {
58+
em.persist(e);
59+
}
60+
61+
public void persistWithJoin(Employee e) {
62+
em.joinTransaction();
63+
em.persist(e);
64+
}
65+
66+
67+
public List<Employee> get() {
68+
return em.createNamedQuery("Employee.findAll", Employee.class).getResultList();
69+
}
70+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
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+
* http://glassfish.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.
39+
*/
40+
package org.javaee7.jpa.unsynchronized.pc;
41+
42+
import java.io.IOException;
43+
import java.io.PrintWriter;
44+
import javax.inject.Inject;
45+
import javax.servlet.ServletException;
46+
import javax.servlet.annotation.WebServlet;
47+
import javax.servlet.http.HttpServlet;
48+
import javax.servlet.http.HttpServletRequest;
49+
import javax.servlet.http.HttpServletResponse;
50+
51+
/**
52+
* @author Arun Gupta
53+
*/
54+
@WebServlet(urlPatterns = {"/TestServlet"})
55+
public class TestServlet extends HttpServlet {
56+
57+
@Inject
58+
EmployeeBean bean;
59+
60+
/**
61+
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
62+
* methods.
63+
*
64+
* @param request servlet request
65+
* @param response servlet response
66+
* @throws ServletException if a servlet-specific error occurs
67+
* @throws IOException if an I/O error occurs
68+
*/
69+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
70+
throws ServletException, IOException {
71+
response.setContentType("text/html;charset=UTF-8");
72+
PrintWriter out = response.getWriter();
73+
out.println("<html>");
74+
out.println("<head>");
75+
out.println("<title>List of Employees</title>");
76+
out.println("</head>");
77+
out.println("<body>");
78+
listEmployees(out, "Initial list", "Total == 7?");
79+
Employee e = new Employee(8, "Priya");
80+
bean.persistWithoutJoin(e);
81+
listEmployees(out, "Unsynchronized PC has not joined a transaction", "Total == 7?");
82+
bean.persistWithJoin(e);
83+
listEmployees(out, "Unsynchronized PC has joined a transaction", "Total == 8?");
84+
out.println("</body>");
85+
out.println("</html>");
86+
}
87+
88+
private void listEmployees(PrintWriter out, String title, String result) {
89+
out.println("<h3>" + title + " (" + bean.get().size() + ") " + result + "</h3>");
90+
for (Employee e : bean.get()) {
91+
out.println(e.getName() + "<br>");
92+
}
93+
}
94+
95+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
96+
/**
97+
* Handles the HTTP <code>GET</code> method.
98+
*
99+
* @param request servlet request
100+
* @param response servlet response
101+
* @throws ServletException if a servlet-specific error occurs
102+
* @throws IOException if an I/O error occurs
103+
*/
104+
@Override
105+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
106+
throws ServletException, IOException {
107+
processRequest(request, response);
108+
}
109+
110+
/**
111+
* Handles the HTTP <code>POST</code> method.
112+
*
113+
* @param request servlet request
114+
* @param response servlet response
115+
* @throws ServletException if a servlet-specific error occurs
116+
* @throws IOException if an I/O error occurs
117+
*/
118+
@Override
119+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
120+
throws ServletException, IOException {
121+
processRequest(request, response);
122+
}
123+
124+
/**
125+
* Returns a short description of the servlet.
126+
*
127+
* @return a String containing servlet description
128+
*/
129+
@Override
130+
public String getServletInfo() {
131+
return "Short description";
132+
}// </editor-fold>
133+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE EMPLOYEE_SCHEMA_UNSYNCHRONIZED_PC ("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_UNSYNCHRONIZED_PC
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
INSERT INTO EMPLOYEE_SCHEMA_UNSYNCHRONIZED_PC("ID", "NAME") VALUES (1, 'Penny')
2+
INSERT INTO EMPLOYEE_SCHEMA_UNSYNCHRONIZED_PC("ID", "NAME") VALUES (2, 'Sheldon')
3+
INSERT INTO EMPLOYEE_SCHEMA_UNSYNCHRONIZED_PC("ID", "NAME") VALUES (3, 'Amy')
4+
INSERT INTO EMPLOYEE_SCHEMA_UNSYNCHRONIZED_PC("ID", "NAME") VALUES (4, 'Leonard')
5+
INSERT INTO EMPLOYEE_SCHEMA_UNSYNCHRONIZED_PC("ID", "NAME") VALUES (5, 'Bernadette')
6+
INSERT INTO EMPLOYEE_SCHEMA_UNSYNCHRONIZED_PC("ID", "NAME") VALUES (6, 'Raj')
7+
INSERT INTO EMPLOYEE_SCHEMA_UNSYNCHRONIZED_PC("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>

0 commit comments

Comments
 (0)