1 22 package org.jboss.test.recover.oracle; 23 24 import org.jboss.system.ServiceMBeanSupport; 25 import org.jboss.test.recover.bean.MockLoggerServiceMBean; 26 import org.jboss.test.recover.interfaces.MockLogger; 27 import org.jboss.tm.recovery.RecoveryTestingException; 28 import org.jboss.tm.recovery.RecoveryManagerServiceMBean; 29 30 import javax.sql.DataSource ; 31 import javax.naming.InitialContext ; 32 import javax.naming.NamingException ; 33 import javax.transaction.TransactionManager ; 34 import javax.transaction.RollbackException ; 35 import javax.transaction.HeuristicMixedException ; 36 import javax.transaction.HeuristicRollbackException ; 37 import javax.transaction.SystemException ; 38 import javax.transaction.NotSupportedException ; 39 import java.sql.Connection ; 40 import java.sql.Statement ; 41 import java.sql.SQLException ; 42 import java.sql.ResultSet ; 43 44 50 public class OracleTest extends ServiceMBeanSupport implements OracleTestMBean 51 { 52 private RecoveryManagerServiceMBean manager; 53 private MockLogger logger; 54 private DataSource xds1; 55 private DataSource xds2; 56 private DataSource ds1; 57 private DataSource ds2; 58 private TransactionManager tm; 59 60 public void setMock(MockLoggerServiceMBean mock) 61 { 62 this.logger = (MockLogger) mock.getInstance(); 63 } 64 65 public void setManager(RecoveryManagerServiceMBean manager) 66 { 67 this.manager = manager; 68 } 69 70 protected void startService() throws Exception 71 { 72 super.startService(); 73 } 74 75 public void initTester() 76 throws NamingException 77 { 78 InitialContext ctx = new InitialContext (); 79 xds1 = (DataSource ) ctx.lookup("java:/OracleXA1"); 80 xds2 = (DataSource ) ctx.lookup("java:/OracleXA2"); 81 ds1 = (DataSource ) ctx.lookup("java:/Oracle1"); 82 ds2 = (DataSource ) ctx.lookup("java:/Oracle2"); 83 tm = (TransactionManager ) ctx.lookup("java:/TransactionManager"); 84 } 85 86 public void createTables() 87 throws NotSupportedException , SystemException , SQLException , RollbackException , HeuristicMixedException , HeuristicRollbackException 88 { 89 tm.begin(); 90 Connection conn1 = ds1.getConnection(); 91 Connection conn2 = ds2.getConnection(); 92 initialize(conn1); 93 initialize(conn2); 94 conn1.close(); 95 conn2.close(); 96 tm.commit(); 97 } 98 99 private void initialize(Connection conn1) 100 throws SQLException 101 { 102 Statement st = conn1.createStatement(); 103 try 104 { 105 st.execute("drop table xa_test_table"); 106 } 107 catch (SQLException ignore) 108 { 109 110 } 111 try 112 { 113 st.execute("create table xa_test_table(col1 int)"); 114 } 115 catch (SQLException ignore) 116 { 117 ignore.printStackTrace(); 118 } 119 st.close(); 120 } 121 122 public void testCommitRecover() throws Exception 123 { 124 createTables(); 125 126 Connection conn1 = null; 127 Connection conn2 = null; 128 129 logger.setFailAfterCommitting(true); 130 logger.setFailAfter(0); 131 132 boolean didntFail = false; 133 134 try 135 { 136 tm.begin(); 137 conn1 = xds1.getConnection(); 138 conn2 = xds2.getConnection(); 139 Statement st1 = conn1.createStatement(); 140 Statement st2 = conn2.createStatement(); 141 try 142 { 143 st1.executeUpdate("insert into xa_test_table values(1)"); 144 st2.executeUpdate("insert into xa_test_table values(1)"); 145 } 146 finally 147 { 148 st1.close(); 149 st2.close(); 150 conn1.close(); 151 conn2.close(); 152 } 153 try 154 { 155 tm.commit(); 156 didntFail = true; 157 } 158 catch (RecoveryTestingException e) 159 { 160 } 161 162 logger.setFailAfterCommitting(false); 163 164 if (didntFail) throw new RuntimeException ("Transaction should have failed"); 165 166 logger.stop(); 168 logger.start(); 169 manager.recover(); 170 171 tm.suspend(); 173 tm.begin(); 174 conn1 = xds1.getConnection(); 175 conn2 = xds1.getConnection(); 176 Statement stmta = conn1.createStatement(); 177 Statement stmtb = conn2.createStatement(); 178 179 try 180 { 181 ResultSet rset = stmta.executeQuery("select col1 from xa_test_table"); 182 int count1 = 0; 183 184 while (rset.next()) 185 { 186 System.out.println("Col1 is " + rset.getInt(1)); 187 count1++; 188 } 189 190 rset.close(); 191 rset = null; 192 193 if (count1 != 1) throw new RuntimeException ("No recovery on 1"); 194 195 196 ResultSet rset2 = stmta.executeQuery("select col1 from xa_test_table"); 197 int count2 = 0; 198 199 while (rset2.next()) 200 { 201 System.out.println("Col1 is " + rset2.getInt(1)); 202 count2++; 203 } 204 205 rset2.close(); 206 rset2 = null; 207 208 if (count2 != 1) throw new RuntimeException ("No recovery on 2"); 209 210 } 211 finally 212 { 213 stmta.close(); 214 stmta = null; 215 stmtb.close(); 216 stmtb = null; 217 conn1.close(); 218 conn2.close(); 219 } 220 tm.commit(); 221 222 } 223 finally 224 { 225 logger.setFailAfterCommitting(false); 226 } 227 } 228 229 } 230 | Popular Tags |