javax.persistence.SynchronizationType Java Examples

The following examples show how to use javax.persistence.SynchronizationType. 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: QuarkusJpaConnectionProviderFactory.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public JpaConnectionProvider create(KeycloakSession session) {
    logger.trace("Create QuarkusJpaConnectionProvider");
    EntityManager em;
    if (!jtaEnabled) {
        logger.trace("enlisting EntityManager in JpaKeycloakTransaction");
        em = emf.createEntityManager();
        try {
            SessionImpl.class.cast(em).connection().setAutoCommit(false);
        } catch (SQLException cause) {
            throw new RuntimeException(cause);
        }
    } else {

        em = emf.createEntityManager(SynchronizationType.SYNCHRONIZED);
    }
    em = PersistenceExceptionConverter.create(em);
    if (!jtaEnabled) session.getTransactionManager().enlist(new JpaKeycloakTransaction(em));
    return new DefaultJpaConnectionProvider(em);
}
 
Example #2
Source File: EntityManagerBean.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
void init(final PersistenceUnitInfo info, final BeanManager bm) {
    final PersistenceProvider provider;
    try {
        provider = PersistenceProvider.class.cast(
                Thread.currentThread().getContextClassLoader().loadClass(info.getPersistenceProviderClassName()).newInstance());
    } catch (final InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw new IllegalArgumentException("Bad provider: " + info.getPersistenceProviderClassName());
    }
    final EntityManagerFactory factory = provider.createContainerEntityManagerFactory(info, new HashMap() {{
        put("javax.persistence.bean.manager", bm);
        if (ValidationMode.NONE != info.getValidationMode()) {
            ofNullable(findValidatorFactory(bm)).ifPresent(factory -> put("javax.persistence.validation.factory", factory));
        }
    }});
    instanceFactory = synchronization == SynchronizationType.SYNCHRONIZED ? factory::createEntityManager : () -> factory.createEntityManager(synchronization);
}
 
Example #3
Source File: JtaEntityManager.java    From tomee with Apache License 2.0 6 votes vote down vote up
public JtaEntityManager(final String unitName, final JtaEntityManagerRegistry registry, final EntityManagerFactory entityManagerFactory,
                        final Map properties, final boolean extended, final String synchronizationType) {
    if (registry == null) {
        throw new NullPointerException("registry is null");
    }
    if (entityManagerFactory == null) {
        throw new NullPointerException("entityManagerFactory is null");
    }
    this.unitName = unitName;
    this.registry = registry;
    this.entityManagerFactory = entityManagerFactory;
    this.properties = properties;
    this.extended = extended;
    this.synchronizationType = !isJPA21(entityManagerFactory) || synchronizationType == null ?
            null : SynchronizationType.valueOf(synchronizationType.toUpperCase(Locale.ENGLISH));
    final String globalTimerConfig = SystemInstance.get().getProperty("openejb.jpa.timer");
    final Object localTimerConfig = properties == null ? null : properties.get("openejb.jpa.timer");
    this.timer = localTimerConfig == null ? (globalTimerConfig == null || Boolean.parseBoolean(globalTimerConfig)) : Boolean.parseBoolean(localTimerConfig.toString());
    logger = unitName == null ? baseLogger : baseLogger.getChildLogger(unitName);
    final String wrapConfig = ReloadableEntityManagerFactory.class.isInstance(entityManagerFactory) ?
            ReloadableEntityManagerFactory.class.cast(entityManagerFactory).getUnitProperties().getProperty("openejb.jpa.query.wrap-no-tx", "true") : "true";
    this.wrapNoTxQueries = wrapConfig == null || "true".equalsIgnoreCase(wrapConfig);
}
 
