[英]Transaction not active
I got this problem and I can't see whats wrong. 我遇到了这个问题,看不到有什么问题。
Exception in thread "main" java.lang.IllegalStateException: Transaction not active
I want to get a Client by his id. 我想通过他的身份证件获得客户。 So my code is: 所以我的代码是:
... somewhere at UI Layer ... {
...
Cliente c = ServicesFactory.getClientService().clientWith(id);
...
}
Where clientWith is something like: 其中clientWith类似于:
@Override
public Cliente clientWith(Long id) throws BusinessException {
return (Cliente) executor.execute(new FindClientByID(id));
}
And FindClientById is a command with this code 而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);
}
}
And ClienteFinder is: ClienteFinder是:
public class ClienteFinder {
public static Cliente findById(Long id) {
return Jpa.getManager().find(Cliente.class, id);
}
}
The class Cliente I think it's well mapped. 我认为该类Cliente映射良好。 Where my code fails and why? 我的代码在哪里失败,为什么?
EDIT 编辑
Ok, my code is crashing at method execute() of FindClientByID but I really don't know why. 好的,我的代码在FindClientByID的方法execute()处崩溃,但我真的不知道为什么。 The call seems to throw a RuntimeException. 该调用似乎引发RuntimeException。
Btw, my Command Executor is something like 顺便说一句,我的命令执行器就像
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;
}
Thanks guys : D 谢谢大家:D
The problem was at one catch. 问题是一catch而就的。 Thanks guys. 多谢你们。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.