1 package connector; 2 3 import java.io.*; 4 import java.util.Map ; 5 6 import javax.resource.ResourceException ; 7 import javax.servlet.*; 8 import javax.servlet.http.*; 9 import javax.transaction.SystemException ; 10 import javax.transaction.UserTransaction ; 11 12 import javax.naming.InitialContext ; 13 import javax.naming.Context ; 14 15 import org.apache.commons.transaction.memory.jca.*; 16 17 20 public class TestServlet extends HttpServlet { 21 private MapConnectionFactory _factory; 23 24 28 public void init() throws ServletException { 29 try { 30 Context ic = new InitialContext (); 31 32 _factory = (MapConnectionFactory) ic.lookup("java:comp/env/Map"); 33 } catch (Exception e) { 34 throw new ServletException(e); 35 } 36 } 37 38 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 tx = null; 52 try { 53 Context ic = new InitialContext (); 54 tx = (UserTransaction ) 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 map1 = conn1.getMap(); 72 Map 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 e) { 87 if (tx != null) 88 try { 89 tx.rollback(); 90 } catch (IllegalStateException e1) { 91 e1.printStackTrace(); 93 } catch (SecurityException e1) { 94 e1.printStackTrace(); 96 } catch (SystemException e1) { 97 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 e1) { 108 e1.printStackTrace(); 110 } 111 if (conn2 != null) 112 try { 113 conn2.close(); 114 } catch (ResourceException e1) { 115 e1.printStackTrace(); 117 } 118 } 119 } 120 } | Popular Tags |