我正在尝试使用与Ebean和Play Framework 2.2.2的交易。由于@transactional注释在我的服务方法中不起作用(我想它们没有像Ebean文档中的解释那样增强),因此我尝试手动管理交易。
我有此代码:
public void method1() {
Ebean.beginTransaction();
try {
// Do something
// Do something else
method2();
Ebean.commitTransaction();
}
finally {
Ebean.endTransaction();
}
}
public void method2() {
Ebean.beginTransaction();
try {
doSomething();
Ebean.commitTransaction();
}
finally {
Ebean.endTransaction();
}
}
使用此代码,我在method2
中开始交易时会有以下错误:
javax.persistence.PersistenceException: The existing transaction is still active?
像我对 @transactional一样,在手动声明交易时如何定义交易的传播?我的method2
可以在method1
中和其他地方调用,因此我无法删除其中的交易...
ebean.begintransaction();方法返回事务对象。
所以,如果您更改了方法的内部,例如可能会有所帮助:
Transaction t = Ebean.beginTransaction();
try {
doSomething();
t.commit();
}
finally {
t.end();
}
尝试在方法2
上开始另一笔交易替换
@Transactional
public void method2() {..}
@Transactional(type = TxType.REQUIRES_NEW)
public void method2() {..}
或
TxScope txScope = TxScope.requiresNew();
public void method2() {
Ebean.execute(txScope, new TxRunnable() {
public void run() {
..
}
}
您可能在这里找到这些建议:http://www.avaje.org/doc/ebean-userguide.pdf
是正确的EBEAN每个线程每个eBeanserver都有一项主动交易。