1 4 package org.objectweb.jonas.stests.manyops; 5 6 import java.sql.Connection ; 7 import java.sql.SQLException ; 8 import java.sql.Statement ; 9 10 import javax.ejb.CreateException ; 11 import javax.ejb.EJBException ; 12 import javax.ejb.SessionBean ; 13 import javax.ejb.SessionContext ; 14 import javax.naming.InitialContext ; 15 import javax.sql.DataSource ; 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 25 public class InitDBSL implements SessionBean { 26 27 static private Logger logger = null; 28 SessionContext ejbContext; 29 30 34 45 public void setSessionContext(SessionContext ctx) { 46 if( logger == null) 47 logger = Log.getLogger("org.objectweb.jonas_tests"); 48 logger.log(BasicLevel.DEBUG, ""); 49 ejbContext = ctx; 50 } 51 52 61 public void ejbRemove() { 62 logger.log(BasicLevel.DEBUG, ""); 63 } 64 65 70 public void ejbCreate() throws CreateException { 71 logger.log(BasicLevel.DEBUG, ""); 72 } 73 74 75 79 public void ejbPassivate() { 80 logger.log(BasicLevel.DEBUG, ""); 81 } 82 83 88 public void ejbActivate() { 89 logger.log(BasicLevel.DEBUG, ""); 90 } 91 92 96 99 public void createTable() { 100 logger.log(BasicLevel.DEBUG, ""); 101 initDB("stestsmanyops"); 102 } 103 104 void initDB(String tableName) { 105 logger.log(BasicLevel.DEBUG, ""); 106 107 DataSource ds = null; 109 InitialContext ictx = null; 110 try { 111 ictx = new InitialContext (); 112 } catch (Exception e) { 113 logger.log(BasicLevel.ERROR, "new InitialContext() : " + e); 114 throw new EJBException ("Cannot get JNDI InitialContext"); 115 } 116 try { 117 ds = (DataSource ) ictx.lookup("java:comp/env/jdbc/mydb"); 118 } catch (Exception e) { 119 logger.log(BasicLevel.ERROR, "cannot lookup datasource " + e); 120 throw new EJBException ("cannot lookup datasource"); 121 } 122 123 Connection conn = null; 125 Statement stmt = null; 126 try { 127 conn = ds.getConnection(); 128 stmt = conn.createStatement(); 129 stmt.execute("drop table " + tableName); 130 stmt.close(); 131 } catch(SQLException e) { 132 logger.log(BasicLevel.INFO, "Exception in dropTable : "+e); 134 } 135 136 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 e) { 143 logger.log(BasicLevel.ERROR, "Exception in createTable : "+e); 144 throw new EJBException ("Exception in createTable"); 145 } 146 } 147 148 } 149 | Popular Tags |