1 4 package org.objectweb.jonas.jtests.beans.bmt; 5 6 import java.rmi.RemoteException ; 7 import java.sql.Connection ; 8 import java.sql.ResultSet ; 9 import java.sql.SQLException ; 10 import java.sql.Statement ; 11 import java.sql.SQLException ; 12 13 import javax.ejb.CreateException ; 14 import javax.ejb.EJBException ; 15 import javax.ejb.SessionBean ; 16 import javax.ejb.SessionContext ; 17 import javax.naming.Context ; 18 import javax.naming.InitialContext ; 19 import javax.naming.NamingException ; 20 import javax.rmi.PortableRemoteObject ; 21 import javax.sql.DataSource ; 22 import javax.transaction.NotSupportedException ; 23 import javax.transaction.SystemException ; 24 import javax.transaction.UserTransaction ; 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 35 public class MosconeST implements SessionBean { 36 37 static private Logger logger = null; 38 SessionContext ejbContext; 39 40 Context ictx = null; 42 UserTransaction ut = null; 43 Connection cnx = null; 44 ResultSet rs = null; 45 Statement st = null; 46 47 51 62 public void setSessionContext(SessionContext ctx) { 63 if( logger == null) 64 logger = Log.getLogger("org.objectweb.jonas_tests"); 65 logger.log(BasicLevel.DEBUG, ""); 66 ejbContext = ctx; 67 } 68 69 78 public void ejbRemove() { 79 logger.log(BasicLevel.DEBUG, ""); 80 } 81 82 private void getConnection() throws RemoteException { 83 try { 84 ictx = new InitialContext (); 85 DataSource ds = (DataSource ) PortableRemoteObject.narrow(ictx.lookup("jdbc_1"), DataSource .class); 86 cnx = ds.getConnection(); 87 } catch (Exception e) { 88 throw new RemoteException ("cannot get connection: " + e); 89 } 90 } 91 92 private void closeConnection() throws RemoteException { 93 try { 94 if (cnx != null) { 95 cnx.close(); 96 } 97 } catch (Exception e) { 98 throw new RemoteException ("cannot close connection: " + e); 99 } 100 } 101 102 107 public void ejbCreate() throws CreateException { 108 logger.log(BasicLevel.DEBUG, ""); 109 } 110 111 112 116 public void ejbPassivate() { 117 logger.log(BasicLevel.DEBUG, ""); 118 } 119 120 125 public void ejbActivate() { 126 logger.log(BasicLevel.DEBUG, ""); 127 } 128 129 133 137 public void tx_start() throws RemoteException { 138 logger.log(BasicLevel.DEBUG, ""); 139 140 try { 142 ut = ejbContext.getUserTransaction(); 143 } catch (IllegalStateException e) { 144 logger.log(BasicLevel.ERROR, "Can't get UserTransaction"); 145 throw new RemoteException ("Can't get UserTransaction:", e); 146 } 147 148 try { 150 ut.begin(); 151 } catch (NotSupportedException e) { 152 logger.log(BasicLevel.ERROR, "Can't start Transaction"); 153 throw new RemoteException ("Can't start Transaction:", e); 154 } catch (SystemException e) { 155 logger.log(BasicLevel.ERROR, "Can't start Transaction"); 156 throw new RemoteException ("Can't start Transaction:", e); 157 } 158 } 159 160 163 public void tx_commit() throws RemoteException { 164 logger.log(BasicLevel.DEBUG, ""); 165 166 try { 168 ut.commit(); 169 } catch (Exception e) { 170 logger.log(BasicLevel.ERROR, "Can't commit Transaction"); 171 throw new RemoteException ("Can't commit Transaction:", e); 172 } 173 } 174 175 178 public void tx_rollback() throws RemoteException { 179 logger.log(BasicLevel.DEBUG, ""); 180 181 try { 183 ut.rollback(); 184 } catch (Exception e) { 185 logger.log(BasicLevel.ERROR, "Can't rollback Transaction"); 186 throw new RemoteException ("Can't rollback Transaction:", e); 187 } 188 } 189 190 193 public void moscone1() throws RemoteException { 194 logger.log(BasicLevel.DEBUG, ""); 195 196 getConnection(); 197 198 try { 200 ut = ejbContext.getUserTransaction(); 201 } catch (IllegalStateException e) { 202 logger.log(BasicLevel.ERROR, "Can't get UserTransaction"); 203 throw new RemoteException ("Can't get UserTransaction:", e); 204 } 205 206 try { 208 ut.begin(); 209 } catch (NotSupportedException e) { 210 logger.log(BasicLevel.ERROR, "Can't start Transaction"); 211 throw new RemoteException ("Can't start Transaction:", e); 212 } catch (SystemException e) { 213 logger.log(BasicLevel.ERROR, "Can't start Transaction"); 214 throw new RemoteException ("Can't start Transaction:", e); 215 } 216 217 try { 219 String 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 e) { 229 throw new RemoteException ("Error working on database: " + e); 230 } 231 232 try { 234 ut.commit(); 235 } catch (Exception e) { 236 logger.log(BasicLevel.ERROR, "Can't commit Transaction"); 237 throw new RemoteException ("Can't commit Transaction:", e); 238 } 239 240 closeConnection(); 241 } 242 243 } 244 | Popular Tags |