Search in sources :

Example 31 with JpaTransactionManager

use of org.springframework.orm.jpa.JpaTransactionManager in project workbench by all-of-us.

the class TestJpaConfig method transactionManager.

@Bean
public JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
    final JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory);
    return transactionManager;
}
Also used : JpaTransactionManager(org.springframework.orm.jpa.JpaTransactionManager) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 32 with JpaTransactionManager

use of org.springframework.orm.jpa.JpaTransactionManager in project modesti by jlsalmon.

the class JpaConfig method coreTransactionManager.

@Bean
@Primary
public JpaTransactionManager coreTransactionManager(EntityManagerFactoryBuilder builder) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(coreEntityManagerFactory(builder).getObject());
    return transactionManager;
}
Also used : JpaTransactionManager(org.springframework.orm.jpa.JpaTransactionManager) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 33 with JpaTransactionManager

use of org.springframework.orm.jpa.JpaTransactionManager in project BroadleafCommerce by BroadleafCommerce.

the class AdminTestHelper method startView.

/**
 * Useful to start an EntityManager-In-View. This allows test operations that want to read directly from the database
 * to work without lazy init exceptions, etc... This is not needed for MockMVC#perform operations, since those
 * requests will include the OpenEntityManagerInView filter as part of their flow. At the completion of the test
 * operation, the {@link #endView()} method should be called to end the scope of the view.
 * </p>
 * This view scope is also aware of nested views against the same persistence unit, so you don't need to worry
 * about coding carefully to avoid nesting calls to startView.
 *
 * @param siteId
 * @param sandBoxName
 */
public void startView(Long siteId, String sandBoxName) {
    EntityManagerFactory emf = ((JpaTransactionManager) transUtil.getTransactionManager()).getEntityManagerFactory();
    boolean isEntityManagerInView = TransactionSynchronizationManager.hasResource(emf);
    if (!isEntityManagerInView) {
        EntityManager em = emf.createEntityManager();
        em.clear();
        EntityManagerHolder emHolder = new EntityManagerHolder(em);
        TransactionSynchronizationManager.bindResource(emf, emHolder);
        Stack<String> stack = new Stack<>();
        TransactionSynchronizationManager.bindResource("emStack", stack);
        if (siteId != null) {
            Site site = siteService.retrievePersistentSiteById(siteId);
            ThreadLocalManager.remove();
            BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
            context.setSite(site);
            context.setDeployBehavior(DeployBehavior.CLONE_PARENT);
            context.setAdmin(true);
            if (!StringUtils.isEmpty(sandBoxName)) {
                SandBox sb = findSandBox(siteId, sandBoxName, false);
                context.setSandBox(sb);
            }
        }
    }
    Stack<String> stack = (Stack<String>) TransactionSynchronizationManager.getResource("emStack");
    RuntimeException trace = new RuntimeException();
    StringWriter writer = new StringWriter();
    PrintWriter pw = new PrintWriter(writer);
    trace.printStackTrace(pw);
    stack.push(writer.toString());
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) SandBox(org.broadleafcommerce.common.sandbox.domain.SandBox) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) JpaTransactionManager(org.springframework.orm.jpa.JpaTransactionManager) Stack(java.util.Stack) EntityManager(javax.persistence.EntityManager) StringWriter(java.io.StringWriter) EntityManagerFactory(javax.persistence.EntityManagerFactory) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder) PrintWriter(java.io.PrintWriter)

Example 34 with JpaTransactionManager

use of org.springframework.orm.jpa.JpaTransactionManager in project BroadleafCommerce by BroadleafCommerce.

the class AdminTestHelper method findSandBox.

private SandBox findSandBox(Long siteId, String sandBoxName, boolean initialize) {
    if (initialize) {
        startView(siteId, sandBoxName);
    }
    try {
        if (DEFAULT_SB.equals(sandBoxName)) {
            return getDefaultSandBox(siteId);
        } else {
            EntityManagerFactory emf = ((JpaTransactionManager) transUtil.getTransactionManager()).getEntityManagerFactory();
            EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.getResource(emf);
            return findSandBox(sandBoxName, emHolder.getEntityManager());
        }
    } finally {
        if (initialize) {
            endView();
        }
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) JpaTransactionManager(org.springframework.orm.jpa.JpaTransactionManager) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder)

Example 35 with JpaTransactionManager

use of org.springframework.orm.jpa.JpaTransactionManager in project BroadleafCommerce by BroadleafCommerce.

the class AdminTestHelper method endView.

/**
 * Complete the scope of the EntityManager-In-View operation. See {@link #startView(Long, String)}.
 */
public void endView() {
    EntityManagerFactory emf = ((JpaTransactionManager) transUtil.getTransactionManager()).getEntityManagerFactory();
    boolean isEntityManagerInView = TransactionSynchronizationManager.hasResource(emf);
    if (isEntityManagerInView) {
        Stack<Integer> stack = (Stack<Integer>) TransactionSynchronizationManager.getResource("emStack");
        if (stack.size() <= 1) {
            EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResource(emf);
            EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
            TransactionSynchronizationManager.unbindResource("emStack");
            ThreadLocalManager.remove();
        } else {
            stack.pop();
        }
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) JpaTransactionManager(org.springframework.orm.jpa.JpaTransactionManager) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder) Stack(java.util.Stack)

Aggregations

JpaTransactionManager (org.springframework.orm.jpa.JpaTransactionManager)70 Bean (org.springframework.context.annotation.Bean)54 LocalContainerEntityManagerFactoryBean (org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)54 Autowired (org.springframework.beans.factory.annotation.Autowired)12 EntityManagerFactory (javax.persistence.EntityManagerFactory)8 Test (org.junit.Test)5 Primary (org.springframework.context.annotation.Primary)5 EntityManagerHolder (org.springframework.orm.jpa.EntityManagerHolder)4 EntityManager (javax.persistence.EntityManager)3 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)3 Stack (java.util.Stack)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 ProtocolAdapterException (com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException)1 Iec61850CoreDbApiException (com.alliander.osgp.core.db.api.iec61850.exceptions.Iec61850CoreDbApiException)1 AugmentableQueryRepositoryFactoryBean (com.thinkbiganalytics.metadata.jpa.feed.AugmentableQueryRepositoryFactoryBean)1 LoggingDelegateTransactionManager (de.invesdwin.context.persistence.jpa.scanning.transaction.LoggingDelegateTransactionManager)1 EntityManager (jakarta.persistence.EntityManager)1 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)1 EntityTransaction (jakarta.persistence.EntityTransaction)1 PrintWriter (java.io.PrintWriter)1