javax.persistence.spi.PersistenceUnitTransactionType Java Examples

The following examples show how to use javax.persistence.spi.PersistenceUnitTransactionType. 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: PersistenceXmlParser.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void decodeTransactionType(ParsedPersistenceXmlDescriptor persistenceUnit) {
	// if transaction type is set already
	// 		use that value
	// else
	//		if JTA DS
	//			use JTA
	//		else if NOT JTA DS
	//			use RESOURCE_LOCAL
	//		else
	//			use defaultTransactionType
	if ( persistenceUnit.getTransactionType() != null ) {
		return;
	}

	if ( persistenceUnit.getJtaDataSource() != null ) {
		persistenceUnit.setTransactionType( PersistenceUnitTransactionType.JTA );
	}
	else if ( persistenceUnit.getNonJtaDataSource() != null ) {
		persistenceUnit.setTransactionType( PersistenceUnitTransactionType.RESOURCE_LOCAL );
	}
	else {
		persistenceUnit.setTransactionType( defaultTransactionType );
	}
}
 
Example #2
Source File: PersistenceXmlParser.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parse a specific {@code persistence.xml} and return the descriptor for the persistence-unit with matching name
 *
 * @param persistenceXmlUrl The {@code persistence.xml} URL
 * @param name The PU name to match
 * @param transactionType The specific PersistenceUnitTransactionType to incorporate into the persistence-unit descriptor
 * @param integration The Map of integration settings
 *
 * @return The matching persistence-unit descriptor
 */
@SuppressWarnings("WeakerAccess")
public static ParsedPersistenceXmlDescriptor locateNamedPersistenceUnit(
		URL persistenceXmlUrl,
		String name,
		PersistenceUnitTransactionType transactionType,
		Map integration) {
	assert StringHelper.isNotEmpty( name );

	final PersistenceXmlParser parser = new PersistenceXmlParser(
			ClassLoaderServiceImpl.fromConfigSettings( integration ),
			transactionType
	);

	parser.parsePersistenceXml( persistenceXmlUrl, integration );
	assert parser.persistenceUnits.containsKey( name );

	return parser.persistenceUnits.get( name );
}
 
Example #3
Source File: EntityManagerFactoryBuilderImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void applyTransactionProperties(StandardServiceRegistryBuilder ssrBuilder) {
	PersistenceUnitTransactionType txnType = PersistenceUnitTransactionTypeHelper.interpretTransactionType(
			configurationValues.get( JPA_TRANSACTION_TYPE )
	);
	if ( txnType == null ) {
		txnType = persistenceUnit.getTransactionType();
	}
	if ( txnType == null ) {
		// is it more appropriate to have this be based on bootstrap entry point (EE vs SE)?
		txnType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
	}
	boolean hasTxStrategy = configurationValues.containsKey( TRANSACTION_COORDINATOR_STRATEGY );
	if ( hasTxStrategy ) {
		LOG.overridingTransactionStrategyDangerous( TRANSACTION_COORDINATOR_STRATEGY );
	}
	else {
		if ( txnType == PersistenceUnitTransactionType.JTA ) {
			ssrBuilder.applySetting( TRANSACTION_COORDINATOR_STRATEGY, JtaTransactionCoordinatorBuilderImpl.class );
		}
		else if ( txnType == PersistenceUnitTransactionType.RESOURCE_LOCAL ) {
			ssrBuilder.applySetting( TRANSACTION_COORDINATOR_STRATEGY, JdbcResourceLocalTransactionCoordinatorBuilderImpl.class );
		}
	}
}
 
Example #4
Source File: JpaTest.java    From tomee with Apache License 2.0 6 votes vote down vote up
public void testJta() throws Exception {
    transactionType = PersistenceUnitTransactionType.JTA;
    entityManagerFactory = createEntityManagerFactory();

    final Object jpaTestObject = getClass().getClassLoader().loadClass("org.apache.openejb.core.cmp.jpa.JpaTestObject").newInstance();
    set(jpaTestObject, "EntityManagerFactory", EntityManagerFactory.class, entityManagerFactory);
    set(jpaTestObject, "TransactionManager", TransactionManager.class, transactionManager);
    set(jpaTestObject, "NonJtaDs", DataSource.class, nonJtaDs);

    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    invoke(jpaTestObject, "setUp");
    try {
        invoke(jpaTestObject, "jpaLifecycle");
    } finally {
        invoke(jpaTestObject, "tearDown");
    }
}
 
