KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > stests > manyops > InitDBSL


1 // InitDBSL.java
2
// Stateless Session bean
3

4 package org.objectweb.jonas.stests.manyops;
5
6 import java.sql.Connection JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import java.sql.Statement JavaDoc;
9
10 import javax.ejb.CreateException JavaDoc;
11 import javax.ejb.EJBException JavaDoc;
12 import javax.ejb.SessionBean JavaDoc;
13 import javax.ejb.SessionContext JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.sql.DataSource JavaDoc;
16
17 import org.objectweb.jonas.common.Log;
18 import org.objectweb.util.monolog.api.BasicLevel;
19 import org.objectweb.util.monolog.api.Logger;
20
21
22 /**
23  *
24  */

25 public class InitDBSL implements SessionBean JavaDoc {
26
27     static private Logger logger = null;
28     SessionContext JavaDoc ejbContext;
29
30     // ------------------------------------------------------------------
31
// SessionBean implementation
32
// ------------------------------------------------------------------
33

34     /**
35      * Set the associated session context. The container calls this method
36      * after the instance creation.
37      * The enterprise Bean instance should store the reference to the context
38      * object in an instance variable.
39      * This method is called with no transaction context.
40      *
41      * @param sessionContext A SessionContext interface for the instance.
42      * @throws EJBException Thrown by the method to indicate a failure caused by
43      * a system-level error.
44      */

45     public void setSessionContext(SessionContext JavaDoc ctx) {
46         if( logger == null)
47                 logger = Log.getLogger("org.objectweb.jonas_tests");
48     logger.log(BasicLevel.DEBUG, "");
49     ejbContext = ctx;
50     }
51     
52     /**
53      * A container invokes this method before it ends the life of the session object.
54      * This happens as a result of a client's invoking a remove operation, or when a
55      * container decides to terminate the session object after a timeout.
56      * This method is called with no transaction context.
57      *
58      * @throws EJBException Thrown by the method to indicate a failure caused by
59      * a system-level error.
60      */

61     public void ejbRemove() {
62     logger.log(BasicLevel.DEBUG, "");
63     }
64     
65     /**
66      * The Session bean must define 1 or more ejbCreate methods.
67      *
68      * @throws CreateException Failure to create a session EJB object.
69      */

70     public void ejbCreate() throws CreateException JavaDoc {
71     logger.log(BasicLevel.DEBUG, "");
72     }
73
74
75     /**
76      * A container invokes this method on an instance before the instance
77      * becomes disassociated with a specific EJB object.
78      */

79     public void ejbPassivate() {
80     logger.log(BasicLevel.DEBUG, "");
81     }
82
83     /**
84      * A container invokes this method when the instance is taken out of
85      * the pool of available instances to become associated with a specific
86      * EJB object.
87      */

88     public void ejbActivate() {
89     logger.log(BasicLevel.DEBUG, "");
90     }
91     
92     // ------------------------------------------------------------------
93
// InitDB implementation
94
// ------------------------------------------------------------------
95

96     /**
97      * createTable
98      */

99     public void createTable() {
100     logger.log(BasicLevel.DEBUG, "");
101     initDB("stestsmanyops");
102     }
103
104     void initDB(String JavaDoc tableName) {
105     logger.log(BasicLevel.DEBUG, "");
106
107     // Get my DataSource from JNDI
108
DataSource JavaDoc ds = null;
109     InitialContext JavaDoc ictx = null;
110     try {
111         ictx = new InitialContext JavaDoc();
112     } catch (Exception JavaDoc e) {
113         logger.log(BasicLevel.ERROR, "new InitialContext() : " + e);
114         throw new EJBException JavaDoc("Cannot get JNDI InitialContext");
115     }
116     try {
117         ds = (DataSource JavaDoc) ictx.lookup("java:comp/env/jdbc/mydb");
118     } catch (Exception JavaDoc e) {
119         logger.log(BasicLevel.ERROR, "cannot lookup datasource " + e);
120         throw new EJBException JavaDoc("cannot lookup datasource");
121     }
122
123     // Drop table
124
Connection JavaDoc conn = null;
125     Statement JavaDoc stmt = null;
126     try {
127         conn = ds.getConnection();
128         stmt = conn.createStatement();
129         stmt.execute("drop table " + tableName);
130         stmt.close();
131     } catch(SQLException JavaDoc e) {
132         // The first time, table will not exist.
133
logger.log(BasicLevel.INFO, "Exception in dropTable : "+e);
134     }
135
136     // Create table.
137
try {
138         stmt = conn.createStatement();
139         stmt.execute("create table " + tableName + "(name varchar(30) not null primary key, number integer, price integer)");
140         stmt.close();
141         conn.close();
142     } catch(SQLException JavaDoc e) {
143         logger.log(BasicLevel.ERROR, "Exception in createTable : "+e);
144         throw new EJBException JavaDoc("Exception in createTable");
145     }
146     }
147
148 }
149
Popular Tags