Search code examples
javajakarta-eejbossejb

@EJB annotation null


I'm trying to understand how @EJB(beanName="etc") works. I've tried to use it and when called stays null. I've read in other threads that I shouldn't be using a "new ClassNameEJB()" to initialize it. I am under the impression that using the @EJB annotation provides the initialization for the local private field. Here are my implementations:

Bean Class:

@Stateless(name = "AsynchronousErrorLog")
@TransactionManagement(TransactionManagementType.BEAN)
public class AsynchronousErrorLogEJB {

@PersistenceContext(unitName = "errorLog")
EntityManager entityManager;
@Resource
private EJBContext context;
private static final Logger LOG = Logger.getLogger(AsynchronousErrorLogEJB.class);


/**
 * logError: method to store error within the database 
 * @param errorLog: log to be persisted to the database
 */
@Asynchronous   
public void logError(final ErrorLog errorLog) {
    try {
        LOG.debug("Entering logError");
        context.getUserTransaction().begin();
        entityManager.persist(errorLog);
        context.getUserTransaction().commit();
    } catch (final Exception e) {
        LOG.secureError("Exception while logging error during transaction", e);
        try {
            context.getUserTransaction().rollback();
        } catch (final Exception e1) {
            LOG.secureError("Could not rollback transaction", e1);
        }
    }
    LOG.debug("error logging complete.");
}

}

I have in a class a private @EJB field written as such:

@EJB(beanName="AsynchronousErrorLogEJB")
private AsynchronousErrorLogEJB errorLogLocal;

which utilizes the EJB as such:

    try {
        ErrorLog eLog = new ErrorLog(e, ServerName.getServerName());
        errorLogLocal.logError(eLog);
    } catch (GeneralException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

I have the table structure for this EJB as such:

@Entity(name="ERROR_LOG")
public class ErrorLog {

    @Id
    @Column(name="ERROR_LOG_ID")
    private String errorLogId;
    @Column(name="ERROR_MESSAGE")
    private String errorMessage;
    @Column(name="TIMESTAMP")
    private Date timeStamp;
    @Column(name="SERVER_NAME")
    private String serverName;
    @Column(name="STACK_TRACE")
    private String stackTrace;

when it gets to the errorLogLocal.logError, it passes right over it. When debugging in JBoss 9 the value for errorLogLocal is null. Is there a step I'm missing somewhere? Do I need to include some underlying structure in an xml file to the container? Do I have to use INitialContext.lookup in some instances or can I use @EJB notation without lookup?

EDIT: I've added the necessary elements to my persistence.xml (under META-INF) file for the PersistenceContext. I'm not sure if its properly implemented but I followed along with a previous implementation:

    <persistence-unit name="errorLog">
        <non-jta-data-source>java:/jdbc/STOMP</non-jta-data-source>
        <class>com.ens.stomp.message.log.ErrorLog</class>
        <properties>
            <property name="hibernate.archive.autodetection" value="false" />
        </properties>
    </persistence-unit>

</persistence> 

Solution

  • Make sure you are using @EJB from an EJB, Servlet or JSF Managed Bean.. You can´t use @EJB from a Pojo