Example #5
Source File: FastBootMetadataBuilder.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static void applyTransactionProperties(PersistenceUnitDescriptor persistenceUnit,
        Map<String, Object> configurationValues) {
    PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionTypeHelper
            .interpretTransactionType(configurationValues.get(JPA_TRANSACTION_TYPE));
    if (transactionType == null) {
        transactionType = persistenceUnit.getTransactionType();
    }
    if (transactionType == null) {
        // is it more appropriate to have this be based on bootstrap entry point (EE vs SE)?
        transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
    }
    boolean hasTransactionStrategy = configurationValues.containsKey(TRANSACTION_COORDINATOR_STRATEGY);
    if (hasTransactionStrategy) {
        LOG.overridingTransactionStrategyDangerous(TRANSACTION_COORDINATOR_STRATEGY);
    } else {
        if (transactionType == PersistenceUnitTransactionType.JTA) {
            configurationValues.put(TRANSACTION_COORDINATOR_STRATEGY,
                    JtaTransactionCoordinatorBuilderImpl.class);
        } else if (transactionType == PersistenceUnitTransactionType.RESOURCE_LOCAL) {
            configurationValues.put(TRANSACTION_COORDINATOR_STRATEGY,
                    JdbcResourceLocalTransactionCoordinatorBuilderImpl.class);
        }
    }
}
 
Example #6
Source File: JpaUtils.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static EntityManagerFactory createEntityManagerFactory(KeycloakSession session, String unitName, Map<String, Object> properties, boolean jta) {
    PersistenceUnitTransactionType txType = jta ? PersistenceUnitTransactionType.JTA : PersistenceUnitTransactionType.RESOURCE_LOCAL;
    List<ParsedPersistenceXmlDescriptor> persistenceUnits = PersistenceXmlParser.locatePersistenceUnits(properties);
    for (ParsedPersistenceXmlDescriptor persistenceUnit : persistenceUnits) {
        if (persistenceUnit.getName().equals(unitName)) {
            List<Class<?>> providedEntities = getProvidedEntities(session);
            for (Class<?> entityClass : providedEntities) {
                // Add all extra entity classes to the persistence unit.
                persistenceUnit.addClasses(entityClass.getName());
            }
            // Now build the entity manager factory, supplying a proxy classloader, so Hibernate will be able
            // to find and load the extra provided entities.
            persistenceUnit.setTransactionType(txType);
            return Bootstrap.getEntityManagerFactoryBuilder(persistenceUnit, properties,
                    new ProxyClassLoader(providedEntities)).build();
        }
    }
    throw new RuntimeException("Persistence unit '" + unitName + "' not found");
}
 
Example #7
Source File: PersistenceXmlParsingTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testExample4() throws Exception {
	SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
	DataSource ds = new DriverManagerDataSource();
	builder.bind("java:comp/env/jdbc/MyDB", ds);

	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-example4.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals(1, info.length);
	assertEquals("OrderManagement4", info[0].getPersistenceUnitName());

	assertEquals(1, info[0].getMappingFileNames().size());
	assertEquals("order-mappings.xml", info[0].getMappingFileNames().get(0));

	assertEquals(3, info[0].getManagedClassNames().size());
	assertEquals("com.acme.Order", info[0].getManagedClassNames().get(0));
	assertEquals("com.acme.Customer", info[0].getManagedClassNames().get(1));
	assertEquals("com.acme.Item", info[0].getManagedClassNames().get(2));

	assertTrue("Exclude unlisted should be true when no value.", info[0].excludeUnlistedClasses());

	assertSame(PersistenceUnitTransactionType.RESOURCE_LOCAL, info[0].getTransactionType());
	assertEquals(0, info[0].getProperties().keySet().size());

	builder.clear();
}
 
Example #8
Source File: ReloadableEntityManagerFactory.java    From tomee with Apache License 2.0 5 votes vote down vote up
@ManagedOperation
@Description("change the current transaction type")
public void setTransactionType(final String type) {
    try {
        final PersistenceUnitTransactionType tt = PersistenceUnitTransactionType.valueOf(type.toUpperCase());
        reloadableEntityManagerFactory.setTransactionType(tt);
    } catch (final Exception iae) {
        // ignored
    }
}
 
