KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > connector > TestServlet


1 package connector;
2
3 import java.io.*;
4 import java.util.Map JavaDoc;
5
6 import javax.resource.ResourceException JavaDoc;
7 import javax.servlet.*;
8 import javax.servlet.http.*;
9 import javax.transaction.SystemException JavaDoc;
10 import javax.transaction.UserTransaction JavaDoc;
11
12 import javax.naming.InitialContext JavaDoc;
13 import javax.naming.Context JavaDoc;
14
15 import org.apache.commons.transaction.memory.jca.*;
16
17 /**
18  * Implementation of the test servlet.
19  */

20 public class TestServlet extends HttpServlet {
21     // Reference to the factory
22
private MapConnectionFactory _factory;
23
24     /**
25      * <code>init()</code> stores the factory for efficiency since JNDI
26      * is relatively slow.
27      */

28     public void init() throws ServletException {
29         try {
30             Context JavaDoc ic = new InitialContext JavaDoc();
31
32             _factory = (MapConnectionFactory) ic.lookup("java:comp/env/Map");
33         } catch (Exception JavaDoc e) {
34             throw new ServletException(e);
35         }
36     }
37
38     /**
39      * Use the connection. All JCA connections must use the following
40      * pattern to ensure the connection is closed even when exceptions
41      * occur.
42      */

43     public void service(HttpServletRequest request, HttpServletResponse response)
44         throws IOException, ServletException {
45         response.setContentType("text/html");
46         PrintWriter out = response.getWriter();
47
48         MapConnection conn1 = null;
49         MapConnection conn2 = null;
50
51         UserTransaction JavaDoc tx = null;
52         try {
53             Context JavaDoc ic = new InitialContext JavaDoc();
54             tx = (UserTransaction JavaDoc) ic.lookup("java:comp/UserTransaction");
55
56             tx.begin();
57
58             System.out.println("Tx: " + tx);
59             out.println("Tx: " + tx + "<br>");
60
61             System.out.println("Factory: " + _factory);
62             out.println("Factory: " + _factory + "<br>");
63
64             conn1 = (MapConnection) _factory.getConnection(new MapConnectionSpec("map1"));
65             conn2 = (MapConnection) _factory.getConnection(new MapConnectionSpec("map2"));
66             out.println("Connection1: " + conn1 + "<br>");
67             System.out.println("Connection1: " + conn1);
68             out.println("Connection2: " + conn2 + "<br>");
69             System.out.println("Connection2: " + conn2);
70
71             Map JavaDoc map1 = conn1.getMap();
72             Map JavaDoc map2 = conn2.getMap();
73             out.println("Map1: " + map1 + "<br>");
74             System.out.println("Map1: " + map1);
75             out.println("Map2: " + map2 + "<br>");
76             System.out.println("Map2: " + map2);
77
78             map1.put("Olli", "Molli");
79             map1.remove("Berti");
80
81             map2.put("Walter", "Alter");
82             map2.put("Gundel", "Flunder");
83             map2.remove("Hertha");
84
85             tx.commit();
86         } catch (Exception JavaDoc e) {
87             if (tx != null)
88                 try {
89                     tx.rollback();
90                 } catch (IllegalStateException JavaDoc e1) {
91                     // TODO Auto-generated catch block
92
e1.printStackTrace();
93                 } catch (SecurityException JavaDoc e1) {
94                     // TODO Auto-generated catch block
95
e1.printStackTrace();
96                 } catch (SystemException JavaDoc e1) {
97                     // TODO Auto-generated catch block
98
e1.printStackTrace();
99                 }
100             System.out.println(e);
101             e.printStackTrace();
102             throw new ServletException(e);
103         } finally {
104             if (conn1 != null)
105                 try {
106                     conn1.close();
107                 } catch (ResourceException JavaDoc e1) {
108                     // TODO Auto-generated catch block
109
e1.printStackTrace();
110                 }
111             if (conn2 != null)
112                 try {
113                     conn2.close();
114                 } catch (ResourceException JavaDoc e1) {
115                     // TODO Auto-generated catch block
116
e1.printStackTrace();
117                 }
118         }
119     }
120 }
Popular Tags