Java Reference
In-Depth Information
method that creates the JB oss/Hibernate implementation of an EntityManager-
Factory and a tearDown() method that closes it. Like the JDO and Hibernate tests
you have seen earlier, this test uses a doInTransaction() method to execute code
within a transaction. This method opens an EntityManager , begins a transaction,
executes the callback, commits the transaction, and closes the EntityManager .
Listing 10.3
EJB3PendingOrderPersistenceTests
public class EJB3PendingOrderPersistenceTests extends
TestCase {
private EntityManager em;
private EntityTransaction transaction;
private EntityManagerFactory emf;
private String poId;
protected void setUp() throws Exception {
bb super.setUp();
bb Properties props = new Properties();
B Specifies persistence provider
bb props.setProperty(
Persistence.PERSISTENCE_PROVIDER,
HibernatePersistence.class.getName());
C Creates EntityManagerFactory
emf = Persistence
.createEntityManagerFactory(props);
}
protected void tearDown() throws Exception {
bb super.tearDown();
bb if (emf != null)
emf.close();
}
D Closes EntityManagerFactory
protected void doWithTransaction(TxnCallback cb)
throws Throwable {
E Creates EntityManager
bb em = emf
.createEntityManager(PersistenceContextType.EXTENDED);
transaction = em.getTransaction();
try {
transaction.begin();
F Begins
transaction
G Executes
callback
cb.execute();
transaction.commit();
} finally {
if (transaction != null
&& transaction.isActive())
H Commits or rolls back
the transaction
 
Search WWH ::




Custom Search