Example #9
Source File: LocalContainerEntityManagerFactoryBeanTests.java    From spring-analysis-note 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 #10
Source File: MutablePersistenceUnitInfo.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public PersistenceUnitTransactionType getTransactionType() {
	if (this.transactionType != null) {
		return this.transactionType;
	}
	else {
		return (this.jtaDataSource != null ?
				PersistenceUnitTransactionType.JTA : PersistenceUnitTransactionType.RESOURCE_LOCAL);
	}
}
 
Example #11
Source File: ReloadableEntityManagerFactory.java    From tomee with Apache License 2.0 5 votes vote down vote up
public synchronized void setTransactionType(final PersistenceUnitTransactionType type) {
    final PersistenceUnitInfoImpl info = entityManagerFactoryCallable.getUnitInfo();
    info.setTransactionType(type);

    final Properties properties = entityManagerFactoryCallable.getUnitInfo().getProperties();
    if (properties.containsKey(JAVAX_PERSISTENCE_TRANSACTION_TYPE)) {
        properties.setProperty(JAVAX_PERSISTENCE_TRANSACTION_TYPE, type.name());
    }
}
 
Example #12
Source File: PersistenceUnitInfoImpl.java    From hibernate-master-class with Apache License 2.0 5 votes vote down vote up
public static PersistenceUnitInfoImpl newJTAPersistenceUnitInfo(
        String persistenceUnitName, List<String> mappingFileNames, Properties properties, DataSource jtaDataSource) {
    PersistenceUnitInfoImpl persistenceUnitInfo = new PersistenceUnitInfoImpl(
        persistenceUnitName, mappingFileNames, properties
    );
    persistenceUnitInfo.jtaDataSource = jtaDataSource;
    persistenceUnitInfo.nonJtaDataSource = null;
    persistenceUnitInfo.transactionType = PersistenceUnitTransactionType.JTA;
    return persistenceUnitInfo;
}
 
Example #13
Source File: PersistenceXmlParsingTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testExample4() throws Exception {
	SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
	DataSource ds = new DriverManagerDataSource();
	builder.bind("java:comp/env/jdbc/MyDB", ds);

	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-example4.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals(1, info.length);
	assertEquals("OrderManagement4", info[0].getPersistenceUnitName());

	assertEquals(1, info[0].getMappingFileNames().size());
	assertEquals("order-mappings.xml", info[0].getMappingFileNames().get(0));

	assertEquals(3, info[0].getManagedClassNames().size());
	assertEquals("com.acme.Order", info[0].getManagedClassNames().get(0));
	assertEquals("com.acme.Customer", info[0].getManagedClassNames().get(1));
	assertEquals("com.acme.Item", info[0].getManagedClassNames().get(2));

	assertTrue("Exclude unlisted should be true when no value.", info[0].excludeUnlistedClasses());

	assertSame(PersistenceUnitTransactionType.RESOURCE_LOCAL, info[0].getTransactionType());
	assertEquals(0, info[0].getProperties().keySet().size());

	builder.clear();
}
 
Example #14
Source File: MutablePersistenceUnitInfo.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public PersistenceUnitTransactionType getTransactionType() {
	if (this.transactionType != null) {
		return this.transactionType;
	}
	else {
		return (this.jtaDataSource != null ?
				PersistenceUnitTransactionType.JTA : PersistenceUnitTransactionType.RESOURCE_LOCAL);
	}
}
 
Example #15
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 #16
Source File: MutablePersistenceUnitInfo.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public PersistenceUnitTransactionType getTransactionType() {
	if (this.transactionType != null) {
		return this.transactionType;
	}
	else {
		return (this.jtaDataSource != null ?
				PersistenceUnitTransactionType.JTA : PersistenceUnitTransactionType.RESOURCE_LOCAL);
	}
}
 
Example #17
Source File: Bootstrap.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Intended for use in Hibernate tests
 *
 * @param persistenceXmlUrl The URL to a persistence.xml
 * @param persistenceUnitName The name of the persistence-unit to parse
 * @param integration setting overrides
 *
 * @return The EMFB
 */
public static EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(
		URL persistenceXmlUrl,
		String persistenceUnitName,
		PersistenceUnitTransactionType transactionType,
		Map integration) {
	;
	return new EntityManagerFactoryBuilderImpl(
			PersistenceXmlParser.parse( persistenceXmlUrl, transactionType, integration ).get( persistenceUnitName ),
			integration
	);
}
 
Example #18
Source File: PersistenceXmlParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find all persistence-units from all accessible {@code META-INF/persistence.xml} resources
 *
 * @param integration The Map of integration settings
 *
 * @return List of descriptors for all discovered persistence-units.
 */
