1 package org.objectweb.jonas.jtests.beans.jdbc; 2 3 import java.rmi.RemoteException ; 4 import java.sql.Connection ; 5 import java.sql.SQLException ; 6 import java.util.Hashtable ; 7 import java.util.Iterator ; 8 9 import javax.ejb.CreateException ; 10 import javax.ejb.EJBException ; 11 import javax.ejb.SessionBean ; 12 import javax.ejb.SessionContext ; 13 import javax.ejb.SessionSynchronization ; 14 import javax.naming.InitialContext ; 15 import javax.naming.NamingException ; 16 import javax.rmi.PortableRemoteObject ; 17 import javax.sql.DataSource ; 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 27 public class ManagerSY implements SessionBean , SessionSynchronization { 28 29 static private Logger logger = null; 30 SessionContext ejbContext; 31 32 int conb = 100; 36 InitialContext ictx = null; 37 DataSource ds = null; 38 Hashtable clist = new Hashtable (); 40 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 60 63 public void setSessionContext(SessionContext 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 try { 71 Connection c = ds.getConnection(); 72 clist.put(new Integer (0), c); 73 } catch (SQLException e) { 74 throw new EJBException ("Cannot get first Connection:"+e); 75 } 76 } 77 78 public void ejbRemove() { 79 logger.log(BasicLevel.DEBUG, ""); 80 for (Iterator i = clist.values().iterator(); i.hasNext(); ) { 82 Connection c = (Connection ) i.next(); 83 try { 84 c.close(); 85 } catch (SQLException e) { 86 logger.log(BasicLevel.ERROR, "Cannot close Connection:" + e); 87 } 88 } 89 } 90 91 public void ejbCreate() throws CreateException { 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 107 110 public boolean openCloseConnection() throws RemoteException { 111 logger.log(BasicLevel.DEBUG, ""); 112 Connection c = null; 113 try { 114 c = ds.getConnection(); 115 } catch (SQLException e) { 116 logger.log(BasicLevel.ERROR, "Cannot get a Connection:"+e); 117 return false; 118 } 119 try { 120 c.close(); 121 } catch (SQLException e) { 122 logger.log(BasicLevel.ERROR, "Cannot close Connection:"+e); 123 return false; 124 } 125 return true; 126 } 127 128 132 public int openConnection() throws RemoteException { 133 logger.log(BasicLevel.DEBUG, ""); 134 Connection c = null; 135 try { 136 c = ds.getConnection(); 137 } catch (SQLException e) { 138 logger.log(BasicLevel.ERROR, "Cannot get a Connection:"+e); 139 return 0; 140 } 141 conb++; 143 clist.put(new Integer (conb), c); 144 logger.log(BasicLevel.DEBUG, "connection number="+conb); 145 return conb; 146 } 147 148 151 public boolean closeConnection(int nb) throws RemoteException { 152 logger.log(BasicLevel.DEBUG, "connection number="+conb); 153 Connection c = (Connection ) clist.remove(new Integer (nb)); 154 if (c == null) { 155 logger.log(BasicLevel.ERROR, "Unknown Connection"); 156 return false; 157 } 158 try { 159 c.close(); 160 } catch (SQLException 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 e) { 168 logger.log(BasicLevel.ERROR, "Cannot check if closed:"+e); 169 return false; 170 } 171 return isclosed; 172 } 173 174 178 181 private DataSource getDataSource(String db) { 182 183 DataSource ds = null; 185 try { 186 if (ictx == null) { 188 ictx = new InitialContext (); 189 } 190 ds = (DataSource ) PortableRemoteObject.narrow(ictx.lookup(db), DataSource .class); 191 } catch (NamingException e) { 192 logger.log(BasicLevel.ERROR, "Cannot lookup datasource "+db+": "+e); 193 throw new EJBException ("Cannot access DataSource"); 194 } 195 196 return ds; 198 } 199 200 } 201 | Popular Tags |