2013-11-24 2 views
0

У меня проблема, и я не понимаю, что случилось.Сделки не активны

Exception in thread "main" java.lang.IllegalStateException: Transaction not active 

Я хочу получить Клиента по его идентификатору. Так что мой код:

... somewhere at UI Layer ... { 
    ... 
    Cliente c = ServicesFactory.getClientService().clientWith(id); 
    ... 
} 

Где clientWith что-то вроде:

@Override 
    public Cliente clientWith(Long id) throws BusinessException { 
     return (Cliente) executor.execute(new FindClientByID(id)); 
    } 

И FindClientById это команда, с этим кодом

public class FindClientByID implements Command { 

    private Long id; 

    public FindClientByID(Long id) { 
     this.id = id; 
    } 

    @Override 
    public Object execute() throws BusinessException { 
     return ClienteFinder.findById(id); 
    } 
} 

И ClienteFinder является:

public class ClienteFinder { 

    public static Cliente findById(Long id) { 
     return Jpa.getManager().find(Cliente.class, id); 
    } 
} 

Клиент класса Я думаю, что это хорошо отображено. Где мой код не работает и почему?

EDIT

Хорошо, мой код сбой в методе Execute() из FindClientByID, но я действительно не знаю, почему. Похоже, что вызов вызывает исключение RuntimeException.

Btw, моя команда Палач что-то вроде

public Object execute(Command command) throws BusinessException { 

     EntityManager em = Jpa.createEntityManager(); 
     EntityTransaction trx = em.getTransaction(); 
     trx.begin(); 

     Object ret = null; 
     try { 
      ret = command.execute(); // <-- This line throws RuntimeException 
      trx.commit(); 
     } catch (BusinessException bex) { 
      if(trx.isActive()) 
       trx.rollback(); 
      throw bex; 
     } catch (RuntimeException tex) { 
      if(trx.isActive()) 
       trx.rollback(); 
      trx.rollback(); 
      throw tex; 
     } finally { 
      if(em.isOpen()) 
       em.close(); 
     } 



     return ret; 
    } 

Спасибо, ребята: D

+1

Какую операцию вы используете? –

+0

Я не знаю, я просто новичок в этом. Как я могу это проверить? – tehAnswer

ответ

0

Проблема была в одном улове. Спасибо, парни.

public Object execute(Command command) throws BusinessException { 

     EntityManager em = Jpa.createEntityManager(); 
     EntityTransaction trx = em.getTransaction(); 
     trx.begin(); 

     Object ret = null; 
     try { 
      ret = command.execute(); // <-- This line throws RuntimeException 
      trx.commit(); 
     } catch (BusinessException bex) { 
      if(trx.isActive()) 
       trx.rollback(); 
      throw bex; 
     } catch (RuntimeException tex) { 
      if(trx.isActive()) 
       trx.rollback(); 
      trx.rollback(); <--- **The problem was here.** 
      throw tex; 
     } finally { 
      if(em.isOpen()) 
       em.close(); 
     } 



     return ret; 
    } 
Смежные вопросы