pveselov

transaction manager test JSP

May 13th, 2014
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. <%@ page import="javax.transaction.TransactionManager" %>
  2. <%@ page import="javax.transaction.TransactionSynchronizationRegistry" %>
  3. <%@ page import="javax.naming.InitialContext" %>
  4. <%@ page import="java.sql.Connection" %>
  5. <%@ page import="javax.sql.DataSource" %>
  6. <%@ page import="java.sql.Statement" %>
  7. <%@ page import="java.sql.ResultSet" %>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <html>
  10. <head>
  11. <title></title>
  12. </head>
  13. <body>
  14.  
  15.  
  16. <%!
  17. private static TransactionManager txManager;
  18.  
  19. static {
  20.  
  21. try {
  22.  
  23. InitialContext ic = new InitialContext();
  24.  
  25. txManager = (TransactionManager)
  26. ic.lookup("java:appserver/TransactionManager");
  27.  
  28. } catch (Throwable th) {
  29. throw new RuntimeException(th);
  30. }
  31.  
  32. }
  33.  
  34. %>
  35.  
  36. <%
  37.  
  38. TransactionManager tMgr = txManager;
  39. tMgr.begin();
  40.  
  41. InitialContext ic = new InitialContext();
  42. DataSource ds = (DataSource) ic.lookup("mi_pe");
  43. Connection conn = ds.getConnection();
  44.  
  45. Statement s = conn.createStatement();
  46. ResultSet rs = s.executeQuery("select nmr from inc_test");
  47. rs.next();
  48. %>
  49.  
  50. Before : <%=rs.getLong("nmr")%><br>
  51.  
  52. <%
  53. s.close();
  54.  
  55. s = conn.createStatement();
  56. s.execute("update inc_test set nmr = nmr + 1");
  57. s.close();
  58.  
  59. tMgr.rollback();
  60. conn.close();
  61.  
  62. tMgr.begin();
  63. // re-create another connection in a new transaction
  64.  
  65. ic = new InitialContext();
  66. ds = (DataSource) ic.lookup("mi_pe");
  67. conn = ds.getConnection();
  68. s = conn.createStatement();
  69. rs = s.executeQuery("select nmr from inc_test");
  70. rs.next();
  71.  
  72. %>
  73.  
  74. After : <%=rs.getLong("nmr")%><br>
  75.  
  76. <%
  77.  
  78. s.close();
  79. tMgr.rollback();
  80. conn.close();
  81.  
  82. %>
  83.  
  84.  
  85. </body>
  86. </html>
Advertisement
Add Comment
Please, Sign In to add comment