下面列出了javax.persistence.EntityManager#joinTransaction ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void transactionStarted(final InstanceId instanceId) {
if (instanceId == null) {
throw new NullPointerException("instanceId is null");
}
if (!isTransactionActive()) {
throw new TransactionRequiredException();
}
final Map<EntityManagerFactory, EntityManagerTracker> entityManagers = entityManagersByDeploymentId.get(instanceId);
if (entityManagers == null) {
return;
}
for (final Map.Entry<EntityManagerFactory, EntityManagerTracker> entry : entityManagers.entrySet()) {
final EntityManagerFactory entityManagerFactory = entry.getKey();
final EntityManagerTracker value = entry.getValue();
final EntityManager entityManager = value.getEntityManager();
if (value.autoJoinTx) {
entityManager.joinTransaction();
}
final EntityManagerTxKey txKey = new EntityManagerTxKey(entityManagerFactory);
transactionRegistry.putResource(txKey, entityManager);
}
}
@Test
public void testApplicationManagedEntityManagerWithTransaction() throws Exception {
Object testEntity = new Object();
EntityTransaction mockTx = mock(EntityTransaction.class);
// This one's for the tx (shared)
EntityManager sharedEm = mock(EntityManager.class);
given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
// This is the application-specific one
EntityManager mockEm = mock(EntityManager.class);
given(mockEm.getTransaction()).willReturn(mockTx);
given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
JpaTransactionManager jpatm = new JpaTransactionManager();
jpatm.setEntityManagerFactory(cefb.getObject());
TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
EntityManagerFactory emf = cefb.getObject();
assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());
assertNotSame("EMF must be proxied", mockEmf, emf);
EntityManager em = emf.createEntityManager();
em.joinTransaction();
assertFalse(em.contains(testEntity));
jpatm.commit(txStatus);
cefb.destroy();
verify(mockTx).begin();
verify(mockTx).commit();
verify(mockEm).contains(testEntity);
verify(mockEmf).close();
}
@Test
public void testReuseInNewTransaction() {
EntityManager em = entityManagerFactory.createEntityManager();
em.joinTransaction();
doInstantiateAndSave(em);
endTransaction();
assertFalse(em.getTransaction().isActive());
startNewTransaction();
// Call any method: should cause automatic tx invocation
assertFalse(em.contains(new Person()));
assertFalse(em.getTransaction().isActive());
em.joinTransaction();
assertTrue(em.getTransaction().isActive());
doInstantiateAndSave(em);
setComplete();
endTransaction(); // Should rollback
assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));
// Now clean up the database
startNewTransaction();
em.joinTransaction();
deleteAllPeopleUsingEntityManager(em);
assertEquals("People have been killed", 0, countRowsInTable(em, "person"));
setComplete();
}
@Test
public void testRollbackOccurs() {
EntityManager em = entityManagerFactory.createEntityManager();
em.joinTransaction();
doInstantiateAndSave(em);
endTransaction(); // Should rollback
assertEquals("Tx must have been rolled back", 0, countRowsInTable(em, "person"));
}
@Test
public void testApplicationManagedEntityManagerWithTransaction() throws Exception {
Object testEntity = new Object();
EntityTransaction mockTx = mock(EntityTransaction.class);
// This one's for the tx (shared)
EntityManager sharedEm = mock(EntityManager.class);
given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
// This is the application-specific one
EntityManager mockEm = mock(EntityManager.class);
given(mockEm.getTransaction()).willReturn(mockTx);
given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
JpaTransactionManager jpatm = new JpaTransactionManager();
jpatm.setEntityManagerFactory(cefb.getObject());
TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
EntityManagerFactory emf = cefb.getObject();
assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());
assertNotSame("EMF must be proxied", mockEmf, emf);
EntityManager em = emf.createEntityManager();
em.joinTransaction();
assertFalse(em.contains(testEntity));
jpatm.commit(txStatus);
cefb.destroy();
verify(mockTx).begin();
verify(mockTx).commit();
verify(mockEm).contains(testEntity);
verify(mockEmf).close();
}
public void testReuseInNewTransaction() {
EntityManager em = entityManagerFactory.createEntityManager();
em.joinTransaction();
doInstantiateAndSave(em);
endTransaction();
assertFalse(em.getTransaction().isActive());
startNewTransaction();
// Call any method: should cause automatic tx invocation
assertFalse(em.contains(new Person()));
assertFalse(em.getTransaction().isActive());
em.joinTransaction();
assertTrue(em.getTransaction().isActive());
doInstantiateAndSave(em);
setComplete();
endTransaction(); // Should rollback
assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));
// Now clean up the database
startNewTransaction();
em.joinTransaction();
deleteAllPeopleUsingEntityManager(em);
assertEquals("People have been killed", 0, countRowsInTable(em, "person"));
setComplete();
}
@Test
public void testReuseInNewTransaction() {
EntityManager em = entityManagerFactory.createEntityManager();
em.joinTransaction();
doInstantiateAndSave(em);
endTransaction();
assertFalse(em.getTransaction().isActive());
startNewTransaction();
// Call any method: should cause automatic tx invocation
assertFalse(em.contains(new Person()));
assertFalse(em.getTransaction().isActive());
em.joinTransaction();
assertTrue(em.getTransaction().isActive());
doInstantiateAndSave(em);
setComplete();
endTransaction(); // Should rollback
assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));
// Now clean up the database
startNewTransaction();
em.joinTransaction();
deleteAllPeopleUsingEntityManager(em);
assertEquals("People have been killed", 0, countRowsInTable(em, "person"));
setComplete();
}
@Test
public void testRollbackOccurs() {
EntityManager em = entityManagerFactory.createEntityManager();
em.joinTransaction();
doInstantiateAndSave(em);
endTransaction(); // Should rollback
assertEquals("Tx must have been rolled back", 0, countRowsInTable(em, "person"));
}
@Test
public void testApplicationManagedEntityManagerWithJtaTransaction() throws Exception {
Object testEntity = new Object();
// This one's for the tx (shared)
EntityManager sharedEm = mock(EntityManager.class);
given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
// This is the application-specific one
EntityManager mockEm = mock(EntityManager.class);
given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
MutablePersistenceUnitInfo pui = ((MutablePersistenceUnitInfo) cefb.getPersistenceUnitInfo());
pui.setTransactionType(PersistenceUnitTransactionType.JTA);
JpaTransactionManager jpatm = new JpaTransactionManager();
jpatm.setEntityManagerFactory(cefb.getObject());
TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
EntityManagerFactory emf = cefb.getObject();
assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());
assertNotSame("EMF must be proxied", mockEmf, emf);
EntityManager em = emf.createEntityManager();
em.joinTransaction();
assertFalse(em.contains(testEntity));
jpatm.commit(txStatus);
cefb.destroy();
verify(mockEm).joinTransaction();
verify(mockEm).contains(testEntity);
verify(mockEmf).close();
}
public void testCommitOccurs() {
EntityManager em = entityManagerFactory.createEntityManager();
em.joinTransaction();
doInstantiateAndSave(em);
setComplete();
endTransaction(); // Should rollback
assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));
// Now clean up the database
deleteFromTables("person");
}
@Test
public void testApplicationManagedEntityManagerWithJtaTransaction() throws Exception {
Object testEntity = new Object();
// This one's for the tx (shared)
EntityManager sharedEm = mock(EntityManager.class);
given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
// This is the application-specific one
EntityManager mockEm = mock(EntityManager.class);
given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
MutablePersistenceUnitInfo pui = ((MutablePersistenceUnitInfo) cefb.getPersistenceUnitInfo());
pui.setTransactionType(PersistenceUnitTransactionType.JTA);
JpaTransactionManager jpatm = new JpaTransactionManager();
jpatm.setEntityManagerFactory(cefb.getObject());
TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
EntityManagerFactory emf = cefb.getObject();
assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());
assertNotSame("EMF must be proxied", mockEmf, emf);
EntityManager em = emf.createEntityManager();
em.joinTransaction();
assertFalse(em.contains(testEntity));
jpatm.commit(txStatus);
cefb.destroy();
verify(mockEm).joinTransaction();
verify(mockEm).contains(testEntity);
verify(mockEmf).close();
}
private void addEntityManagers(final InstanceId instanceId, final Map<EntityManagerFactory, EntityManagerTracker> entityManagers)
throws EntityManagerAlreadyRegisteredException {
if (instanceId == null) {
throw new NullPointerException("instanceId is null");
}
if (entityManagers == null) {
throw new NullPointerException("entityManagers is null");
}
if (isTransactionActive()) {
for (final Map.Entry<EntityManagerFactory, EntityManagerTracker> entry : entityManagers.entrySet()) {
final EntityManagerFactory entityManagerFactory = entry.getKey();
final EntityManagerTracker tracker = entry.getValue();
final EntityManager entityManager = tracker.getEntityManager();
final EntityManagerTxKey txKey = new EntityManagerTxKey(entityManagerFactory);
final EntityManager oldEntityManager = (EntityManager) transactionRegistry.getResource(txKey);
if (entityManager == oldEntityManager) {
break;
}
if (oldEntityManager != null) {
throw new EntityManagerAlreadyRegisteredException("Another entity manager is already registered for this persistence unit");
}
if (tracker.autoJoinTx) {
entityManager.joinTransaction();
}
transactionRegistry.putResource(txKey, entityManager);
}
}
entityManagersByDeploymentId.put(instanceId, entityManagers);
}
@Test
public void testEntityManagerProxyAcceptsProgrammaticTxJoining() {
EntityManager em = entityManagerFactory.createEntityManager();
em.joinTransaction();
}
@Test
public void testApplicationManagedEntityManagerWithTransactionAndCommitException() throws Exception {
Object testEntity = new Object();
EntityTransaction mockTx = mock(EntityTransaction.class);
willThrow(new OptimisticLockException()).given(mockTx).commit();
// This one's for the tx (shared)
EntityManager sharedEm = mock(EntityManager.class);
given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
// This is the application-specific one
EntityManager mockEm = mock(EntityManager.class);
given(mockEm.getTransaction()).willReturn(mockTx);
given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
JpaTransactionManager jpatm = new JpaTransactionManager();
jpatm.setEntityManagerFactory(cefb.getObject());
TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
EntityManagerFactory emf = cefb.getObject();
assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());
assertNotSame("EMF must be proxied", mockEmf, emf);
EntityManager em = emf.createEntityManager();
em.joinTransaction();
assertFalse(em.contains(testEntity));
try {
jpatm.commit(txStatus);
fail("Should have thrown OptimisticLockingFailureException");
}
catch (OptimisticLockingFailureException ex) {
// expected
}
cefb.destroy();
verify(mockTx).begin();
verify(mockEm).contains(testEntity);
verify(mockEmf).close();
}
@Test(expected = TransactionRequiredException.class)
public void transactionRequiredExceptionOnJoinTransaction() {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
em.joinTransaction();
}
@Test
public void testApplicationManagedEntityManagerWithTransactionAndCommitException() throws Exception {
Object testEntity = new Object();
EntityTransaction mockTx = mock(EntityTransaction.class);
willThrow(new OptimisticLockException()).given(mockTx).commit();
// This one's for the tx (shared)
EntityManager sharedEm = mock(EntityManager.class);
given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
// This is the application-specific one
EntityManager mockEm = mock(EntityManager.class);
given(mockEm.getTransaction()).willReturn(mockTx);
given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
JpaTransactionManager jpatm = new JpaTransactionManager();
jpatm.setEntityManagerFactory(cefb.getObject());
TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
EntityManagerFactory emf = cefb.getObject();
assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());
assertNotSame("EMF must be proxied", mockEmf, emf);
EntityManager em = emf.createEntityManager();
em.joinTransaction();
assertFalse(em.contains(testEntity));
try {
jpatm.commit(txStatus);
fail("Should have thrown OptimisticLockingFailureException");
}
catch (OptimisticLockingFailureException ex) {
// expected
}
cefb.destroy();
verify(mockTx).begin();
verify(mockEm).contains(testEntity);
verify(mockEmf).close();
}
public void testInstantiateAndSave() {
EntityManager em = entityManagerFactory.createEntityManager();
em.joinTransaction();
doInstantiateAndSave(em);
}
@Test
public void testInstantiateAndSave() {
EntityManager em = entityManagerFactory.createEntityManager();
em.joinTransaction();
doInstantiateAndSave(em);
}
public void testApplicationManaged() {
EntityManager appManaged = this.entityManagerFactory.createEntityManager();
appManaged.joinTransaction();
}
@Test
public void testApplicationManagedEntityManagerWithTransactionAndCommitException() throws Exception {
Object testEntity = new Object();
EntityTransaction mockTx = mock(EntityTransaction.class);
willThrow(new OptimisticLockException()).given(mockTx).commit();
// This one's for the tx (shared)
EntityManager sharedEm = mock(EntityManager.class);
given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
// This is the application-specific one
EntityManager mockEm = mock(EntityManager.class);
given(mockEm.getTransaction()).willReturn(mockTx);
given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
JpaTransactionManager jpatm = new JpaTransactionManager();
jpatm.setEntityManagerFactory(cefb.getObject());
TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
EntityManagerFactory emf = cefb.getObject();
assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());
assertNotSame("EMF must be proxied", mockEmf, emf);
EntityManager em = emf.createEntityManager();
em.joinTransaction();
assertFalse(em.contains(testEntity));
try {
jpatm.commit(txStatus);
fail("Should have thrown OptimisticLockingFailureException");
}
catch (OptimisticLockingFailureException ex) {
// expected
}
cefb.destroy();
verify(mockTx).begin();
verify(mockEm).contains(testEntity);
verify(mockEmf).close();
}