KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > recover > oracle > OracleTest


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.transaction.TransactionManager JavaDoc;
34 import javax.transaction.RollbackException JavaDoc;
35 import javax.transaction.HeuristicMixedException JavaDoc;
36 import javax.transaction.HeuristicRollbackException JavaDoc;
37 import javax.transaction.SystemException JavaDoc;
38 import javax.transaction.NotSupportedException JavaDoc;
39 import java.sql.Connection JavaDoc;
40 import java.sql.Statement JavaDoc;
41 import java.sql.SQLException JavaDoc;
42 import java.sql.ResultSet JavaDoc;
43
44 /**
45  * Comment
46  *
47  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
48  * @version $Revision: 37406 $
49  */

50 public class OracleTest extends ServiceMBeanSupport implements OracleTestMBean
51 {
52    private RecoveryManagerServiceMBean manager;
53    private MockLogger logger;
54    private DataSource JavaDoc xds1;
55    private DataSource JavaDoc xds2;
56    private DataSource JavaDoc ds1;
57    private DataSource JavaDoc ds2;
58    private TransactionManager JavaDoc 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 JavaDoc
71    {
72       super.startService();
73    }
74
75    public void initTester()
76    throws NamingException JavaDoc
77    {
78       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
79       xds1 = (DataSource JavaDoc) ctx.lookup("java:/OracleXA1");
80       xds2 = (DataSource JavaDoc) ctx.lookup("java:/OracleXA2");
81       ds1 = (DataSource JavaDoc) ctx.lookup("java:/Oracle1");
82       ds2 = (DataSource JavaDoc) ctx.lookup("java:/Oracle2");
83       tm = (TransactionManager JavaDoc) ctx.lookup("java:/TransactionManager");
84    }
85
86    public void createTables()
87    throws NotSupportedException JavaDoc, SystemException JavaDoc, SQLException JavaDoc, RollbackException JavaDoc, HeuristicMixedException JavaDoc, HeuristicRollbackException JavaDoc
88    {
89       tm.begin();
90       Connection JavaDoc conn1 = ds1.getConnection();
91       Connection JavaDoc 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 JavaDoc conn1)
100    throws SQLException JavaDoc
101    {
102       Statement JavaDoc st = conn1.createStatement();
103       try
104       {
105          st.execute("drop table xa_test_table");
106       }
107       catch (SQLException JavaDoc ignore)
108       {
109
110       }
111       try
112       {
113          st.execute("create table xa_test_table(col1 int)");
114       }
115       catch (SQLException JavaDoc ignore)
116       {
117          ignore.printStackTrace();
118       }
119       st.close();
120    }
121
122    public void testCommitRecover() throws Exception JavaDoc
123    {
124       createTables();
125
126       Connection JavaDoc conn1 = null;
127       Connection JavaDoc 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 JavaDoc st1 = conn1.createStatement();
140          Statement JavaDoc 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 JavaDoc("Transaction should have failed");
165
166          // a stop/start sequence will put current log file into recoverable mode
167
logger.stop();
168          logger.start();
169          manager.recover();
170
171          tm.suspend(); // old transaction
172

173          tm.begin();
174          conn1 = xds1.getConnection();
175          conn2 = xds1.getConnection();
176          Statement JavaDoc stmta = conn1.createStatement();
177          Statement JavaDoc stmtb = conn2.createStatement();
178
179          try
180          {
181             ResultSet JavaDoc 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 JavaDoc("No recovery on 1");
194
195
196             ResultSet JavaDoc 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 JavaDoc("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