Example #4
Source File: SessionFactoryImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private Session buildEntityManager(SynchronizationType synchronizationType, Map map) {
	assert !isClosed;

	SessionBuilderImplementor builder = withOptions();
	if ( synchronizationType == SynchronizationType.SYNCHRONIZED ) {
		builder.autoJoinTransactions( true );
	}
	else {
		builder.autoJoinTransactions( false );
	}

	final Session session = builder.openSession();
	if ( map != null ) {
		map.keySet().forEach( key -> {
			if ( key instanceof String ) {
				session.setProperty( (String) key, map.get( key ) );
			}
		} );
	}
	return session;
}
 
Example #5
Source File: DefaultJpaConnectionProviderFactory.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public JpaConnectionProvider create(KeycloakSession session) {
    logger.trace("Create JpaConnectionProvider");
    lazyInit(session);

    EntityManager em;
    if (!jtaEnabled) {
        logger.trace("enlisting EntityManager in JpaKeycloakTransaction");
        em = emf.createEntityManager();
    } else {

        em = emf.createEntityManager(SynchronizationType.SYNCHRONIZED);
    }
    em = PersistenceExceptionConverter.create(em);
    if (!jtaEnabled) session.getTransactionManager().enlist(new JpaKeycloakTransaction(em));
    return new DefaultJpaConnectionProvider(em);
}
 
Example #6
Source File: StatefulContainer.java    From tomee with Apache License 2.0 5 votes vote down vote up
private Index<EntityManagerFactory, JtaEntityManagerRegistry.EntityManagerTracker> createEntityManagers(final BeanContext beanContext) {
    // create the extended entity managers
    final Index<EntityManagerFactory, BeanContext.EntityManagerConfiguration> factories = beanContext.getExtendedEntityManagerFactories();
    Index<EntityManagerFactory, JtaEntityManagerRegistry.EntityManagerTracker> entityManagers = null;
    if (factories != null && factories.size() > 0) {
        entityManagers = new Index<>(new ArrayList<>(factories.keySet()));
        for (final Map.Entry<EntityManagerFactory, BeanContext.EntityManagerConfiguration> entry : factories.entrySet()) {
            final EntityManagerFactory entityManagerFactory = entry.getKey();

            JtaEntityManagerRegistry.EntityManagerTracker entityManagerTracker = entityManagerRegistry.getInheritedEntityManager(entityManagerFactory);
            final EntityManager entityManager;
            if (entityManagerTracker == null) {
                final Map properties = entry.getValue().getProperties();
                final SynchronizationType synchronizationType = entry.getValue().getSynchronizationType();
                if (synchronizationType != null) {
                    if (properties != null) {
                        entityManager = entityManagerFactory.createEntityManager(synchronizationType, properties);
                    } else {
                        entityManager = entityManagerFactory.createEntityManager(synchronizationType);
                    }
                } else if (properties != null) {
                    entityManager = entityManagerFactory.createEntityManager(properties);
                } else {
                    entityManager = entityManagerFactory.createEntityManager();
                }
                entityManagerTracker = new JtaEntityManagerRegistry.EntityManagerTracker(entityManager, synchronizationType != SynchronizationType.UNSYNCHRONIZED);
            } else {
                entityManagerTracker.incCounter();
            }
            entityManagers.put(entityManagerFactory, entityManagerTracker);
        }
    }
    return entityManagers;
}
 
