Source Code Cross Referenced for XATestBean.java in  » EJB-Server-JBoss-4.2.1 » testsuite » org » jboss » test » xa » bean » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » EJB Server JBoss 4.2.1 » testsuite » org.jboss.test.xa.bean 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.