Source Code Cross Referenced for BeanTxStatefulBean.java in  » J2EE » openejb3 » org » apache » openejb » test » stateful » 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.stateful 
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.stateful;
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 BeanTxStatefulBean implements  javax.ejb.SessionBean {
040:
041:            private String name;
042:            private SessionContext ejbContext;
043:            private InitialContext jndiContext;
044:            public final String jndiDatabaseEntry = "jdbc/stateful/beanManagedTransaction/database";
045:
046:            //=============================
047:            // Home interface methods
048:            //    
049:            /**
050:             * Maps to BasicStatefulHome.create
051:             * 
052:             * @param name
053:             * @exception javax.ejb.CreateException
054:             * @see BasicStatefulHome#createObject
055:             */
056:            public void ejbCreate(String name) throws javax.ejb.CreateException {
057:                this .name = name;
058:                try {
059:                    jndiContext = new InitialContext();
060:                } catch (Exception e) {
061:                    throw new CreateException(
062:                            "Can not get the initial context: "
063:                                    + e.getMessage());
064:                }
065:            }
066:
067:            //    
068:            // Home interface methods
069:            //=============================
070:
071:            //=============================
072:            // Remote interface methods
073:            //    
074:
075:            /**
076:             * Maps to BasicStatefulObject.businessMethod
077:             * 
078:             * @return 
079:             * @see BasicStatefulObject#businessMethod
080:             */
081:            public Transaction getUserTransaction() throws RemoteException {
082:
083:                UserTransaction ut = null;
084:                try {
085:                    ut = ejbContext.getUserTransaction();
086:                } catch (IllegalStateException ise) {
087:                    throw new RemoteException(ise.getMessage());
088:                }
089:                if (ut == null)
090:                    return null;
091:                return new Transaction(ut);
092:            }
093:
094:            public Transaction jndiUserTransaction() throws RemoteException {
095:                UserTransaction ut = null;
096:                try {
097:                    ut = (UserTransaction) jndiContext
098:                            .lookup("java:comp/UserTransaction");
099:                } catch (Exception e) {
100:                    throw new RemoteException(e.getMessage());
101:                }
102:                if (ut == null)
103:                    return null;
104:                return new Transaction(ut);
105:            }
106:
107:            public void openAccount(Account acct, Boolean rollback)
108:                    throws RemoteException, RollbackException {
109:
110:                try {
111:
112:                    DataSource ds = (DataSource) javax.rmi.PortableRemoteObject
113:                            .narrow(jndiContext
114:                                    .lookup("java:comp/env/datasource"),
115:                                    DataSource.class);
116:                    Connection con = ds.getConnection();
117:
118:                    try {
119:                        UserTransaction ut = ejbContext.getUserTransaction();
120:                        /*[1] Begin the transaction */
121:                        ut.begin();
122:
123:                        /*[2] Update the table */
124:                        PreparedStatement stmt = con
125:                                .prepareStatement("insert into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)");
126:                        try {
127:                            stmt.setString(1, acct.getSsn());
128:                            stmt.setString(2, acct.getFirstName());
129:                            stmt.setString(3, acct.getLastName());
130:                            stmt.setInt(4, acct.getBalance());
131:                            stmt.executeUpdate();
132:                        } finally {
133:                            stmt.close();
134:                        }
135:
136:                        /*[3] Commit or Rollback the transaction */
137:                        if (rollback.booleanValue())
138:                            ut.setRollbackOnly();
139:
140:                        /*[4] Commit or Rollback the transaction */
141:                        ut.commit();
142:
143:                    } finally {
144:                        con.close();
145:                    }
146:                } catch (RollbackException re) {
147:                    throw re;
148:                } catch (Exception e) {
149:                    e.printStackTrace();
150:                    throw new RemoteException("[Bean] "
151:                            + e.getClass().getName() + " : " + e.getMessage());
152:                }
153:            }
154:
155:            public Account retreiveAccount(String ssn) throws RemoteException {
156:                Account acct = new Account();
157:                try {
158:                    DataSource ds = (DataSource) javax.rmi.PortableRemoteObject
159:                            .narrow(jndiContext
160:                                    .lookup("java:comp/env/datasource"),
161:                                    DataSource.class);
162:                    Connection con = ds.getConnection();
163:
164:                    try {
165:                        PreparedStatement stmt = con
166:                                .prepareStatement("select * from Account where SSN = ?");
167:                        try {
168:                            stmt.setString(1, ssn);
169:                            ResultSet rs = stmt.executeQuery();
170:                            if (!rs.next())
171:                                return null;
172:
173:                            acct.setSsn(rs.getString(1));
174:                            acct.setFirstName(rs.getString(2));
175:                            acct.setLastName(rs.getString(3));
176:                            acct.setBalance(rs.getInt(4));
177:                        } finally {
178:                            stmt.close();
179:                        }
180:                    } finally {
181:                        con.close();
182:                    }
183:                } catch (Exception e) {
184:                    e.printStackTrace();
185:                    throw new RemoteException("[Bean] "
186:                            + e.getClass().getName() + " : " + e.getMessage());
187:                }
188:                return acct;
189:            }
190:
191:            public String remove(String arg) {
192:                return arg;
193:            }
194:
195:            //    
196:            // Remote interface methods
197:            //=============================
198:
199:            //=================================
200:            // SessionBean interface methods
201:            //    
202:            /**
203:             * Set the associated session context. The container calls this method
204:             * after the instance creation.
205:             */
206:            public void setSessionContext(SessionContext ctx)
207:                    throws EJBException, RemoteException {
208:                ejbContext = ctx;
209:            }
210:
211:            /**
212:             * A container invokes this method before it ends the life of the session
213:             * object. This happens as a result of a client's invoking a remove
214:             * operation, or when a container decides to terminate the session object
215:             * after a timeout.
216:             */
217:            public void ejbRemove() throws EJBException, RemoteException {
218:            }
219:
220:            /**
221:             * The activate method is called when the instance is activated
222:             * from its "passive" state. The instance should acquire any resource
223:             * that it has released earlier in the ejbPassivate() method.
224:             */
225:            public void ejbActivate() throws EJBException, RemoteException {
226:            }
227:
228:            /**
229:             * The passivate method is called before the instance enters
230:             * the "passive" state. The instance should release any resources that
231:             * it can re-acquire later in the ejbActivate() method.
232:             */
233:            public void ejbPassivate() throws EJBException, RemoteException {
234:            }
235:            //    
236:            // SessionBean interface methods
237:            //==================================
238:
239:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.