Source Code Cross Referenced for BeanTxStatelessBean.java in  » J2EE » openejb3 » org » apache » openejb » test » stateless » 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 » J2EE » openejb3 » org.apache.openejb.test.stateless 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */package org.apache.openejb.test.stateless;
017:
018:        import java.rmi.RemoteException;
019:        import java.sql.Connection;
020:        import java.sql.PreparedStatement;
021:        import java.sql.ResultSet;
022:
023:        import javax.ejb.CreateException;
024:        import javax.ejb.EJBException;
025:        import javax.ejb.SessionContext;
026:        import javax.naming.InitialContext;
027:        import javax.sql.DataSource;
028:        import javax.transaction.RollbackException;
029:        import javax.transaction.UserTransaction;
030:
031:        import org.apache.openejb.test.object.Account;
032:        import org.apache.openejb.test.object.Transaction;
033:
034:        /**
035:         * 
036:         * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
037:         * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
038:         */
039:        public class BeanTxStatelessBean implements  javax.ejb.SessionBean {
040:
041:            private String name;
042:            private SessionContext ejbContext;
043:            private InitialContext jndiContext;
044:            public final String jndiDatabaseEntry = "jdbc/stateless/beanManagedTransaction/database";
045:
046:            //=============================
047:            // Home interface methods
048:            //    
049:
050:            //    
051:            // Home interface methods
052:            //=============================
053:
054:            //=============================
055:            // Remote interface methods
056:            //    
057:
058:            public Transaction getUserTransaction() throws RemoteException {
059:
060:                UserTransaction ut = null;
061:                try {
062:                    ut = ejbContext.getUserTransaction();
063:                } catch (IllegalStateException ise) {
064:                    throw new RemoteException(ise.getMessage());
065:                }
066:                if (ut == null)
067:                    return null;
068:                return new Transaction(ut);
069:            }
070:
071:            public Transaction jndiUserTransaction() throws RemoteException {
072:                UserTransaction ut = null;
073:                try {
074:                    ut = (UserTransaction) jndiContext
075:                            .lookup("java:comp/UserTransaction");
076:                } catch (Exception e) {
077:                    throw new RemoteException(e.getMessage());
078:                }
079:                if (ut == null)
080:                    return null;
081:                return new Transaction(ut);
082:            }
083:
084:            public void openAccount(Account acct, Boolean rollback)
085:                    throws RemoteException, RollbackException {
086:
087:                try {
088:                    DataSource ds = (DataSource) javax.rmi.PortableRemoteObject
089:                            .narrow(jndiContext
090:                                    .lookup("java:comp/env/database"),
091:                                    DataSource.class);
092:                    Connection con = ds.getConnection();
093:
094:                    try {
095:                        UserTransaction ut = ejbContext.getUserTransaction();
096:                        /*[1] Begin the transaction */
097:                        ut.begin();
098:
099:                        /*[2] Update the table */
100:                        PreparedStatement stmt = con
101:                                .prepareStatement("insert into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)");
102:                        try {
103:                            stmt.setString(1, acct.getSsn());
104:                            stmt.setString(2, acct.getFirstName());
105:                            stmt.setString(3, acct.getLastName());
106:                            stmt.setInt(4, acct.getBalance());
107:                            stmt.executeUpdate();
108:                        } finally {
109:                            stmt.close();
110:                        }
111:
112:                        /*[3] Commit or Rollback the transaction */
113:                        if (rollback.booleanValue())
114:                            ut.setRollbackOnly();
115:
116:                        /*[4] Commit or Rollback the transaction */
117:                        ut.commit();
118:                    } finally {
119:                        con.close();
120:                    }
121:                } catch (RollbackException re) {
122:                    throw re;
123:                } catch (Exception e) {
124:                    e.printStackTrace();
125:                    throw new RemoteException("[Bean] "
126:                            + e.getClass().getName() + " : " + e.getMessage());
127:                }
128:            }
129:
130:            public Account retreiveAccount(String ssn) throws RemoteException {
131:                Account acct = new Account();
132:                try {
133:                    DataSource ds = (DataSource) javax.rmi.PortableRemoteObject
134:                            .narrow(jndiContext
135:                                    .lookup("java:comp/env/database"),
136:                                    DataSource.class);
137:                    Connection con = ds.getConnection();
138:
139:                    try {
140:                        PreparedStatement stmt = con
141:                                .prepareStatement("select * from Account where SSN = ?");
142:                        try {
143:                            stmt.setString(1, ssn);
144:                            ResultSet rs = stmt.executeQuery();
145:                            if (!rs.next())
146:                                return null;
147:
148:                            acct.setSsn(rs.getString(1));
149:                            acct.setFirstName(rs.getString(2));
150:                            acct.setLastName(rs.getString(3));
151:                            acct.setBalance(rs.getInt(4));
152:                        } finally {
153:                            stmt.close();
154:                        }
155:                    } finally {
156:                        con.close();
157:                    }
158:                } catch (Exception e) {
159:                    e.printStackTrace();
160:                    throw new RemoteException("[Bean] "
161:                            + e.getClass().getName() + " : " + e.getMessage());
162:                }
163:                return acct;
164:            }
165:
166:            //    
167:            // Remote interface methods
168:            //=============================
169:
170:            //=================================
171:            // SessionBean interface methods
172:            //    
173:            /**
174:             * 
175:             * @param name
176:             * @exception javax.ejb.CreateException
177:             */
178:            public void ejbCreate() throws javax.ejb.CreateException {
179:                try {
180:                    jndiContext = new InitialContext();
181:                } catch (Exception e) {
182:                    throw new CreateException(
183:                            "Can not get the initial context: "
184:                                    + e.getMessage());
185:                }
186:            }
187:
188:            /**
189:             * Set the associated session context. The container calls this method
190:             * after the instance creation.
191:             */
192:            public void setSessionContext(SessionContext ctx)
193:                    throws EJBException, RemoteException {
194:                ejbContext = ctx;
195:            }
196:
197:            /**
198:             * A container invokes this method before it ends the life of the session
199:             * object. This happens as a result of a client's invoking a remove
200:             * operation, or when a container decides to terminate the session object
201:             * after a timeout.
202:             */
203:            public void ejbRemove() throws EJBException, RemoteException {
204:            }
205:
206:            /**
207:             * The activate method is called when the instance is activated
208:             * from its "passive" state. The instance should acquire any resource
209:             * that it has released earlier in the ejbPassivate() method.
210:             */
211:            public void ejbActivate() throws EJBException, RemoteException {
212:            }
213:
214:            /**
215:             * The passivate method is called before the instance enters
216:             * the "passive" state. The instance should release any resources that
217:             * it can re-acquire later in the ejbActivate() method.
218:             */
219:            public void ejbPassivate() throws EJBException, RemoteException {
220:            }
221:
222:            //    
223:            // SessionBean interface methods
224:            //==================================
225:
226:            public String remove(String arg) {
227:                return arg;
228:            }
229:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.