Java Code Examples for javax.persistence.EntityManager#joinTransaction()

The following examples show how to use javax.persistence.EntityManager#joinTransaction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JtaEntityManagerRegistry.java    From tomee with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example 2
Source File: LocalContainerEntityManagerFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 3
Source File: JtaEntityManagerRegistry.java    From tomee with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 4
Source File: LocalContainerEntityManagerFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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();
}
 
Example 5
Source File: ApplicationManagedEntityManagerIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example 6
Source File: LocalContainerEntityManagerFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 7
Source File: ApplicationManagedEntityManagerIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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"));
}
 
Example 8
Source File: ApplicationManagedEntityManagerIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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();
}
 
Example 9
Source File: ApplicationManagedEntityManagerIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 10
Source File: LocalContainerEntityManagerFactoryBeanTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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();
}
 
Example 11
Source File: ApplicationManagedEntityManagerIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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"));
}
 
Example 12
Source File: ApplicationManagedEntityManagerIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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();
}
 
Example 13
Source File: ApplicationManagedEntityManagerIntegrationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public void testInstantiateAndSave() {
	EntityManager em = entityManagerFactory.createEntityManager();
	em.joinTransaction();
	doInstantiateAndSave(em);
}
 
Example 14
Source File: ApplicationManagedEntityManagerIntegrationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testInstantiateAndSave() {
	EntityManager em = entityManagerFactory.createEntityManager();
	em.joinTransaction();
	doInstantiateAndSave(em);
}
 
Example 15
Source File: LocalContainerEntityManagerFactoryBeanTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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();
}
 
Example 16
Source File: SharedEntityManagerCreatorTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test(expected = TransactionRequiredException.class)
public void transactionRequiredExceptionOnJoinTransaction() {
	EntityManagerFactory emf = mock(EntityManagerFactory.class);
	EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
	em.joinTransaction();
}
 
Example 17
Source File: AbstractJpaClinicTests.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testApplicationManaged() {
	EntityManager appManaged = this.entityManagerFactory.createEntityManager();
	appManaged.joinTransaction();
}
 
Example 18
Source File: LocalContainerEntityManagerFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@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();
}
 
Example 19
Source File: LocalContainerEntityManagerFactoryBeanTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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();
}
 
Example 20
Source File: ApplicationManagedEntityManagerIntegrationTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testEntityManagerProxyAcceptsProgrammaticTxJoining() {
	EntityManager em = entityManagerFactory.createEntityManager();
	em.joinTransaction();
}