KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > bmt > MosconeST


1 // MosconeST.java
2
// Stateful Session Bean
3

4 package org.objectweb.jonas.jtests.beans.bmt;
5
6 import java.rmi.RemoteException JavaDoc;
7 import java.sql.Connection JavaDoc;
8 import java.sql.ResultSet JavaDoc;
9 import java.sql.SQLException JavaDoc;
10 import java.sql.Statement JavaDoc;
11 import java.sql.SQLException JavaDoc;
12
13 import javax.ejb.CreateException JavaDoc;
14 import javax.ejb.EJBException JavaDoc;
15 import javax.ejb.SessionBean JavaDoc;
16 import javax.ejb.SessionContext JavaDoc;
17 import javax.naming.Context JavaDoc;
18 import javax.naming.InitialContext JavaDoc;
19 import javax.naming.NamingException JavaDoc;
20 import javax.rmi.PortableRemoteObject JavaDoc;
21 import javax.sql.DataSource JavaDoc;
22 import javax.transaction.NotSupportedException JavaDoc;
23 import javax.transaction.SystemException JavaDoc;
24 import javax.transaction.UserTransaction JavaDoc;
25
26 import org.objectweb.jonas.common.Log;
27 import org.objectweb.util.monolog.api.BasicLevel;
28 import org.objectweb.util.monolog.api.Logger;
29
30
31 /**
32  * Stateful Session bean that manages transactions inside the bean.
33  * This type of bean must NOT implement SessionSynchronization.
34  */

