001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.xa.bean;
023:
024: import java.sql.Connection;
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027: import java.sql.Statement;
028: import javax.ejb.CreateException;
029: import javax.ejb.EJBException;
030: import javax.ejb.SessionBean;
031: import javax.ejb.SessionContext;
032: import javax.naming.Context;
033: import javax.naming.InitialContext;
034: import javax.naming.NamingException;
035: import javax.sql.DataSource;
036: import org.jboss.test.xa.interfaces.CantSeeDataException;
037:
038: public class XATestBean implements SessionBean {
039: org.apache.log4j.Category log = org.apache.log4j.Category
040: .getInstance(getClass());
041:
042: public final static String DROP_TABLE = "DROP TABLE XA_TEST";
043:
044: public final static String CREATE_TABLE = "CREATE TABLE XA_TEST(ID INTEGER NOT NULL PRIMARY KEY, DATA INTEGER NOT NULL)";
045:
046: public final static String DB_1_NAME = "java:comp/env/jdbc/DBConnection1";
047: public final static String DB_2_NAME = "java:comp/env/jdbc/DBConnection2";
048:
049: public XATestBean() {
050: }
051:
052: public void ejbCreate() throws CreateException {
053: }
054:
055: public void ejbActivate() throws EJBException {
056: }
057:
058: public void ejbPassivate() throws EJBException {
059: }
060:
061: public void ejbRemove() throws EJBException {
062: }
063:
064: public void setSessionContext(SessionContext parm1)
065: throws EJBException {
066: }
067:
068: protected void execute(DataSource ds, String sql)
069: throws SQLException {
070: Connection con = ds.getConnection();
071: try {
072: Statement s = con.createStatement();
073: s.execute(sql);
074: s.close();
075: } finally {
076: con.close();
077: }
078: }
079:
080: protected void execute(Connection con, String sql)
081: throws SQLException {
082: Statement s = con.createStatement();
083: s.execute(sql);
084: s.close();
085: }
086:
087: public void createTables() throws NamingException, SQLException {
088: Context ctx = new InitialContext();
089: try {
090: DataSource ds1 = (DataSource) ctx.lookup(DB_1_NAME);
091: try {
092: execute(ds1, DROP_TABLE);
093: } catch (Exception ignore) {
094: }
095: execute(ds1, CREATE_TABLE);
096:
097: DataSource ds2 = (DataSource) ctx.lookup(DB_2_NAME);
098: try {
099: execute(ds2, DROP_TABLE);
100: } catch (Exception ignore) {
101: }
102: execute(ds2, CREATE_TABLE);
103: } finally {
104: ctx.close();
105: }
106: }
107:
108: public void clearData() {
109: try {
110: Context ctx = new InitialContext();
111: DataSource db1ds = (DataSource) ctx.lookup(DB_1_NAME);
112: Connection db1con = db1ds.getConnection();
113: Statement db1st = db1con.createStatement();
114: db1st.executeUpdate("DELETE FROM XA_TEST");
115: db1st.close();
116:
117: DataSource db2ds = (DataSource) ctx.lookup(DB_2_NAME);
118: Connection db2con = db2ds.getConnection();
119: Statement db2st = db2con.createStatement();
120: db2st.executeUpdate("DELETE FROM XA_TEST");
121: db2st.close();
122:
123: db2con.close();
124: db1con.close();
125: } catch (SQLException e) {
126: throw new EJBException(
127: "Unable to clear data (have tables been created?): "
128: + 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: // Create 3 connections
138: 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: // Insert some data on one connection
146: Statement s = db1cona.createStatement();
147: int data = (int) (System.currentTimeMillis() & 0x0000FFFFL);
148: s
149: .executeUpdate("INSERT INTO XA_TEST (ID, DATA) VALUES (1, "
150: + data + ")");
151: s.close();
152:
153: // Verify that another connection on the same DS can read it
154: s = db1conb.createStatement();
155: int result = -1;
156: ResultSet rs = s
157: .executeQuery("SELECT DATA FROM XA_TEST WHERE ID=1");
158: while (rs.next()) {
159: result = rs.getInt(1);
160: }
161: rs.close();
162: s.close();
163:
164: // Do some work on the other data source
165: s = db2con.createStatement();
166: s
167: .executeUpdate("INSERT INTO XA_TEST (ID, DATA) VALUES (1, "
168: + data + ")");
169: s.close();
170:
171: if (result != data)
172: throw new CantSeeDataException(
173: "Insert performed on one connection wasn't visible\n"
174: + "to another connection in the same transaction!");
175:
176: } catch (SQLException e) {
177: throw new EJBException(
178: "Unable to clear data (have tables been created?): "
179: + e);
180: } catch (NamingException e) {
181: throw new EJBException("Unable to find DB pool: " + e);
182: } finally {
183: // Close all connections
184: if (db2con != null)
185: try {
186: db2con.close();
187: } catch (SQLException e) {
188: }
189: if (db1cona != null)
190: try {
191: db1cona.close();
192: } catch (SQLException e) {
193: }
194: if (db1conb != null)
195: try {
196: db1conb.close();
197: } catch (SQLException e) {
198: }
199: }
200: }
201: }
|