1 /* 2 * JBoss, Home of Professional Open Source 3 * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual 4 * contributors by the @authors tag. See the copyright.txt in the 5 * distribution for a full listing of individual contributors. 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.jboss.as.quickstarts.bmt; 18 19 import java.io.IOException; 20 import java.io.PrintWriter; 21 22 import javax.inject.Inject; 23 import javax.servlet.ServletException; 24 import javax.servlet.annotation.WebServlet; 25 import javax.servlet.http.HttpServlet; 26 import javax.servlet.http.HttpServletRequest; 27 import javax.servlet.http.HttpServletResponse; 28 29 /** 30 * <p> 31 * A servlet which shows how to manually manage transactions and database resources using 32 * both managed (session bean) and non-managed (simple CDI pojo) components. Reasons 33 * why the developer might want to control transaction demarcation are various, for example 34 * application requirements may need to access synchronizations or there may be XAResources 35 * that are not supported by the JEE container. 36 * </p> 37 * 38 * <p> 39 * The servlet is registered and mapped to /BMT using the {@linkplain WebServlet 40 * @HttpServlet}. The {@link ManagedComponent} and {@link UnManagedComponent} are injected by CDI. 41 * </p> 42 * 43 * @author Mike Musgrove 44 * 45 */ 46 47 @WebServlet("/BMT") 48 public class TransactionServlet extends HttpServlet { 49 /** Default value included to remove warning. **/ 50 private static final long serialVersionUID = 1L; 51 52 static String PAGE_HEADER = "<html><head><title>bmt</title></head><body>"; 53 54 static String PAGE_CONTENT = "<h1>Stepping Outside the Container (with JPA and JTA)</h1>" 55 + "<form>" 56 + "<input checked type=\"checkbox\" name=\"strategy\" value=\"managed\" /> Use bean managed Entity Managers <br />" 57 + "Key: <input type=\"text\" name=\"key\" /><br />" 58 + "Value: <input type=\"text\" name=\"value\" /><br />" 59 + "<input type=\"submit\" value=\"Submit\" /><br />" 60 + "</form>"; 61 62 static String PAGE_FOOTER = "</body></html>"; 63 64 /* 65 * Inject a stateless bean. Although stateless beans are thread safe it is probably not a solution that scales particularly 66 * well. 67 */ 68 @Inject 69 ManagedComponent managedBean; 70 71 /* 72 * Create a CDI POJO that will manage both transactions and the JPA EntityManager itself 73 */ 74 @Inject 75 UnManagedComponent unManagedBean; 76 77 /** 78 * <p> 79 * Servlet entry point. 80 * </p> 81 * <p> 82 * The behaviour of the servlet is controlled by servlet query parameter or form parameters. If parameters named "key" and 83 * "value" are present then that pair is added (or the key is updated if it already exists) to the database. If the form 84 * parameter "strategy" is not set to the value "managed" then both the transaction and the EntityManager are controlled 85 * manually. Otherwise the Entity Manager is controlled by the container and the transaction is controlled by the developer. 86 * </p> 87 * 88 * @param req the HTTP request 89 * @param resp the HTTP response 90 * @throws ServletException 91 * @throws IOException 92 */ 93 @Override 94 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 95 PrintWriter writer = resp.getWriter(); 96 String responseText; 97 String key = req.getParameter("key"); 98 String value = req.getParameter("value"); 99 String txStrategy = req.getParameter("strategy"); 100 101 if ("managed".equalsIgnoreCase(txStrategy)) 102 responseText = managedBean.updateKeyValueDatabase(key, value); 103 else 104 responseText = unManagedBean.updateKeyValueDatabase(key, value); 105 106 writer.println(PAGE_HEADER); 107 writer.println(PAGE_CONTENT); 108 writer.println("<p>" + responseText + "</p>"); 109 writer.println(PAGE_FOOTER); 110 111 writer.close(); 112 } 113 114 }