35 public class MosconeST implements SessionBean JavaDoc {
36
37     static private Logger logger = null;
38     SessionContext JavaDoc ejbContext;
39
40     // state of the sessionbean
41
Context JavaDoc ictx = null;
42     UserTransaction JavaDoc ut = null;
43     Connection JavaDoc cnx = null;
44     ResultSet JavaDoc rs = null;
45     Statement JavaDoc st = null;
46
47     // ------------------------------------------------------------------
48
// SessionBean implementation
49
// ------------------------------------------------------------------
50

51     /**
52      * Set the associated session context. The container calls this method
53      * after the instance creation.
54      * The enterprise Bean instance should store the reference to the context
55      * object in an instance variable.
56      * This method is called with no transaction context.
57      *
58      * @param sessionContext A SessionContext interface for the instance.
59      * @throws EJBException Thrown by the method to indicate a failure caused by
60      * a system-level error.
61      */

62     public void setSessionContext(SessionContext JavaDoc ctx) {
63         if( logger == null)
64             logger = Log.getLogger("org.objectweb.jonas_tests");
65         logger.log(BasicLevel.DEBUG, "");
66         ejbContext = ctx;
67     }
68     
69     /**
70      * A container invokes this method before it ends the life of the session object.
71      * This happens as a result of a client's invoking a remove operation, or when a
72      * container decides to terminate the session object after a timeout.
73      * This method is called with no transaction context.
74      *
75      * @throws EJBException Thrown by the method to indicate a failure caused by
76      * a system-level error.
77      */

78     public void ejbRemove() {
79         logger.log(BasicLevel.DEBUG, "");
80     }
81     
82     private void getConnection() throws RemoteException JavaDoc {
83         try {
84             ictx = new InitialContext JavaDoc();
85             DataSource JavaDoc ds = (DataSource JavaDoc) PortableRemoteObject.narrow(ictx.lookup("jdbc_1"), DataSource JavaDoc.class);
86             cnx = ds.getConnection();
87         } catch (Exception JavaDoc e) {
88             throw new RemoteException JavaDoc("cannot get connection: " + e);
89         }
90     }
91
92     private void closeConnection() throws RemoteException JavaDoc {
93         try {
94             if (cnx != null) {
95                 cnx.close();
96             }
97         } catch (Exception JavaDoc e) {
98             throw new RemoteException JavaDoc("cannot close connection: " + e);
99         }
100     }
101
102     /**
103      * The Session bean must define 1 or more ejbCreate methods.
104      *
105      * @throws CreateException Failure to create a session EJB object.
106      */

107     public void ejbCreate() throws CreateException JavaDoc {
108         logger.log(BasicLevel.DEBUG, "");
109     }
110
111
112     /**
113      * A container invokes this method on an instance before the instance
114      * becomes disassociated with a specific EJB object.
115      */

116     public void ejbPassivate() {
117         logger.log(BasicLevel.DEBUG, "");
118     }
119
120     /**
121      * A container invokes this method when the instance is taken out of
122      * the pool of available instances to become associated with a specific
123      * EJB object.
124      */

125     public void ejbActivate() {
126         logger.log(BasicLevel.DEBUG, "");
127     }
128     
129     // ------------------------------------------------------------------
130
// Moscone implementation
131
// ------------------------------------------------------------------
132

133     /**
134      * The following method start a transaction that will be continued
135      * in other methods of this bean.
136      */

137     public void tx_start() throws RemoteException JavaDoc {
138         logger.log(BasicLevel.DEBUG, "");
139
140         // Obtain the UserTransaction interface
141
try {
142             ut = ejbContext.getUserTransaction();
143         } catch (IllegalStateException JavaDoc e) {
144             logger.log(BasicLevel.ERROR, "Can't get UserTransaction");
145             throw new RemoteException JavaDoc("Can't get UserTransaction:", e);
146         }
147
148         // Start a global transaction
149
try {
150             ut.begin();
151         } catch (NotSupportedException JavaDoc e) {
152             logger.log(BasicLevel.ERROR, "Can't start Transaction");
153             throw new RemoteException JavaDoc("Can't start Transaction:", e);
154         } catch (SystemException JavaDoc e) {
155             logger.log(BasicLevel.ERROR, "Can't start Transaction");
156             throw new RemoteException JavaDoc("Can't start Transaction:", e);
157         }
158     }
159
160     /**
161      * This method commits the current transaction, started previously by tx_start().
162      */

163     public void tx_commit() throws RemoteException JavaDoc {
164         logger.log(BasicLevel.DEBUG, "");
165
166         // Commit this GLOBAL transaction
167
try {
168             ut.commit();
169         } catch (Exception JavaDoc e) {
170             logger.log(BasicLevel.ERROR, "Can't commit Transaction");
171             throw new RemoteException JavaDoc("Can't commit Transaction:", e);
172         }
173     }
174
175     /**
176      * This method rolls back the current transaction, started previously by tx_start().
177      */

178     public void tx_rollback() throws RemoteException JavaDoc {
179         logger.log(BasicLevel.DEBUG, "");
180
181         // Roll back this GLOBAL transaction
182
try {
183             ut.rollback();
184         } catch (Exception JavaDoc e) {
185             logger.log(BasicLevel.ERROR, "Can't rollback Transaction");
186             throw new RemoteException JavaDoc("Can't rollback Transaction:", e);
187         }
188     }
189
190     /**
191      * This method open a connection before starting transactions.
192      */

193     public void moscone1() throws RemoteException JavaDoc {
194         logger.log(BasicLevel.DEBUG, "");
195
196         getConnection();
197
198         // Obtain the UserTransaction interface
199
try {
200             ut = ejbContext.getUserTransaction();
201         } catch (IllegalStateException JavaDoc e) {
202             logger.log(BasicLevel.ERROR, "Can't get UserTransaction");
203             throw new RemoteException JavaDoc("Can't get UserTransaction:", e);
204         }
205
206         // Start a global transaction
207
try {
208             ut.begin();
209         } catch (NotSupportedException JavaDoc e) {
210             logger.log(BasicLevel.ERROR, "Can't start Transaction");
211             throw new RemoteException JavaDoc("Can't start Transaction:", e);
212         } catch (SystemException JavaDoc e) {
213             logger.log(BasicLevel.ERROR, "Can't start Transaction");
214             throw new RemoteException JavaDoc("Can't start Transaction:", e);
215         }
216
217         // work with connection.
218
try {
219             String JavaDoc ret = null;
220             st = cnx.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
221             rs = st.executeQuery("SELECT * FROM JT2_MARCHE");
222             if (rs.first()) {
223                 do {
224                     ret += rs.getInt("IDMAR") + ":" + rs.getString("NOM") + "\n";
225                 } while(rs.next());
226             }
227             st.close();
228         } catch (SQLException JavaDoc e) {
229             throw new RemoteException JavaDoc("Error working on database: " + e);
230         }
231
232         // Commit this GLOBAL transaction
233
try {
234             ut.commit();
235         } catch (Exception JavaDoc e) {
236             logger.log(BasicLevel.ERROR, "Can't commit Transaction");
237             throw new RemoteException JavaDoc("Can't commit Transaction:", e);
238         }
239
240         closeConnection();
241     }
242     
243 }
244
Popular Tags