Example #7
Source File: ManagedContainer.java    From tomee with Apache License 2.0 5 votes vote down vote up
private Index<EntityManagerFactory, JtaEntityManagerRegistry.EntityManagerTracker> createEntityManagers(final BeanContext beanContext) {
    // create the extended entity managers
    final Index<EntityManagerFactory, BeanContext.EntityManagerConfiguration> factories = beanContext.getExtendedEntityManagerFactories();
    Index<EntityManagerFactory, JtaEntityManagerRegistry.EntityManagerTracker> entityManagers = null;
    if (factories != null && factories.size() > 0) {
        entityManagers = new Index<>(new ArrayList<>(factories.keySet()));
        for (final Map.Entry<EntityManagerFactory, BeanContext.EntityManagerConfiguration> entry : factories.entrySet()) {
            final EntityManagerFactory entityManagerFactory = entry.getKey();

            JtaEntityManagerRegistry.EntityManagerTracker entityManagerTracker = entityManagerRegistry.getInheritedEntityManager(entityManagerFactory);
            final EntityManager entityManager;
            if (entityManagerTracker == null) {
                final SynchronizationType synchronizationType = entry.getValue().getSynchronizationType();
                final Map properties = entry.getValue().getProperties();
                if (synchronizationType != null) {
                    if (properties != null) {
                        entityManager = entityManagerFactory.createEntityManager(synchronizationType, properties);
                    } else {
                        entityManager = entityManagerFactory.createEntityManager(synchronizationType);
                    }
                } else if (properties != null) {
                    entityManager = entityManagerFactory.createEntityManager(properties);
                } else {
                    entityManager = entityManagerFactory.createEntityManager();
                }
                entityManagerTracker = new JtaEntityManagerRegistry.EntityManagerTracker(entityManager, synchronizationType != SynchronizationType.UNSYNCHRONIZED);
            } else {
                entityManagerTracker.incCounter();
            }
            entityManagers.put(entityManagerFactory, entityManagerTracker);
        }
    }
    return entityManagers;
}
 
Example #8
Source File: ReloadableEntityManagerFactory.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public EntityManager createEntityManager(final SynchronizationType synchronizationType, final Map map) {
    EntityManager em;
    try {
        em = delegate().createEntityManager(synchronizationType, map);
    } catch (final LinkageError le) {
        em = delegate.createEntityManager(synchronizationType, map);
    }

    if (logCriteriaJpql) {
        return new QueryLogEntityManager(em, logCriteriaJpqlLevel);
    }
    return em;
}
 
Example #9
Source File: ReloadableEntityManagerFactory.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public EntityManager createEntityManager(final SynchronizationType synchronizationType) {
    EntityManager em;
    try {
        em = delegate().createEntityManager(synchronizationType);
    } catch (final LinkageError le) {
        em = delegate.createEntityManager(synchronizationType);
    }

    if (logCriteriaJpql) {
        return new QueryLogEntityManager(em, logCriteriaJpqlLevel);
    }
    return em;
}
 
Example #10
Source File: JtaEntityManager.java    From tomee with Apache License 2.0 5 votes vote down vote up
public static boolean isJPA21(final EntityManagerFactory entityManagerFactory) {
    return ReloadableEntityManagerFactory.class.isInstance(entityManagerFactory) ?
            hasMethod(
                    ReloadableEntityManagerFactory.class.cast(entityManagerFactory).getEntityManagerFactoryCallable().getProvider(),
                    "generateSchema", String.class, Map.class)
            : hasMethod(entityManagerFactory.getClass(), "createEntityManager", SynchronizationType.class);
}
 
Example #11
Source File: DatabaseReadOnlyNetwork.java    From BotLibre with Eclipse Public License 1.0 5 votes vote down vote up
public DatabaseReadOnlyNetwork(EntityManager entityManager, boolean isShortTerm) {
	super(new EntityManagerImpl(entityManager.unwrap(ServerSession.class), SynchronizationType.UNSYNCHRONIZED), isShortTerm);
	ServerSession server = entityManager.unwrap(ServerSession.class);
	if (!server.getProperties().containsKey("network")) {
		server.setProperty("network", this);
	}
}
 
Example #12
Source File: PersistenceContextResourceFactory.java    From kumuluzee with MIT License 4 votes vote down vote up
public PersistenceContextResourceFactory(String unitName, EntityManagerFactory emf, TransactionType transactionType, SynchronizationType sync) {
    this.unitName = unitName;
    this.emf = emf;
    this.sync = sync;
    this.transactionType = transactionType;
}
 
Example #13
Source File: BeanContext.java    From tomee with Apache License 2.0 4 votes vote down vote up
public SynchronizationType getSynchronizationType() {
    return synchronizationType;
}
 
