Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@ page import="javax.transaction.TransactionManager" %>
- <%@ page import="javax.transaction.TransactionSynchronizationRegistry" %>
- <%@ page import="javax.naming.InitialContext" %>
- <%@ page import="java.sql.Connection" %>
- <%@ page import="javax.sql.DataSource" %>
- <%@ page import="java.sql.Statement" %>
- <%@ page import="java.sql.ResultSet" %>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title></title>
- </head>
- <body>
- <%!
- private static TransactionManager txManager;
- static {
- try {
- InitialContext ic = new InitialContext();
- txManager = (TransactionManager)
- ic.lookup("java:appserver/TransactionManager");
- } catch (Throwable th) {
- throw new RuntimeException(th);
- }
- }
- %>
- <%
- TransactionManager tMgr = txManager;
- tMgr.begin();
- InitialContext ic = new InitialContext();
- DataSource ds = (DataSource) ic.lookup("mi_pe");
- Connection conn = ds.getConnection();
- Statement s = conn.createStatement();
- ResultSet rs = s.executeQuery("select nmr from inc_test");
- rs.next();
- %>
- Before : <%=rs.getLong("nmr")%><br>
- <%
- s.close();
- s = conn.createStatement();
- s.execute("update inc_test set nmr = nmr + 1");
- s.close();
- tMgr.rollback();
- conn.close();
- tMgr.begin();
- // re-create another connection in a new transaction
- ic = new InitialContext();
- ds = (DataSource) ic.lookup("mi_pe");
- conn = ds.getConnection();
- s = conn.createStatement();
- rs = s.executeQuery("select nmr from inc_test");
- rs.next();
- %>
- After : <%=rs.getLong("nmr")%><br>
- <%
- s.close();
- tMgr.rollback();
- conn.close();
- %>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment