001: package com.mockrunner.example.ejb;
002:
003: import java.rmi.RemoteException;
004: import java.sql.Connection;
005: import java.sql.ResultSet;
006: import java.sql.SQLException;
007: import java.sql.Statement;
008:
009: import javax.ejb.CreateException;
010: import javax.ejb.EJBException;
011: import javax.ejb.SessionBean;
012: import javax.ejb.SessionContext;
013: import javax.naming.InitialContext;
014: import javax.naming.NamingException;
015: import javax.sql.DataSource;
016:
017: /*
018: * @ejb:bean name="PaySession"
019: * display-name="PaySessionBean"
020: * type="Stateless"
021: * transaction-type="Container"
022: * jndi-name="com/mockrunner/example/PaySession"
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 is the EJB-version of
034: * {@link com.mockrunner.example.jdbc.PayAction}.
035: * It throws a <code>PaySessionException</code> in case of error.
036: */
037: public class PaySessionBean implements SessionBean {
038: private SessionContext sessionContext;
039:
040: /*
041: * @ejb:interface-method
042: * @ejb:transaction type="Required"
043: */
044: public void payBill(String customerId, String billId, double amount)
045: throws PaySessionException {
046: Connection connection = null;
047: try {
048: InitialContext context = new InitialContext();
049: DataSource dataSource = (DataSource) context
050: .lookup("java:comp/env/jdbc/MySQLDB");
051: connection = dataSource.getConnection();
052: String name = getName(connection, customerId);
053: if (null == name) {
054: sessionContext.setRollbackOnly();
055: throw new PaySessionException(
056: PaySessionException.UNKNOWN_CUSTOMER);
057: }
058: checkBillIntegrity(connection, customerId, billId, amount);
059: markBillAsPaid(connection, customerId, billId, amount);
060: System.out.println(amount + " paid from customer " + name);
061: } catch (NamingException exc) {
062: sessionContext.setRollbackOnly();
063: throw new EJBException("JNDI error " + exc.getMessage());
064: } catch (SQLException exc) {
065: sessionContext.setRollbackOnly();
066: throw new EJBException("Database error " + exc.getMessage());
067: } finally {
068: try {
069: if (null != connection)
070: connection.close();
071: } catch (SQLException sqlExc) {
072:
073: }
074: }
075: }
076:
077: private String getName(Connection connection, String customerId)
078: throws SQLException {
079: Statement statement = connection.createStatement();
080: ResultSet result = statement
081: .executeQuery("select name from customers where id='"
082: + customerId + "'");
083: String name = null;
084: if (result.next()) {
085: name = result.getString("name");
086: }
087: result.close();
088: statement.close();
089: return name;
090: }
091:
092: private void checkBillIntegrity(Connection connection,
093: String customerId, String billId, double amount)
094: throws SQLException, PaySessionException {
095: Statement statement = connection.createStatement();
096: ResultSet result = statement
097: .executeQuery("select * from openbills where id='"
098: + billId + "'");
099: try {
100: if (false == result.next()) {
101: sessionContext.setRollbackOnly();
102: throw new PaySessionException(
103: PaySessionException.UNKNOWN_BILL);
104: }
105: if (!result.getString("customerid").equals(customerId)) {
106: sessionContext.setRollbackOnly();
107: throw new PaySessionException(
108: PaySessionException.WRONG_BILL_FOR_CUSTOMER);
109: }
110: if (result.getDouble("amount") != amount) {
111: sessionContext.setRollbackOnly();
112: throw new PaySessionException(
113: PaySessionException.WRONG_AMOUNT_FOR_BILL);
114: }
115: } finally {
116: result.close();
117: statement.close();
118: }
119: }
120:
121: private void markBillAsPaid(Connection connection,
122: String customerId, String billId, double amount)
123: throws SQLException {
124: Statement statement = connection.createStatement();
125: statement.executeUpdate("delete from openbills where id='"
126: + billId + "'");
127: statement.executeUpdate("insert into paidbills values('"
128: + billId + "','" + customerId + "'," + amount + ")");
129: statement.close();
130: }
131:
132: /*
133: * @ejb:create-method
134: */
135: public void ejbCreate() throws CreateException {
136:
137: }
138:
139: public void ejbActivate() throws EJBException, RemoteException {
140:
141: }
142:
143: public void ejbPassivate() throws EJBException, RemoteException {
144:
145: }
146:
147: public void ejbRemove() throws EJBException, RemoteException {
148:
149: }
150:
151: public void setSessionContext(SessionContext sessionContext)
152: throws EJBException, RemoteException {
153: this.sessionContext = sessionContext;
154: }
155: }
|