1 22 package org.jboss.test.xa.bean; 23 24 import java.sql.Connection ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import javax.ejb.CreateException ; 29 import javax.ejb.EJBException ; 30 import javax.ejb.SessionBean ; 31 import javax.ejb.SessionContext ; 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingException ; 35 import javax.sql.DataSource ; 36 import org.jboss.test.xa.interfaces.CantSeeDataException; 37 38 public class XATestBean 39 implements SessionBean 40 { 41 org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass()); 42 43 public final static String DROP_TABLE = 44 "DROP TABLE XA_TEST"; 45 46 public final static String CREATE_TABLE = 47 "CREATE TABLE XA_TEST(ID INTEGER NOT NULL PRIMARY KEY, DATA INTEGER NOT NULL)"; 48 49 public final static String DB_1_NAME = "java:comp/env/jdbc/DBConnection1"; 50 public final static String DB_2_NAME = "java:comp/env/jdbc/DBConnection2"; 51 52 public XATestBean() { 53 } 54 55 public void ejbCreate() throws CreateException { 56 } 57 58 public void ejbActivate() throws EJBException { 59 } 60 61 public void ejbPassivate() throws EJBException { 62 } 63 64 public void ejbRemove() throws EJBException { 65 } 66 67 public void setSessionContext(SessionContext parm1) throws EJBException { 68 } 69 70 protected void execute(DataSource ds, String sql) throws SQLException { 71 Connection con = ds.getConnection(); 72 try { 73 Statement s = con.createStatement(); 74 s.execute(sql); 75 s.close(); 76 } 77 finally { 78 con.close(); 79 } 80 } 81 82 protected void execute(Connection con, String sql) throws SQLException { 83 Statement s = con.createStatement(); 84 s.execute(sql); 85 s.close(); 86 } 87 88 public void createTables() throws NamingException , SQLException { 89 Context ctx = new InitialContext (); 90 try { 91 DataSource ds1 = (DataSource )ctx.lookup(DB_1_NAME); 92 try { 93 execute(ds1, DROP_TABLE); 94 } 95 catch (Exception ignore) {} 96 execute(ds1, CREATE_TABLE); 97 98 DataSource ds2 = (DataSource )ctx.lookup(DB_2_NAME); 99 try { 100 execute(ds2, DROP_TABLE); 101 } 102 catch (Exception ignore) {} 103 execute(ds2, CREATE_TABLE); 104 } 105 finally { 106 ctx.close(); 107 } 108 } 109 110 public void clearData() { 111 try { 112 Context ctx = new InitialContext (); 113 DataSource db1ds = (DataSource )ctx.lookup(DB_1_NAME); 114 Connection db1con = db1ds.getConnection(); 115 Statement db1st = db1con.createStatement(); 116 db1st.executeUpdate("DELETE FROM XA_TEST"); 117 db1st.close(); 118 119 DataSource db2ds = (DataSource )ctx.lookup(DB_2_NAME); 120 Connection db2con = db2ds.getConnection(); 121 Statement db2st = db2con.createStatement(); 122 db2st.executeUpdate("DELETE FROM XA_TEST"); 123 db2st.close(); 124 125 db2con.close(); 126 db1con.close(); 127 } catch(SQLException e) { 128 throw new EJBException ("Unable to clear data (have tables been created?): "+e); 129 } catch(NamingException e) { 130 throw new EJBException ("Unable to find DB pool: "+e); 131 } 132 } 133 134 public void doWork() throws CantSeeDataException { 135 Connection db1cona = null, db1conb = null, db2con = null; 136 try { 137 Context ctx = new InitialContext (); 139 DataSource db1ds = (DataSource )ctx.lookup(DB_1_NAME); 140 db1cona = db1ds.getConnection(); 141 db1conb = db1ds.getConnection(); 142 DataSource db2ds = (DataSource )ctx.lookup(DB_2_NAME); 143 db2con = db2ds.getConnection(); 144 145 Statement s = db1cona.createStatement(); 147 int data = (int)(System.currentTimeMillis() & 0x0000FFFFL); 148 s.executeUpdate("INSERT INTO XA_TEST (ID, DATA) VALUES (1, "+data+")"); 149 s.close(); 150 151 s = db1conb.createStatement(); 153 int result = -1; 154 ResultSet rs = s.executeQuery("SELECT DATA FROM XA_TEST WHERE ID=1"); 155 while(rs.next()) { 156 result = rs.getInt(1); 157 } 158 rs.close(); 159 s.close(); 160 161 s = db2con.createStatement(); 163 s.executeUpdate("INSERT INTO XA_TEST (ID, DATA) VALUES (1, "+data+")"); 164 s.close(); 165 166 if(result != data) 167 throw new CantSeeDataException("Insert performed on one connection wasn't visible\n"+ 168 "to another connection in the same transaction!"); 169 170 } catch(SQLException e) { 171 throw new EJBException ("Unable to clear data (have tables been created?): "+e); 172 } catch(NamingException e) { 173 throw new EJBException ("Unable to find DB pool: "+e); 174 } finally { 175 if(db2con != null) try {db2con.close();}catch(SQLException e) {} 177 if(db1cona != null) try {db1cona.close();}catch(SQLException e) {} 178 if(db1conb != null) try {db1conb.close();}catch(SQLException e) {} 179 } 180 } 181 } 182 | Popular Tags |