001: // MosconeST.java
002: // Stateful Session Bean
003:
004: package org.objectweb.jonas.jtests.beans.bmt;
005:
006: import java.rmi.RemoteException;
007: import java.sql.Connection;
008: import java.sql.ResultSet;
009: import java.sql.SQLException;
010: import java.sql.Statement;
011: import java.sql.SQLException;
012:
013: import javax.ejb.CreateException;
014: import javax.ejb.EJBException;
015: import javax.ejb.SessionBean;
016: import javax.ejb.SessionContext;
017: import javax.naming.Context;
018: import javax.naming.InitialContext;
019: import javax.naming.NamingException;
020: import javax.rmi.PortableRemoteObject;
021: import javax.sql.DataSource;
022: import javax.transaction.NotSupportedException;
023: import javax.transaction.SystemException;
024: import javax.transaction.UserTransaction;
025:
026: import org.objectweb.jonas.common.Log;
027: import org.objectweb.util.monolog.api.BasicLevel;
028: import org.objectweb.util.monolog.api.Logger;
029:
030: /**
031: * Stateful Session bean that manages transactions inside the bean.
032: * This type of bean must NOT implement SessionSynchronization.
033: */
034: public class MosconeST implements SessionBean {
035:
036: static private Logger logger = null;
037: SessionContext ejbContext;
038:
039: // state of the sessionbean
040: Context ictx = null;
041: UserTransaction ut = null;
042: Connection cnx = null;
043: ResultSet rs = null;
044: Statement st = null;
045:
046: // ------------------------------------------------------------------
047: // SessionBean implementation
048: // ------------------------------------------------------------------
049:
050: /**
051: * Set the associated session context. The container calls this method
052: * after the instance creation.
053: * The enterprise Bean instance should store the reference to the context
054: * object in an instance variable.
055: * This method is called with no transaction context.
056: *
057: * @param sessionContext A SessionContext interface for the instance.
058: * @throws EJBException Thrown by the method to indicate a failure caused by
059: * a system-level error.
060: */
061: public void setSessionContext(SessionContext ctx) {
062: if (logger == null)
063: logger = Log.getLogger("org.objectweb.jonas_tests");
064: logger.log(BasicLevel.DEBUG, "");
065: ejbContext = ctx;
066: }
067:
068: /**
069: * A container invokes this method before it ends the life of the session object.
070: * This happens as a result of a client's invoking a remove operation, or when a
071: * container decides to terminate the session object after a timeout.
072: * This method is called with no transaction context.
073: *
074: * @throws EJBException Thrown by the method to indicate a failure caused by
075: * a system-level error.
076: */
077: public void ejbRemove() {
078: logger.log(BasicLevel.DEBUG, "");
079: }
080:
081: private void getConnection() throws RemoteException {
082: try {
083: ictx = new InitialContext();
084: DataSource ds = (DataSource) PortableRemoteObject.narrow(
085: ictx.lookup("jdbc_1"), DataSource.class);
086: cnx = ds.getConnection();
087: } catch (Exception e) {
088: throw new RemoteException("cannot get connection: " + e);
089: }
090: }
091:
092: private void closeConnection() throws RemoteException {
093: try {
094: if (cnx != null) {
095: cnx.close();
096: }
097: } catch (Exception e) {
098: throw new RemoteException("cannot close connection: " + e);
099: }
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 {
108: logger.log(BasicLevel.DEBUG, "");
109: }
110:
111: /**
112: * A container invokes this method on an instance before the instance
113: * becomes disassociated with a specific EJB object.
114: */
115: public void ejbPassivate() {
116: logger.log(BasicLevel.DEBUG, "");
117: }
118:
119: /**
120: * A container invokes this method when the instance is taken out of
121: * the pool of available instances to become associated with a specific
122: * EJB object.
123: */
124: public void ejbActivate() {
125: logger.log(BasicLevel.DEBUG, "");
126: }
127:
128: // ------------------------------------------------------------------
129: // Moscone implementation
130: // ------------------------------------------------------------------
131:
132: /**
133: * The following method start a transaction that will be continued
134: * in other methods of this bean.
135: */
136: public void tx_start() throws RemoteException {
137: logger.log(BasicLevel.DEBUG, "");
138:
139: // Obtain the UserTransaction interface
140: try {
141: ut = ejbContext.getUserTransaction();
142: } catch (IllegalStateException e) {
143: logger.log(BasicLevel.ERROR, "Can't get UserTransaction");
144: throw new RemoteException("Can't get UserTransaction:", e);
145: }
146:
147: // Start a global transaction
148: try {
149: ut.begin();
150: } catch (NotSupportedException e) {
151: logger.log(BasicLevel.ERROR, "Can't start Transaction");
152: throw new RemoteException("Can't start Transaction:", e);
153: } catch (SystemException e) {
154: logger.log(BasicLevel.ERROR, "Can't start Transaction");
155: throw new RemoteException("Can't start Transaction:", e);
156: }
157: }
158:
159: /**
160: * This method commits the current transaction, started previously by tx_start().
161: */
162: public void tx_commit() throws RemoteException {
163: logger.log(BasicLevel.DEBUG, "");
164:
165: // Commit this GLOBAL transaction
166: try {
167: ut.commit();
168: } catch (Exception e) {
169: logger.log(BasicLevel.ERROR, "Can't commit Transaction");
170: throw new RemoteException("Can't commit Transaction:", e);
171: }
172: }
173:
174: /**
175: * This method rolls back the current transaction, started previously by tx_start().
176: */
177: public void tx_rollback() throws RemoteException {
178: logger.log(BasicLevel.DEBUG, "");
179:
180: // Roll back this GLOBAL transaction
181: try {
182: ut.rollback();
183: } catch (Exception e) {
184: logger.log(BasicLevel.ERROR, "Can't rollback Transaction");
185: throw new RemoteException("Can't rollback Transaction:", e);
186: }
187: }
188:
189: /**
190: * This method open a connection before starting transactions.
191: */
192: public void moscone1() throws RemoteException {
193: logger.log(BasicLevel.DEBUG, "");
194:
195: getConnection();
196:
197: // Obtain the UserTransaction interface
198: try {
199: ut = ejbContext.getUserTransaction();
200: } catch (IllegalStateException e) {
201: logger.log(BasicLevel.ERROR, "Can't get UserTransaction");
202: throw new RemoteException("Can't get UserTransaction:", e);
203: }
204:
205: // Start a global transaction
206: try {
207: ut.begin();
208: } catch (NotSupportedException e) {
209: logger.log(BasicLevel.ERROR, "Can't start Transaction");
210: throw new RemoteException("Can't start Transaction:", e);
211: } catch (SystemException e) {
212: logger.log(BasicLevel.ERROR, "Can't start Transaction");
213: throw new RemoteException("Can't start Transaction:", e);
214: }
215:
216: // work with connection.
217: try {
218: String ret = null;
219: st = cnx.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
220: ResultSet.CONCUR_READ_ONLY);
221: rs = st.executeQuery("SELECT * FROM JT2_MARCHE");
222: if (rs.first()) {
223: do {
224: ret += rs.getInt("IDMAR") + ":"
225: + rs.getString("NOM") + "\n";
226: } while (rs.next());
227: }
228: st.close();
229: } catch (SQLException e) {
230: throw new RemoteException("Error working on database: " + e);
231: }
232:
233: // Commit this GLOBAL transaction
234: try {
235: ut.commit();
236: } catch (Exception e) {
237: logger.log(BasicLevel.ERROR, "Can't commit Transaction");
238: throw new RemoteException("Can't commit Transaction:", e);
239: }
240:
241: closeConnection();
242: }
243:
244: }
|