Source Code Cross Referenced for ManagerSY.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas » jtests » beans » jdbc » 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 » JOnAS 4.8.6 » org.objectweb.jonas.jtests.beans.jdbc 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.objectweb.jonas.jtests.beans.jdbc;
002:
003:        import java.rmi.RemoteException;
004:        import java.sql.Connection;
005:        import java.sql.SQLException;
006:        import java.util.Hashtable;
007:        import java.util.Iterator;
008:
009:        import javax.ejb.CreateException;
010:        import javax.ejb.EJBException;
011:        import javax.ejb.SessionBean;
012:        import javax.ejb.SessionContext;
013:        import javax.ejb.SessionSynchronization;
014:        import javax.naming.InitialContext;
015:        import javax.naming.NamingException;
016:        import javax.rmi.PortableRemoteObject;
017:        import javax.sql.DataSource;
018:
019:        import org.objectweb.jonas.common.Log;
020:        import org.objectweb.util.monolog.api.BasicLevel;
021:        import org.objectweb.util.monolog.api.Logger;
022:
023:        /**
024:         * Stateful Session Bean that manages directly jdbc connections
025:         */
026:        public class ManagerSY implements  SessionBean, SessionSynchronization {
027:
028:            static private Logger logger = null;
029:            SessionContext ejbContext;
030:
031:            // ------------------------------------------------------------------
032:            // The state of this Stateful Session Bean
033:            // ------------------------------------------------------------------
034:            int conb = 100;
035:            InitialContext ictx = null;
036:            DataSource ds = null;
037:            Hashtable clist = new Hashtable(); // Connection List
038:
039:            // ------------------------------------------------------------------
040:            // SessionSynchronization implementation
041:            // ------------------------------------------------------------------
042:
043:            public void afterBegin() {
044:                logger.log(BasicLevel.DEBUG, "");
045:            }
046:
047:            public void beforeCompletion() {
048:                logger.log(BasicLevel.DEBUG, "");
049:            }
050:
051:            public void afterCompletion(boolean committed) {
052:                logger.log(BasicLevel.DEBUG, "");
053:            }
054:
055:            // ------------------------------------------------------------------
056:            // SessionBean implementation
057:            // ------------------------------------------------------------------
058:
059:            /**
060:             * Make here all initialisations needed in this stateful session bean
061:             */
062:            public void setSessionContext(SessionContext ctx) {
063:                if (logger == null)
064:                    logger = Log.getLogger("org.objectweb.jonas_tests");
065:                logger.log(BasicLevel.DEBUG, "");
066:                ejbContext = ctx;
067:                ds = getDataSource("java:comp/env/jdbc/mydb");
068:                // Get a Connection now
069:                try {
070:                    Connection c = ds.getConnection();
071:                    clist.put(new Integer(0), c);
072:                } catch (SQLException e) {
073:                    throw new EJBException("Cannot get first Connection:" + e);
074:                }
075:            }
076:
077:            public void ejbRemove() {
078:                logger.log(BasicLevel.DEBUG, "");
079:                // Close all opened connections
080:                for (Iterator i = clist.values().iterator(); i.hasNext();) {
081:                    Connection c = (Connection) i.next();
082:                    try {
083:                        c.close();
084:                    } catch (SQLException e) {
085:                        logger.log(BasicLevel.ERROR, "Cannot close Connection:"
086:                                + e);
087:                    }
088:                }
089:            }
090:
091:            public void ejbCreate() throws CreateException {
092:                logger.log(BasicLevel.DEBUG, "");
093:            }
094:
095:            public void ejbPassivate() {
096:                logger.log(BasicLevel.DEBUG, "");
097:            }
098:
099:            public void ejbActivate() {
100:                logger.log(BasicLevel.DEBUG, "");
101:            }
102:
103:            // ------------------------------------------------------------------
104:            // Manager implementation
105:            // ------------------------------------------------------------------
106:
107:            /**
108:             * open and close a Connection
109:             */
110:            public boolean openCloseConnection() throws RemoteException {
111:                logger.log(BasicLevel.DEBUG, "");
112:                Connection c = null;
113:                try {
114:                    c = ds.getConnection();
115:                } catch (SQLException e) {
116:                    logger
117:                            .log(BasicLevel.ERROR, "Cannot get a Connection:"
118:                                    + e);
119:                    return false;
120:                }
121:                try {
122:                    c.close();
123:                } catch (SQLException e) {
124:                    logger
125:                            .log(BasicLevel.ERROR, "Cannot close Connection:"
126:                                    + e);
127:                    return false;
128:                }
129:                return true;
130:            }
131:
132:            /**
133:             * return the number associated to the connection opened,
134:             * or 0 if it failed.
135:             */
136:            public int openConnection() throws RemoteException {
137:                logger.log(BasicLevel.DEBUG, "");
138:                Connection c = null;
139:                try {
140:                    c = ds.getConnection();
141:                } catch (SQLException e) {
142:                    logger
143:                            .log(BasicLevel.ERROR, "Cannot get a Connection:"
144:                                    + e);
145:                    return 0;
146:                }
147:                // choose a connection number
148:                conb++;
149:                clist.put(new Integer(conb), c);
150:                logger.log(BasicLevel.DEBUG, "connection number=" + conb);
151:                return conb;
152:            }
153:
154:            /**
155:             * Return true if close Connection worked
156:             */
157:            public boolean closeConnection(int nb) throws RemoteException {
158:                logger.log(BasicLevel.DEBUG, "connection number=" + conb);
159:                Connection c = (Connection) clist.remove(new Integer(nb));
160:                if (c == null) {
161:                    logger.log(BasicLevel.ERROR, "Unknown Connection");
162:                    return false;
163:                }
164:                try {
165:                    c.close();
166:                } catch (SQLException e) {
167:                    logger
168:                            .log(BasicLevel.ERROR, "Cannot close Connection:"
169:                                    + e);
170:                    return false;
171:                }
172:                boolean isclosed = false;
173:                try {
174:                    isclosed = c.isClosed();
175:                } catch (SQLException e) {
176:                    logger.log(BasicLevel.ERROR, "Cannot check if closed:" + e);
177:                    return false;
178:                }
179:                return isclosed;
180:            }
181:
182:            // ------------------------------------------------------------------
183:            // private methods
184:            // ------------------------------------------------------------------
185:
186:            /*
187:             * get the DataSource given its name in JNDI
188:             */
189:            private DataSource getDataSource(String db) {
190:
191:                // lookup the DataSource in the initial context
192:                DataSource ds = null;
193:                try {
194:                    // get initial context
195:                    if (ictx == null) {
196:                        ictx = new InitialContext();
197:                    }
198:                    ds = (DataSource) PortableRemoteObject.narrow(ictx
199:                            .lookup(db), DataSource.class);
200:                } catch (NamingException e) {
201:                    logger.log(BasicLevel.ERROR, "Cannot lookup datasource "
202:                            + db + ": " + e);
203:                    throw new EJBException("Cannot access DataSource");
204:                }
205:
206:                // return it
207:                return ds;
208:            }
209:
210:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.