下面列出了怎么用javax.persistence.SynchronizationType的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
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);
}
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);
}
@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);
}
@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);
}
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);
}
}
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);
}
@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;
}
@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;
}
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;
}
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;
}
@Override
public EntityManager createEntityManager(SynchronizationType synchronizationType) {
return null;
}
@Override
public EntityManager createEntityManager(SynchronizationType synchronizationType, Map map) {
return null;
}
@Override
public EntityManager createEntityManager(SynchronizationType synchronizationType) {
return delegate.createEntityManager( synchronizationType );
}
@Override
public EntityManager createEntityManager(SynchronizationType synchronizationType, Map map) {
return delegate.createEntityManager( synchronizationType, map );
}
@Override
public Session createEntityManager() {
validateNotClosed();
return buildEntityManager( SynchronizationType.SYNCHRONIZED, Collections.emptyMap() );
}
@Override
public Session createEntityManager(Map map) {
validateNotClosed();
return buildEntityManager( SynchronizationType.SYNCHRONIZED, map );
}
@Override
public Session createEntityManager(SynchronizationType synchronizationType) {
validateNotClosed();
errorIfResourceLocalDueToExplicitSynchronizationType();
return buildEntityManager( synchronizationType, Collections.emptyMap() );
}
@Override
public Session createEntityManager(SynchronizationType synchronizationType, Map map) {
validateNotClosed();
errorIfResourceLocalDueToExplicitSynchronizationType();
return buildEntityManager( synchronizationType, map );
}
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;
}
UnitLiteral(final String name, final SynchronizationType synchronization) {
this.name = name;
this.synchronization = synchronization;
}
@Override
public SynchronizationType synchronization() {
return synchronization;
}
private UnitKey(final String unitName, final SynchronizationType synchronizationType) {
this.unitName = unitName;
this.synchronizationType = synchronizationType;
this.hash = 31 * unitName.hashCode() + synchronizationType.hashCode();
}
public EntityManager createEntityManager(SynchronizationType st) {
throw new UnsupportedOperationException("Not supported.");
}
public EntityManager createEntityManager(SynchronizationType st, Map map) {
throw new UnsupportedOperationException("Not supported.");
}
public PersistenceContextResourceFactory(String unitName, EntityManagerFactory emf, TransactionType transactionType, SynchronizationType sync) {
this.unitName = unitName;
this.emf = emf;
this.sync = sync;
this.transactionType = transactionType;
}
@Override public EntityManager createEntityManager(SynchronizationType synchronizationType) {
return null;
}
@Override public EntityManager createEntityManager(SynchronizationType synchronizationType, Map map) {
return null;
}
@Override
public EntityManager createEntityManager( SynchronizationType st )
{
// TODO Auto-generated method stub
return null;
}
@Override
public EntityManager createEntityManager( SynchronizationType st, Map map )
{
// TODO Auto-generated method stub
return null;
}