0 Replies Latest reply on Mar 20, 2006 7:09 PM by Xavier Ferret

    Loading a database on @PostConstruct callback for SessionBea

    Xavier Ferret Newbie

      I've a session bean wich on his @PostConstruct callback method tries to load the database on first time.

      The problem is that i'm constantly getting an error of type "EntityManager must be access within a transaction". I've tried to get a user transaction from
      the injected session context but I'don't get it working.

      this is the code I've

      @PostConstruct
      public void postConstructCallback()
      {

      System.out.println("LanguageBean. postConstructCallback(). Started.");

      EntityManager manager;
      Integer intTableSize;

      // EntityTransaction transaction = manager.getTransaction();
      //UserTransaction transaction = context.getUserTransaction();

      //manager = emf.createEntityManager(PersistenceContextType.EXTENDED);

      try
      {
      //Query query = manager.createQuery("SELECT count(language) FROM Language language");
      //intTableSize = (Integer) query.getSingleResult();

      /*
      if ( transaction != null && intTableSize == 0 )
      {
      transaction.begin();
      this.loadDatabase(manager);
      }
      */
      System.out.println( "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" );
      System.out.println( this.getAll().size() );
      System.out.println( "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" );
      }
      catch (Exception e)
      {
      /*
      try {
      transaction.rollback();
      } catch (IllegalStateException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      } catch (SecurityException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      } catch (SystemException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      }
      */
      System.out.println("LanguageBean. postConstructCallback(). Rollback due to errors.");
      e.printStackTrace();
      }
      finally
      {
      }

      System.out.println("LanguageBean. postConstructCallback(). Ended.");