可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
How do I have the following code in my non-ejb application. The code works.
@Override public void saveItems(Collection items) { synchronized (em) { EntityTransaction tx = em.getTransaction(); try { tx.begin(); for (T item : items) { saveItem_((Class) null, item); } tx.commit(); } finally { if (tx.isActive()) { tx.rollback(); } } } }
In a new application I'm using EJB3 + JSF and would like to re-use the library containing the code above. My peristence unit for the new application looks like this:
org.hibernate.ejb.HibernatePersistenceMySQLConnection
My new application throw an exception when it hits this line:
EntityTransaction tx = em.getTransaction();
the exception is:
A JTA EntityManager cannot use getTransaction()
Which is clear enough. The question is how would I convert my code to have the transactions managed by the container. Presumably my bean methods need to be annotated appropriately... The question is how?
回答1:
EntityTransaction
is used with entity manager of type resource local. If you want to use JTA, then have to use UserTransaction
interface.
From Documentation : EntityTransaction - Interface used to control transactions on resource-local entity managers. The EntityManager.getTransaction() method returns the EntityTransaction interface.
Edit: Added pseudo code.
@Resource private SessionContext sessionContext; void execute(){ UserTransaction userTxn = sessionContext.getUserTransaction(); try{ userTxn.begin(); /** * do-something */ userTxn.commit(); } catch(Throwable e){ userTxn.rollback(); //-- Include this in try-catch } }
回答2:
In the simplest case - it just works. If you have your EntityManager injected into EJB and use no special annotations, the transaction will open in the first EJB method entered (this means that if EjbA calls EjbB and that in turn calls EjbC, only one transaction will be used across all the EJB methods). If you want to modify how transactions are controlled, look up @Transaction.
The simplest way to do a rollback is to throw an exception marked with @ApplicationException(rollback=true)
I may be wrong, but judging from your code you should read up on the difference between EXTENDED and NORMAL EntityManager. It looks like you are using an extended em in a very awkward way (moving the loop out of transaction would help you get rid of finally).
Small edit: if you try to use UserTransaction, as the other post suggests, you will get an error, because a standard EntityManager (that you are probably using) uses the so called CMT (Container Managed Transactions). Don't touch it, unless you understand the three basic opositions (if you want, I can elaborate, but frankly, you will NOT need it):
- container managed EntityManager versus application managed EntityManager,
- container managed transactions versus application managed transactions,
- NORMAL EntityManager and EXTENDED EntityManager.