KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > xa > bean > XATestBean


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.xa.bean;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import javax.ejb.CreateException JavaDoc;
29 import javax.ejb.EJBException JavaDoc;
30 import javax.ejb.SessionBean JavaDoc;
31 import javax.ejb.SessionContext JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import javax.sql.DataSource JavaDoc;
36 import org.jboss.test.xa.interfaces.CantSeeDataException;
37
38 public class XATestBean
39     implements SessionBean JavaDoc
40 {
41    org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass());
42    
43     public final static String JavaDoc DROP_TABLE =
44         "DROP TABLE XA_TEST";
45
46     public final static String JavaDoc CREATE_TABLE =
47         "CREATE TABLE XA_TEST(ID INTEGER NOT NULL PRIMARY KEY, DATA INTEGER NOT NULL)";
48
49     public final static String JavaDoc DB_1_NAME = "java:comp/env/jdbc/DBConnection1";
50     public final static String JavaDoc DB_2_NAME = "java:comp/env/jdbc/DBConnection2";
51
52     public XATestBean() {
53     }
54
55     public void ejbCreate() throws CreateException JavaDoc {
56     }
57
58     public void ejbActivate() throws EJBException JavaDoc {
59     }
60
61     public void ejbPassivate() throws EJBException JavaDoc {
62     }
63
64     public void ejbRemove() throws EJBException JavaDoc {
65     }
66
67     public void setSessionContext(SessionContext JavaDoc parm1) throws EJBException JavaDoc {
68     }
69
70     protected void execute(DataSource JavaDoc ds, String JavaDoc sql) throws SQLException JavaDoc {
71         Connection JavaDoc con = ds.getConnection();
72         try {
73             Statement JavaDoc s = con.createStatement();
74             s.execute(sql);
75             s.close();
76         }
77         finally {
78             con.close();
79         }
80     }
81
82     protected void execute(Connection JavaDoc con, String JavaDoc sql) throws SQLException JavaDoc {
83         Statement JavaDoc s = con.createStatement();
84         s.execute(sql);
85         s.close();
86     }
87     
88     public void createTables() throws NamingException JavaDoc, SQLException JavaDoc {
89         Context JavaDoc ctx = new InitialContext JavaDoc();
90         try {
91             DataSource JavaDoc ds1 = (DataSource JavaDoc)ctx.lookup(DB_1_NAME);
92             try {
93                 execute(ds1, DROP_TABLE);
94             }
95             catch (Exception JavaDoc ignore) {}
96             execute(ds1, CREATE_TABLE);
97
98             DataSource JavaDoc ds2 = (DataSource JavaDoc)ctx.lookup(DB_2_NAME);
99             try {
100                 execute(ds2, DROP_TABLE);
101             }
102             catch (Exception JavaDoc ignore) {}
103             execute(ds2, CREATE_TABLE);
104         }
105         finally {
106             ctx.close();
107         }
108     }
109     
110     public void clearData() {
111         try {
112             Context JavaDoc ctx = new InitialContext JavaDoc();
113             DataSource JavaDoc db1ds = (DataSource JavaDoc)ctx.lookup(DB_1_NAME);
114             Connection JavaDoc db1con = db1ds.getConnection();
115             Statement JavaDoc db1st = db1con.createStatement();
116             db1st.executeUpdate("DELETE FROM XA_TEST");
117             db1st.close();
118
119             DataSource JavaDoc db2ds = (DataSource JavaDoc)ctx.lookup(DB_2_NAME);
120             Connection JavaDoc db2con = db2ds.getConnection();
121             Statement JavaDoc db2st = db2con.createStatement();
122             db2st.executeUpdate("DELETE FROM XA_TEST");
123             db2st.close();
124
125             db2con.close();
126             db1con.close();
127         } catch(SQLException JavaDoc e) {
128             throw new EJBException JavaDoc("Unable to clear data (have tables been created?): "+e);
129         } catch(NamingException JavaDoc e) {
130             throw new EJBException JavaDoc("Unable to find DB pool: "+e);
131         }
132     }
133
134     public void doWork() throws CantSeeDataException {
135         Connection JavaDoc db1cona = null, db1conb = null, db2con = null;
136         try {
137         // Create 3 connections
138
Context JavaDoc ctx = new InitialContext JavaDoc();
139             DataSource JavaDoc db1ds = (DataSource JavaDoc)ctx.lookup(DB_1_NAME);
140             db1cona = db1ds.getConnection();
141             db1conb = db1ds.getConnection();
142             DataSource JavaDoc db2ds = (DataSource JavaDoc)ctx.lookup(DB_2_NAME);
143             db2con = db2ds.getConnection();
144
145         // Insert some data on one connection
146
Statement JavaDoc 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         // Verify that another connection on the same DS can read it
152
s = db1conb.createStatement();
153             int result = -1;
154             ResultSet JavaDoc 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         // Do some work on the other data source
162
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 JavaDoc e) {
171             throw new EJBException JavaDoc("Unable to clear data (have tables been created?): "+e);
172         } catch(NamingException JavaDoc e) {
173             throw new EJBException JavaDoc("Unable to find DB pool: "+e);
174         } finally {
175         // Close all connections
176
if(db2con != null) try {db2con.close();}catch(SQLException JavaDoc e) {}
177             if(db1cona != null) try {db1cona.close();}catch(SQLException JavaDoc e) {}
178             if(db1conb != null) try {db1conb.close();}catch(SQLException JavaDoc e) {}
179         }
180     }
181 }
182
Popular Tags