KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > testbean > bean > BMTStatefulBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.testbean.bean;
23
24 import java.rmi.*;
25 import javax.ejb.*;
26 import javax.naming.InitialContext JavaDoc;
27
28 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
29
30 import javax.transaction.UserTransaction JavaDoc;
31 import javax.transaction.Status JavaDoc;
32 import java.sql.Connection JavaDoc;
33 import java.sql.Statement JavaDoc;
34 import java.sql.ResultSet JavaDoc;
35 import javax.sql.DataSource JavaDoc;
36 import java.sql.SQLException JavaDoc;
37
38 public class BMTStatefulBean implements SessionBean
39 {
40    static org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(BMTStatefulBean.class);
41
42    private SessionContext sessionContext;
43
44    public void ejbCreate() throws RemoteException, CreateException
45    {
46       log.debug("BMTStatefulBean.ejbCreate() called");
47    }
48
49
50    public void ejbCreate(String JavaDoc caca) throws RemoteException, CreateException
51    {
52    };
53     
54    public void ejbCreate(String JavaDoc caca, String JavaDoc cacaprout) throws RemoteException, CreateException
55    {
56    };
57     
58    public void ejbActivate() throws RemoteException
59    {
60       log.debug("BMTStatefulBean.ejbActivate() called");
61    }
62
63    public void ejbPassivate() throws RemoteException
64    {
65       log.debug("BMTStatefulBean.ejbPassivate() called");
66    }
67
68    public void ejbRemove() throws RemoteException
69    {
70       log.debug("BMTStatefulBean.ejbRemove() called");
71    }
72
73    public void setSessionContext(SessionContext context) throws RemoteException
74    {
75       sessionContext = context;
76    }
77
78    public String JavaDoc txExists() throws RemoteException
79    {
80       String JavaDoc result = "";
81       try
82       {
83          UserTransaction JavaDoc ut1 = sessionContext.getUserTransaction();
84          result += "Got UserTransaction via sessionContext.getUserTransaction(): " + statusName(ut1.getStatus()) + "\n";
85
86          UserTransaction JavaDoc ut2 = (UserTransaction JavaDoc) new InitialContext JavaDoc().lookup("java:comp/UserTransaction");
87          result += "Got UserTransaction via lookup(java:comp/UserTransaction): " + statusName(ut2.getStatus()) + "\n";
88
89          return result;
90       }
91       catch(Exception JavaDoc e)
92       {
93          log.debug("failed", e);
94          throw new RemoteException(e.getMessage());
95       }
96    }
97
98
99    public String JavaDoc txCommit() throws RemoteException
100    {
101       try
102       {
103          UserTransaction JavaDoc tx = sessionContext.getUserTransaction();
104
105          String JavaDoc result = "Got transaction : " + statusName(tx.getStatus()) + "\n";
106          tx.begin();
107          result += "tx.begin(): " + statusName(tx.getStatus()) + "\n";
108          tx.commit();
109          result += "tx.commit(): " + statusName(tx.getStatus()) + "\n";
110
111          return result;
112
113       }
114       catch(Exception JavaDoc e)
115       {
116          log.debug("failed", e);
117          throw new RemoteException(e.getMessage());
118       }
119
120    }
121
122    public String JavaDoc txRollback() throws RemoteException
123    {
124       try
125       {
126          UserTransaction JavaDoc tx = sessionContext.getUserTransaction();
127
128          String JavaDoc result = "Got transaction : " + statusName(tx.getStatus()) + "\n";
129          tx.begin();
130          result += "tx.begin(): " + statusName(tx.getStatus()) + "\n";
131          tx.rollback();
132          result += "tx.rollback(): " + statusName(tx.getStatus()) + "\n";
133
134          return result;
135
136       }
137       catch(Exception JavaDoc e)
138       {
139          log.debug("failed", e);
140          throw new RemoteException(e.getMessage());
141       }
142    }
143
144
145    public String JavaDoc txBegin() throws RemoteException
146    {
147       try
148       {
149          UserTransaction JavaDoc tx = sessionContext.getUserTransaction();
150
151          tx.begin();
152          return "status: " + statusName(tx.getStatus());
153       }
154       catch(Exception JavaDoc e)
155       {
156          log.debug("failed", e);
157          throw new RemoteException(e.getMessage());
158       }
159
160    }
161
162    public String JavaDoc txEnd() throws RemoteException
163    {
164       try
165       {
166          UserTransaction JavaDoc tx = sessionContext.getUserTransaction();
167
168          tx.commit();
169          return "status: " + statusName(tx.getStatus());
170       }
171       catch(Exception JavaDoc e)
172       {
173          log.debug("failed", e);
174          throw new RemoteException(e.getMessage());
175       }
176
177    }
178
179    public void createTable() throws RemoteException
180    {
181       Connection JavaDoc connection = null;
182       Statement JavaDoc stm = null;
183       try
184       {
185          connection = ((DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:comp/env/jdbc/myDatabase")).getConnection();
186          stm = connection.createStatement();
187          try
188          {
189             stm.executeUpdate("CREATE TABLE bmttest (field VARCHAR(256))");
190          }
191          catch(SQLException JavaDoc e)
192          {
193             // ignore, table probably already exists
194
}
195          finally
196          {
197             JDBCUtil.safeClose(stm);
198          }
199
200          stm = connection.createStatement();
201          stm.executeUpdate("INSERT INTO bmttest VALUES ('initial value')");
202       }
203       catch(Exception JavaDoc e)
204       {
205          log.debug("failed", e);
206          throw new RemoteException(e.getMessage());
207       }
208       finally
209       {
210          JDBCUtil.safeClose(stm);
211          JDBCUtil.safeClose(connection);
212       }
213    }
214
215    public void dropTable() throws RemoteException
216    {
217       Connection JavaDoc connection = null;
218       Statement JavaDoc stm = null;
219       try
220       {
221          connection = ((DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:comp/env/jdbc/myDatabase")).getConnection();
222          stm = connection.createStatement();
223          stm.executeUpdate("DROP TABLE bmttest");
224       }
225       catch(Exception JavaDoc e)
226       {
227          log.debug("failed", e);
228          throw new RemoteException(e.getMessage());
229       }
230       finally
231       {
232          JDBCUtil.safeClose(stm);
233          JDBCUtil.safeClose(connection);
234       }
235    }
236
237
238    public String JavaDoc dbCommit() throws RemoteException
239    {
240       try
241       {
242          UserTransaction JavaDoc tx = sessionContext.getUserTransaction();
243
244          tx.begin();
245
246          Connection JavaDoc connection = null;
247          Statement JavaDoc stm = null;
248          try
249          {
250             connection = ((DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:comp/env/jdbc/myDatabase")).getConnection();
251             stm = connection.createStatement();
252             stm.executeUpdate("UPDATE bmttest SET field = 'updated via dbCommit'");
253          }
254          finally
255          {
256             JDBCUtil.safeClose(stm);
257             JDBCUtil.safeClose(connection);
258          }
259
260          tx.commit();
261          return statusName(tx.getStatus());
262
263       }
264       catch(Exception JavaDoc e)
265       {
266          log.debug("failed", e);
267          throw new RemoteException(e.getMessage());
268       }
269    }
270
271
272    public String JavaDoc dbRollback() throws RemoteException
273    {
274       try
275       {
276          UserTransaction JavaDoc tx = sessionContext.getUserTransaction();
277
278          tx.begin();
279
280          Connection JavaDoc connection = null;
281          Statement JavaDoc stm = null;
282          try
283          {
284             connection = ((DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:comp/env/jdbc/myDatabase")).getConnection();
285             stm = connection.createStatement();
286             stm.executeUpdate("UPDATE bmttest SET field = 'updated via dbRollback'");
287          }
288          finally
289          {
290             JDBCUtil.safeClose(stm);
291             JDBCUtil.safeClose(connection);
292          }
293
294          tx.rollback();
295          return statusName(tx.getStatus());
296
297       }
298       catch(Exception JavaDoc e)
299       {
300          log.debug("failed", e);
301          throw new RemoteException(e.getMessage());
302       }
303    }
304
305
306    public String JavaDoc getDbField() throws RemoteException
307    {
308       Connection JavaDoc connection = null;
309       Statement JavaDoc stm = null;
310       ResultSet JavaDoc rs = null;
311       try
312       {
313          connection = ((DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:comp/env/jdbc/myDatabase")).getConnection();
314          stm = connection.createStatement();
315          rs = stm.executeQuery("SELECT field FROM bmttest ");
316          String JavaDoc result = "not found";
317          if(rs.next())
318          {
319             result = rs.getString(1);
320          }
321          return result;
322       }
323       catch(Exception JavaDoc e)
324       {
325          log.debug("failed", e);
326          throw new RemoteException(e.getMessage());
327       }
328       finally
329       {
330          JDBCUtil.safeClose(rs);
331          JDBCUtil.safeClose(stm);
332          JDBCUtil.safeClose(connection);
333       }
334    }
335
336
337    private String JavaDoc statusName(int s)
338    {
339       switch(s)
340       {
341          case Status.STATUS_ACTIVE:
342             return "STATUS_ACTIVE";
343          case Status.STATUS_COMMITTED:
344             return "STATUS_COMMITED";
345          case Status.STATUS_COMMITTING:
346             return "STATUS_COMMITTING";
347          case Status.STATUS_MARKED_ROLLBACK:
348             return "STATUS_MARKED_ROLLBACK";
349          case Status.STATUS_NO_TRANSACTION:
350             return "STATUS_NO_TRANSACTION";
351          case Status.STATUS_PREPARED:
352             return "STATUS_PREPARED";
353          case Status.STATUS_PREPARING:
354             return "STATUS_PREPARING";
355          case Status.STATUS_ROLLEDBACK:
356             return "STATUS_ROLLEDBACK";
357          case Status.STATUS_ROLLING_BACK:
358             return "STATUS_ROLLING_BACK";
359          case Status.STATUS_UNKNOWN:
360             return "STATUS_UNKNOWN";
361       }
362       return "REALLY_UNKNOWN";
363    }
364 }
365
Popular Tags