4 Replies Latest reply on Apr 3, 2006 3:54 PM by kanto

    PersistenceContextType.EXTENDED does not work ??

    kanto

      hi,
      i have a problem with rolling back transactions that are splitted into
      several method invacation on the same stateful bean.

      my bean (lets call it BeanEJB) looks like:

      @Stateful
      @TransactionAttribute( TransactionAttributeType.REQUIRES_NEW )
      public class BasicImport implements ImportTool {
      
       Logger log = Logger.getLogger(BasicImport.class);
      
       private int recordNumber = 0;
      
       @PersistenceContext(unitName="SpaEM", type=PersistenceContextType.EXTENDED)
       EntityManager em;
      
       public BasicImport() {
       }
      
       public String readRecord(BufferedReader br) throws IOException {
      
       }
      
       public void importRecord(Account a) {
       }
      
       public void importRecords( BufferedReader br, int rn ) throws IOException, InvalidFormatException, ImportException {
      
       }
      
       @TransactionAttribute(TransactionAttributeType.REQUIRED)
       public void importRecords( String data, int recordNumber) throws IOException, InvalidFormatException, ImportException {
       log.info("Transaction: "+em.getTransaction().toString());
       BufferedReader br = new BufferedReader( new StringReader(data));
       importRecords(br, recordNumber);
       }
      
       public Account parseRecord(String record) throws InvalidFormatException {
      
       }
      
       @Remove
       public void commit() {
       log.info("Remove method - flushing");
       em.flush();
       }
      
       public int getRecordLineLength() {
      
       }
      }
      


      now i have another bean (lets call it BeanB - simple bean stored in HttpSession - not EJB) that is supposed to make use of the bean above.

      in BaneB i have following code:

      ImportTool it = (ImportTool)getInitialCtx().lookup(IMPORT_USERS_BEAN);
      ...
      it.importRecords(part, toRead);
      it.importRecords(part, toRead);
      
      it.commit();
      ...
      


      now i have prepared data thac causes exception udring second execution of it.importRecords(part, toRead);

      15:55:28,250 WARN [JDBCExceptionReporter] SQL Error: 0, SQLState: null
      15:55:28,250 ERROR [JDBCExceptionReporter] Zadanie wsadowe 0 insert into spa_user_data (name, country, surname, email, phone, pesel, city, street, houseNo, postcode, id) values (namee12, countryy12, surnamee12, emaill12, phonee12, pesell2, cityy12, streett12, housenoo12, postcodee12, 23943) zosta?o przerwane. Wywo?aj getNextException, aby pozna? przyczyn?.
      15:55:28,250 WARN [JDBCExceptionReporter] SQL Error: 0, SQLState: 23505
      15:55:28,250 ERROR [JDBCExceptionReporter] ERROR: duplicate key violates unique constraint "spa_user_data_pesel_key"
      15:55:28,250 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
      org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
       at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
       at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
      ...
      blah blah
      


      my question is: why data written to database in first invacation of
      it.importRecords(part, toRead);
      is not rolled back ??
      instead data written in second transaction is in fact rolled back.

      am i doing something wrong ??
      im using postgresql database "psql (PostgreSQL) 8.1.3"