KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > jdbc > ManagerSY


1 package org.objectweb.jonas.jtests.beans.jdbc;
2
3 import java.rmi.RemoteException JavaDoc;
4 import java.sql.Connection JavaDoc;
5 import java.sql.SQLException JavaDoc;
6 import java.util.Hashtable JavaDoc;
7 import java.util.Iterator JavaDoc;
8
9 import javax.ejb.CreateException JavaDoc;
10 import javax.ejb.EJBException JavaDoc;
11 import javax.ejb.SessionBean JavaDoc;
12 import javax.ejb.SessionContext JavaDoc;
13 import javax.ejb.SessionSynchronization JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.naming.NamingException JavaDoc;
16 import javax.rmi.PortableRemoteObject JavaDoc;
17 import javax.sql.DataSource JavaDoc;
18
19 import org.objectweb.jonas.common.Log;
20 import org.objectweb.util.monolog.api.BasicLevel;
21 import org.objectweb.util.monolog.api.Logger;
22
23
24 /**
25  * Stateful Session Bean that manages directly jdbc connections
26  */

27 public class ManagerSY implements SessionBean JavaDoc, SessionSynchronization JavaDoc {
28
29     static private Logger logger = null;
30     SessionContext JavaDoc ejbContext;
31
32     // ------------------------------------------------------------------
33
// The state of this Stateful Session Bean
34
// ------------------------------------------------------------------
35
int conb = 100;
36     InitialContext JavaDoc ictx = null;
37     DataSource JavaDoc ds = null;
38     Hashtable JavaDoc clist = new Hashtable JavaDoc(); // Connection List
39

40     // ------------------------------------------------------------------
41
// SessionSynchronization implementation
42
// ------------------------------------------------------------------
43

44     public void afterBegin() {
45         logger.log(BasicLevel.DEBUG, "");
46     }
47
48     public void beforeCompletion() {
49         logger.log(BasicLevel.DEBUG, "");
50     }
51
52     public void afterCompletion(boolean committed) {
53         logger.log(BasicLevel.DEBUG, "");
54     }
55
56     // ------------------------------------------------------------------
57
// SessionBean implementation
58
// ------------------------------------------------------------------
59

60     /**
61      * Make here all initialisations needed in this stateful session bean
62      */

63     public void setSessionContext(SessionContext JavaDoc ctx) {
64         if( logger == null)
65             logger = Log.getLogger("org.objectweb.jonas_tests");
66         logger.log(BasicLevel.DEBUG, "");
67         ejbContext = ctx;
68         ds = getDataSource("java:comp/env/jdbc/mydb");
69         // Get a Connection now
70
try {
71             Connection JavaDoc c = ds.getConnection();
72             clist.put(new Integer JavaDoc(0), c);
73         } catch (SQLException JavaDoc e) {
74             throw new EJBException JavaDoc("Cannot get first Connection:"+e);
75         }
76     }
77     
78     public void ejbRemove() {
79         logger.log(BasicLevel.DEBUG, "");
80         // Close all opened connections
81
for (Iterator JavaDoc i = clist.values().iterator(); i.hasNext(); ) {
82             Connection JavaDoc c = (Connection JavaDoc) i.next();
83             try {
84                 c.close();
85             } catch (SQLException JavaDoc e) {
86                 logger.log(BasicLevel.ERROR, "Cannot close Connection:" + e);
87             }
88         }
89     }
90     
91     public void ejbCreate() throws CreateException JavaDoc {
92         logger.log(BasicLevel.DEBUG, "");
93     }
94
95     public void ejbPassivate() {
96         logger.log(BasicLevel.DEBUG, "");
97     }
98
99     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 JavaDoc {
111         logger.log(BasicLevel.DEBUG, "");
112         Connection JavaDoc c = null;
113         try {
114             c = ds.getConnection();
115         } catch (SQLException JavaDoc e) {
116             logger.log(BasicLevel.ERROR, "Cannot get a Connection:"+e);
117             return false;
118         }
119         try {
120             c.close();
121         } catch (SQLException JavaDoc e) {
122             logger.log(BasicLevel.ERROR, "Cannot close Connection:"+e);
123             return false;
124         }
125         return true;
126     }
127
128     /**
129      * return the number associated to the connection opened,
130      * or 0 if it failed.
131      */

132     public int openConnection() throws RemoteException JavaDoc {
133         logger.log(BasicLevel.DEBUG, "");
134         Connection JavaDoc c = null;
135         try {
136             c = ds.getConnection();
137         } catch (SQLException JavaDoc e) {
138             logger.log(BasicLevel.ERROR, "Cannot get a Connection:"+e);
139             return 0;
140         }
141         // choose a connection number
142
conb++;
143         clist.put(new Integer JavaDoc(conb), c);
144         logger.log(BasicLevel.DEBUG, "connection number="+conb);
145         return conb;
146     }
147
148     /**
149      * Return true if close Connection worked
150      */

151     public boolean closeConnection(int nb) throws RemoteException JavaDoc {
152         logger.log(BasicLevel.DEBUG, "connection number="+conb);
153         Connection JavaDoc c = (Connection JavaDoc) clist.remove(new Integer JavaDoc(nb));
154         if (c == null) {
155             logger.log(BasicLevel.ERROR, "Unknown Connection");
156             return false;
157         }
158         try {
159             c.close();
160         } catch (SQLException JavaDoc e) {
161             logger.log(BasicLevel.ERROR, "Cannot close Connection:"+e);
162             return false;
163         }
164         boolean isclosed = false;
165         try {
166             isclosed = c.isClosed();
167         } catch (SQLException JavaDoc e) {
168             logger.log(BasicLevel.ERROR, "Cannot check if closed:"+e);
169             return false;
170         }
171         return isclosed;
172     }
173
174     // ------------------------------------------------------------------
175
// private methods
176
// ------------------------------------------------------------------
177

178     /*
179      * get the DataSource given its name in JNDI
180      */

181     private DataSource JavaDoc getDataSource(String JavaDoc db) {
182
183         // lookup the DataSource in the initial context
184
DataSource JavaDoc ds = null;
185         try {
186             // get initial context
187
if (ictx == null) {
188                 ictx = new InitialContext JavaDoc();
189             }
190             ds = (DataSource JavaDoc) PortableRemoteObject.narrow(ictx.lookup(db), DataSource JavaDoc.class);
191         } catch (NamingException JavaDoc e) {
192             logger.log(BasicLevel.ERROR, "Cannot lookup datasource "+db+": "+e);
193             throw new EJBException JavaDoc("Cannot access DataSource");
194         }
195
196         // return it
197
return ds;
198     }
199
200 }
201
Popular Tags