我在基于Swing的桌面应用程序中使用JPA。我的代码是这样的:
public Object methodA() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
boolean hasError = false;
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// JPA operation does not work here
// because transaction has been committed!
}
});
...
return xXx;
} catch (Exception ex) {
hasError = true;
em.getTransaction().rollback();
} finally {
em.close();
if (!hasError) {
em.getTransaction().commit();
}
}
return null;
}
我将这个try - catch - finally
用于所有需要事务的方法。除了具有SwingUtilities.invokeLater()
的方法之外,它的工作与预期一样。
finally
将在新线程的所有代码执行完毕之前到达;因此,如果在SwingUtilities.invokeLater()
内部有JPA操作,它将失败,因为事务已经提交。
是否有try - catch - finally
的通用用例,我可以遵循以确保只有在所有代码执行后才会提交事务,包括SwingUtilities.invokeLater()
内的代码?
你必须重新考虑你的方法。首先,SwingUtilities.invokeLater()
不是执行JPA操作的最佳选择。该实用工具方法的主要目的是更新UI。关于您的代码,为JPA操作实现单独的线程,该线程将接收事务状态的侦听器。一旦事务完成,您将更新UI。
/**
* Transaction callback.
*/
public interface TransactionListener {
void onTransactionFinished(boolean hasError);
}
/**
* Worker Thread which takes data and performs JPA operations.
*/
public class JPATask implements Runnable {
private final Object dataToPersist;
private final TransactionListener transactionListener;
public JPATask(Object dataToPersist,
TransactionListener transactionListener) {
this.dataToPersist = dataToPersist;
this.transactionListener = transactionListener;
}
private EntityManager getEntityManager() {/* code is omited */}
@Override
public void run() {
EntityManager em = getEntityManager();
try {
em.getTransaction().begin();
// perform JPA actions here
em.getTransaction().commit();
transactionListener.onTransactionFinished(false);
} catch (Exception ex) {
em.getTransaction().rollback();
transactionListener.onTransactionFinished(true);
}
}
}
/**
* Finally you method. Now it looks like this.
*/
public Object methodA() {
JPATask jpaTask = new JPATask(<data to persist>, new TransactionListener() {
@Override
public void onTransactionFinished(boolean hasError) {
// Update UI. It's time to use SwingUtilities.invokeLater()
}
}).start();
}