001: package com.mockrunner.example.ejb;
002:
003: import java.rmi.RemoteException;
004: import java.sql.Connection;
005: import java.sql.PreparedStatement;
006: import java.sql.SQLException;
007: import java.sql.Statement;
008: import java.sql.Timestamp;
009:
010: import javax.ejb.CreateException;
011: import javax.ejb.EJBException;
012: import javax.ejb.SessionBean;
013: import javax.ejb.SessionContext;
014: import javax.naming.InitialContext;
015: import javax.sql.DataSource;
016:
017: /*
018: * @ejb:bean name="LogSession"
019: * display-name="LogSessionBean"
020: * type="Stateless"
021: * transaction-type="Container"
022: * jndi-name="com/mockrunner/example/LogSession"
023: *
024: * @ejb:resource-ref res-ref-name="jdbc/MySQLDB"
025: * res-type="javax.sql.DataSource"
026: * res-auth="Container"
027: * res-sharing-scope="Shareable"
028: *
029: * @jboss:resource-manager res-man-name="jdbc/MySQLDB"
030: * res-man-jndi-name="java:/MySQLDB"
031: */
032: /**
033: * This simple example EJB can be used to write
034: * log messages to a database.
035: */
036: public class LogSessionBean implements SessionBean {
037: private SessionContext sessionContext;
038:
039: /*
040: * @ejb:interface-method
041: * @ejb:transaction type="Required"
042: */
043: /**
044: * Creates the database <i>logtable</i>
045: */
046: public void createLogTable() {
047: Connection connection = null;
048: Statement statement = null;
049: try {
050: InitialContext context = new InitialContext();
051: DataSource dataSource = (DataSource) context
052: .lookup("java:comp/env/jdbc/MySQLDB");
053: connection = dataSource.getConnection();
054: statement = connection.createStatement();
055: statement.execute("create table logtable("
056: + "logtime timestamp not null,"
057: + "message char(255) not null)");
058: } catch (Exception exc) {
059: sessionContext.setRollbackOnly();
060: throw new EJBException(exc.getMessage());
061: } finally {
062: try {
063: if (null != statement)
064: statement.close();
065: if (null != connection)
066: connection.close();
067: } catch (SQLException sqlExc) {
068:
069: }
070: }
071: }
072:
073: /*
074: * @ejb:interface-method
075: * @ejb:transaction type="Required"
076: */
077: /**
078: * Writes the specified message into the <i>logtable</i>
079: * along with the timestamp and the current thread name.
080: * @param message the message
081: */
082: public void logMessage(String message) {
083: Connection connection = null;
084: PreparedStatement statement = null;
085: try {
086: InitialContext context = new InitialContext();
087: DataSource dataSource = (DataSource) context
088: .lookup("java:comp/env/jdbc/MySQLDB");
089: connection = dataSource.getConnection();
090: statement = connection
091: .prepareStatement("insert into logtable values(?, ?)");
092: statement.setTimestamp(1, new Timestamp(System
093: .currentTimeMillis()));
094: statement.setString(2, message);
095: statement.executeUpdate();
096: } catch (Exception exc) {
097: sessionContext.setRollbackOnly();
098: throw new EJBException(exc.getMessage());
099: } finally {
100: try {
101: if (null != statement)
102: statement.close();
103: if (null != connection)
104: connection.close();
105: } catch (SQLException sqlExc) {
106:
107: }
108: }
109: }
110:
111: /*
112: * @ejb:create-method
113: */
114: public void ejbCreate() throws CreateException {
115:
116: }
117:
118: public void ejbActivate() throws EJBException, RemoteException {
119:
120: }
121:
122: public void ejbPassivate() throws EJBException, RemoteException {
123:
124: }
125:
126: public void ejbRemove() throws EJBException, RemoteException {
127:
128: }
129:
130: public void setSessionContext(SessionContext sessionContext)
131: throws EJBException, RemoteException {
132: this.sessionContext = sessionContext;
133: }
134: }
|