2 Replies Latest reply on Jan 6, 2006 1:11 PM by david komar

    EntityManager returns null from @PersistenceContext

    Eric Broda Newbie

      I am unable to successfully create an EntityManager (returns null) using:
      - jboss 4.0.3RC1
      - EJB 3.0
      - operating system: Win2K
      - database: MS SQL Server 2k

      Tried to apply concepts in the EJB3 turorials, and scanned the forums for last 3 days, and tried many different configs/suggestions, but no success... any help is appreciated!

      I was not sure which forum to put this in (also in Persistence forum)... my apologies for the duplicate...


      Configuration/Code details follow:

      - the entity-manager/name configured within persistence.xml is "bgsdb" and is used as the PersistenceContext unitName (code fragment shown later); the EntityManager appears to be established based upon server.log information (again, fragment provided below)
      - standardjaws.xml and standardjbosscmp-jdbc.xml are configured to use MS SQL Server (server.log confirms that it can connect)
      - mssql-ds.xml appears configured properly (within deploy directory)

      *** persistence.xml

      <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>

      Note: the class (code fragment below) is instantiated within a stateless session bean ("execute" method is called from this stateless session bean)
      *** Code fragment
      package com.....
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      public class CLITest {
      EntityManager em;
      public String execute(){
      System.out.println( "emb: CLITest started........");
      System.out.println( "emb: em: " + em );
      System.out.println( "emb: em (string): " + em.toString() );

      Upon executing the code, the EntityManager returned is null (expert from server.log is listed below)

      *** server.log - error
      2005-08-04 22:37:25,015 INFO [STDOUT] emb: CLITest started........
      2005-08-04 22:37:25,015 INFO [STDOUT] emb: em: null
      2005-08-04 22:37:25,015 INFO [STDOUT] javax.ejb.EJBException: null; CausedByException is:
      2005-08-04 22:37:25,015 INFO [STDOUT] at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:46)...........

      *** server.log - EntityManager appears configured
      2005-08-04 22:28:54,180 INFO [org.jboss.ejb3.Ejb3Module] Create EntityManager with JNDI name: bgsdb

      *** server.log - SQL SERVER connection established
      2005-08-04 22:28:52,037 INFO [org.hibernate.connection.DatasourceConnectionProvider] Using datasource: java:/MSSQLDS