The ejbRemove( ) Method

In addition to handling their own inserts and updates, bean-managed entities must handle their own deletions. When a client application invokes the remove method on the EJB home or EJB object, that method invocation is delegated to the bean-managed entity by calling ejbRemove( ). It is the bean developer’s responsibility to implement an ejbRemove( ) method that deletes the entity’s data from the database. Here’s the ejbRemove( ) method for our bean-managed ShipBean:

public void ejbRemove( ) {
    Connection con = null;
    PreparedStatement ps = null;
    try {
        con = this.getConnection( );
        ps = con.prepareStatement("delete from Ship where id = ?");
        ps.setInt(1, id.intValue( ));
        if (ps.executeUpdate( ) != 1) {
            throw new EJBException("ejbRemove");
        }
    }
    catch (SQLException se) {
        throw new EJBException (se);
    }
    finally {
        try {
            if (ps != null) ps.close( ); 
            if (con!= null) con.close( ); 
        } catch(SQLException se) {
            se.printStackTrace( );
        }
    }
}

Get Enterprise JavaBeans, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.