001: package org.objectweb.jonas.jtests.beans.jdbc;
002:
003: import java.rmi.RemoteException;
004: import java.sql.Connection;
005: import java.sql.SQLException;
006: import java.util.Hashtable;
007: import java.util.Iterator;
008:
009: import javax.ejb.CreateException;
010: import javax.ejb.EJBException;
011: import javax.ejb.SessionBean;
012: import javax.ejb.SessionContext;
013: import javax.ejb.SessionSynchronization;
014: import javax.naming.InitialContext;
015: import javax.naming.NamingException;
016: import javax.rmi.PortableRemoteObject;
017: import javax.sql.DataSource;
018:
019: import org.objectweb.jonas.common.Log;
020: import org.objectweb.util.monolog.api.BasicLevel;
021: import org.objectweb.util.monolog.api.Logger;
022:
023: /**
024: * Stateful Session Bean that manages directly jdbc connections
025: */
026: public class ManagerSY implements SessionBean, SessionSynchronization {
027:
028: static private Logger logger = null;
029: SessionContext ejbContext;
030:
031: // ------------------------------------------------------------------
032: // The state of this Stateful Session Bean
033: // ------------------------------------------------------------------
034: int conb = 100;
035: InitialContext ictx = null;
036: DataSource ds = null;
037: Hashtable clist = new Hashtable(); // Connection List
038:
039: // ------------------------------------------------------------------
040: // SessionSynchronization implementation
041: // ------------------------------------------------------------------
042:
043: public void afterBegin() {
044: logger.log(BasicLevel.DEBUG, "");
045: }
046:
047: public void beforeCompletion() {
048: logger.log(BasicLevel.DEBUG, "");
049: }
050:
051: public void afterCompletion(boolean committed) {
052: logger.log(BasicLevel.DEBUG, "");
053: }
054:
055: // ------------------------------------------------------------------
056: // SessionBean implementation
057: // ------------------------------------------------------------------
058:
059: /**
060: * Make here all initialisations needed in this stateful session bean
061: */
062: public void setSessionContext(SessionContext ctx) {
063: if (logger == null)
064: logger = Log.getLogger("org.objectweb.jonas_tests");
065: logger.log(BasicLevel.DEBUG, "");
066: ejbContext = ctx;
067: ds = getDataSource("java:comp/env/jdbc/mydb");
068: // Get a Connection now
069: try {
070: Connection c = ds.getConnection();
071: clist.put(new Integer(0), c);
072: } catch (SQLException e) {
073: throw new EJBException("Cannot get first Connection:" + e);
074: }
075: }
076:
077: public void ejbRemove() {
078: logger.log(BasicLevel.DEBUG, "");
079: // Close all opened connections
080: for (Iterator i = clist.values().iterator(); i.hasNext();) {
081: Connection c = (Connection) i.next();
082: try {
083: c.close();
084: } catch (SQLException e) {
085: logger.log(BasicLevel.ERROR, "Cannot close Connection:"
086: + e);
087: }
088: }
089: }
090:
091: public void ejbCreate() throws CreateException {
092: logger.log(BasicLevel.DEBUG, "");
093: }
094:
095: public void ejbPassivate() {
096: logger.log(BasicLevel.DEBUG, "");
097: }
098:
099: public void ejbActivate() {
100: logger.log(BasicLevel.DEBUG, "");
101: }
102:
103: // ------------------------------------------------------------------
104: // Manager implementation
105: // ------------------------------------------------------------------
106:
107: /**
108: * open and close a Connection
109: */
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
117: .log(BasicLevel.ERROR, "Cannot get a Connection:"
118: + e);
119: return false;
120: }
121: try {
122: c.close();
123: } catch (SQLException e) {
124: logger
125: .log(BasicLevel.ERROR, "Cannot close Connection:"
126: + e);
127: return false;
128: }
129: return true;
130: }
131:
132: /**
133: * return the number associated to the connection opened,
134: * or 0 if it failed.
135: */
136: public int openConnection() throws RemoteException {
137: logger.log(BasicLevel.DEBUG, "");
138: Connection c = null;
139: try {
140: c = ds.getConnection();
141: } catch (SQLException e) {
142: logger
143: .log(BasicLevel.ERROR, "Cannot get a Connection:"
144: + e);
145: return 0;
146: }
147: // choose a connection number
148: conb++;
149: clist.put(new Integer(conb), c);
150: logger.log(BasicLevel.DEBUG, "connection number=" + conb);
151: return conb;
152: }
153:
154: /**
155: * Return true if close Connection worked
156: */
157: public boolean closeConnection(int nb) throws RemoteException {
158: logger.log(BasicLevel.DEBUG, "connection number=" + conb);
159: Connection c = (Connection) clist.remove(new Integer(nb));
160: if (c == null) {
161: logger.log(BasicLevel.ERROR, "Unknown Connection");
162: return false;
163: }
164: try {
165: c.close();
166: } catch (SQLException e) {
167: logger
168: .log(BasicLevel.ERROR, "Cannot close Connection:"
169: + e);
170: return false;
171: }
172: boolean isclosed = false;
173: try {
174: isclosed = c.isClosed();
175: } catch (SQLException e) {
176: logger.log(BasicLevel.ERROR, "Cannot check if closed:" + e);
177: return false;
178: }
179: return isclosed;
180: }
181:
182: // ------------------------------------------------------------------
183: // private methods
184: // ------------------------------------------------------------------
185:
186: /*
187: * get the DataSource given its name in JNDI
188: */
189: private DataSource getDataSource(String db) {
190:
191: // lookup the DataSource in the initial context
192: DataSource ds = null;
193: try {
194: // get initial context
195: if (ictx == null) {
196: ictx = new InitialContext();
197: }
198: ds = (DataSource) PortableRemoteObject.narrow(ictx
199: .lookup(db), DataSource.class);
200: } catch (NamingException e) {
201: logger.log(BasicLevel.ERROR, "Cannot lookup datasource "
202: + db + ": " + e);
203: throw new EJBException("Cannot access DataSource");
204: }
205:
206: // return it
207: return ds;
208: }
209:
210: }
|