0 Replies Latest reply on Feb 6, 2008 4:37 AM by bridge007

    problem with usertransaction annotation

    bridge007

      Hi,

      I'm trying to migrate a session bean from ejb 2.1 to ejb 3.0.
      The migration is almost finished, but I have a problem while migrating
      the usertransaction. I want to use an annotation.
      The annotation works fine for the datasource, but for the usertransaction it has no effect. I always get a NullpointerException.

      This is the code before the usertransaction migration, but with datasource migration to ejb 3.0:

      @Resource protected SessionContext c_sctSContext = null;
      @Resource(mappedName="java:/LABSP_DS") javax.sql.DataSource database;
       protected javax.transaction.UserTransaction ut;
       ...
      protected Connection initTransactionWithConnection()
       throws BSConnectionTroubleException {
       Connection returnConnection = null;
      
       try {
       this.ut = this.c_sctSContext.getUserTransaction();
      
       if (this.ut.getStatus() != javax.transaction.Status.STATUS_NO_TRANSACTION) {
       getLogger()
       .debug(
       this
       + "Failure Transaction State != Status.STATUS_NO_TRANSACTION (" + this.ut.getStatus() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
      
       throw new BSConnectionTroubleException(
       "Failure Transaction State != Status.STATUS_NO_TRANSACTION (" + this.ut.getStatus() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
       }
      
       returnConnection = getConnection();
       this.ut.begin();
      
       } catch (Exception e) {
       getLogger().error("Error init connection/transaction:", e); //$NON-NLS-1$
      
       throw new BSConnectionTroubleException("Could not connect to database", e); //$NON-NLS-1$
       }
      
       return returnConnection;
       }


      This code works fine.

      Now my migration of the usertransaction (add of resource annotation and elemination of usertransaction lookup):

      @Resource protected SessionContext c_sctSContext = null;
      @Resource(mappedName="java:/LABSP_DS") javax.sql.DataSource database;
      @Resource protected javax.transaction.UserTransaction ut;
       ...
      protected Connection initTransactionWithConnection()
       throws BSConnectionTroubleException {
       Connection returnConnection = null;
      
       try {
      
       if (this.ut.getStatus() != javax.transaction.Status.STATUS_NO_TRANSACTION) {
       getLogger()
       .debug(
       this
       + "Failure Transaction State != Status.STATUS_NO_TRANSACTION (" + this.ut.getStatus() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
      
       throw new BSConnectionTroubleException(
       "Failure Transaction State != Status.STATUS_NO_TRANSACTION (" + this.ut.getStatus() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
       }
      
       returnConnection = getConnection();
       this.ut.begin();
      
       } catch (Exception e) {
       getLogger().error("Error init connection/transaction:", e); //$NON-NLS-1$
      
       throw new BSConnectionTroubleException("Could not connect to database", e); //$NON-NLS-1$
       }
      
       return returnConnection;
       }


      After this migration, I always get a NullpointerException on line:
      if (this.ut.getStatus() != javax.transaction.Status.STATUS_NO_TRANSACTION)
      


      This is because the usertransaction was not injected. The question is why?
      Help is welcome!
      Any idea?