Example #14
Source File: BeanContext.java    From tomee with Apache License 2.0 4 votes vote down vote up
public EntityManagerConfiguration(final Map properties, final SynchronizationType synchronizationType) {
    this.properties = properties;
    this.synchronizationType = synchronizationType;
}
 
Example #15
Source File: TestEntityManagerFactory.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public EntityManager createEntityManager( SynchronizationType st, Map map )
{
    // TODO Auto-generated method stub
    return null;
}
 
Example #16
Source File: TestEntityManagerFactory.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public EntityManager createEntityManager( SynchronizationType st )
{
    // TODO Auto-generated method stub
    return null;
}
 
Example #17
Source File: CacheHibernateStoreFactorySelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
@Override public EntityManager createEntityManager(SynchronizationType synchronizationType, Map map) {
    return null;
}
 
Example #18
Source File: CacheHibernateStoreFactorySelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
@Override public EntityManager createEntityManager(SynchronizationType synchronizationType) {
    return null;
}
 
Example #19
Source File: LegacySessionFactory.java    From judgels with GNU General Public License v2.0 4 votes vote down vote up
@Override
public EntityManager createEntityManager(SynchronizationType synchronizationType) {
    return null;
}
 
Example #20
Source File: MockStockPriceEntityManagerFactory.java    From training with MIT License 4 votes vote down vote up
public EntityManager createEntityManager(SynchronizationType st, Map map) {
    throw new UnsupportedOperationException("Not supported.");
}
 
Example #21
Source File: MockStockPriceEntityManagerFactory.java    From training with MIT License 4 votes vote down vote up
public EntityManager createEntityManager(SynchronizationType st) {
    throw new UnsupportedOperationException("Not supported.");
}
 
Example #22
Source File: JpaExtension.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
private UnitKey(final String unitName, final SynchronizationType synchronizationType) {
    this.unitName = unitName;
    this.synchronizationType = synchronizationType;
    this.hash = 31 * unitName.hashCode() + synchronizationType.hashCode();
}
 
Example #23
Source File: UnitLiteral.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
@Override
public SynchronizationType synchronization() {
    return synchronization;
}
 
Example #24
Source File: UnitLiteral.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
UnitLiteral(final String name, final SynchronizationType synchronization) {
    this.name = name;
    this.synchronization = synchronization;
}
 
Example #25
Source File: EntityManagerBean.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
EntityManagerBean(final EntityManagerContext context, final String name, final SynchronizationType synchronization) {
    this.entityManagerContext = context;
    this.qualifiers.addAll(asList(new UnitLiteral(name, synchronization), AnyLiteral.INSTANCE));
    this.id = "meecrowave::jpa::entitymanager::" + name + "/" + synchronization.name();
    this.synchronization = synchronization;
}
 
Example #26
Source File: SessionFactoryImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Session createEntityManager(SynchronizationType synchronizationType, Map map) {
	validateNotClosed();
	errorIfResourceLocalDueToExplicitSynchronizationType();
	return buildEntityManager( synchronizationType, map );
}
 
Example #27
Source File: SessionFactoryImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Session createEntityManager(SynchronizationType synchronizationType) {
	validateNotClosed();
	errorIfResourceLocalDueToExplicitSynchronizationType();
	return buildEntityManager( synchronizationType, Collections.emptyMap() );
}
 
Example #28
Source File: SessionFactoryImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Session createEntityManager(Map map) {
	validateNotClosed();
	return buildEntityManager( SynchronizationType.SYNCHRONIZED, map );
}
 
Example #29
Source File: SessionFactoryImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Session createEntityManager() {
	validateNotClosed();
	return buildEntityManager( SynchronizationType.SYNCHRONIZED, Collections.emptyMap() );
}
 
Example #30
Source File: SessionFactoryDelegatingImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public EntityManager createEntityManager(SynchronizationType synchronizationType, Map map) {
	return delegate.createEntityManager( synchronizationType, map );
}