public static List<ParsedPersistenceXmlDescriptor> locatePersistenceUnits(Map integration) {
	final PersistenceXmlParser parser = new PersistenceXmlParser(
			ClassLoaderServiceImpl.fromConfigSettings( integration ),
			PersistenceUnitTransactionType.RESOURCE_LOCAL
	);
	parser.doResolve( integration );
	return new ArrayList<>( parser.persistenceUnits.values() );
}
 
Example #19
Source File: PersistenceXmlParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static PersistenceUnitTransactionType parseTransactionType(String value) {
	if ( StringHelper.isEmpty( value ) ) {
		return null;
	}
	else if ( value.equalsIgnoreCase( "JTA" ) ) {
		return PersistenceUnitTransactionType.JTA;
	}
	else if ( value.equalsIgnoreCase( "RESOURCE_LOCAL" ) ) {
		return PersistenceUnitTransactionType.RESOURCE_LOCAL;
	}
	else {
		throw new PersistenceException( "Unknown persistence unit transaction type : " + value );
	}
}
 
Example #20
Source File: PersistenceUnitInfoImpl.java    From hibernate-types with Apache License 2.0 4 votes vote down vote up
public PersistenceUnitInfoImpl setNonJtaDataSource(DataSource nonJtaDataSource) {
    this.nonJtaDataSource = nonJtaDataSource;
    this.jtaDataSource = null;
    transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
    return this;
}
 
Example #21
Source File: PersistenceUnitInfoImpl.java    From high-performance-java-persistence with Apache License 2.0 4 votes vote down vote up
public PersistenceUnitInfoImpl setNonJtaDataSource(DataSource nonJtaDataSource) {
    this.nonJtaDataSource = nonJtaDataSource;
    this.jtaDataSource = null;
    transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
    return this;
}
 
Example #22
Source File: UnenhancedTest.java    From tomee with Apache License 2.0 4 votes vote down vote up
public void testEnhancedComplexIdResourceLocal() throws Exception {
    runTest("complexId", PersistenceUnitTransactionType.RESOURCE_LOCAL, true);
}
 
Example #23
Source File: CustomDescriptor.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public PersistenceUnitTransactionType getTransactionType() {
	return descriptor.getTransactionType();
}
 
Example #24
Source File: UnenhancedTest.java    From tomee with Apache License 2.0 4 votes vote down vote up
public void XtestUnenhancedCollectionJta() throws Exception {
    runTest("collection", PersistenceUnitTransactionType.JTA, false);
}
 
Example #25
Source File: PersistenceUnitInfoImpl.java    From hibernate-types with Apache License 2.0 4 votes vote down vote up
@Override
public PersistenceUnitTransactionType getTransactionType() {
    return transactionType;
}
 
Example #26
Source File: PersistenceUnitInfoImpl.java    From hibernate-types with Apache License 2.0 4 votes vote down vote up
public PersistenceUnitInfoImpl setNonJtaDataSource(DataSource nonJtaDataSource) {
    this.nonJtaDataSource = nonJtaDataSource;
    this.jtaDataSource = null;
    transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
    return this;
}
 
Example #27
Source File: PersistenceUnitInfoImpl.java    From hibernate-types with Apache License 2.0 4 votes vote down vote up
@Override
public PersistenceUnitTransactionType getTransactionType() {
    return transactionType;
}
 
Example #28
Source File: PersistenceUnitInfoImpl.java    From high-performance-java-persistence with Apache License 2.0 4 votes vote down vote up
public PersistenceUnitInfoImpl setJtaDataSource(DataSource jtaDataSource) {
    this.jtaDataSource = jtaDataSource;
    this.nonJtaDataSource = null;
    transactionType = PersistenceUnitTransactionType.JTA;
    return this;
}
 
Example #29
Source File: PersistenceUnitInfoImpl.java    From hibernate-types with Apache License 2.0 4 votes vote down vote up
public PersistenceUnitInfoImpl setJtaDataSource(DataSource jtaDataSource) {
    this.jtaDataSource = jtaDataSource;
    this.nonJtaDataSource = null;
    transactionType = PersistenceUnitTransactionType.JTA;
    return this;
}
 
Example #30
Source File: UnenhancedTest.java    From tomee with Apache License 2.0 4 votes vote down vote up
public void testEnhancedComplexIdSubclassJta() throws Exception {
    runTest("complexIdSubclass", PersistenceUnitTransactionType.JTA, true);
}