Example usage for org.springframework.transaction.support TransactionSynchronizationManager hasResource

List of usage examples for org.springframework.transaction.support TransactionSynchronizationManager hasResource

Introduction

In this page you can find the example usage for org.springframework.transaction.support TransactionSynchronizationManager hasResource.

Prototype

public static boolean hasResource(Object key) 

Source Link

Document

Check if there is a resource for the given key bound to the current thread.

Usage

From source file:podd.dataaccess.hibernate.HibernateSessionHelper.java

public static void openSession(SessionFactory sessionFactory) {
    // To avoid LazyInitializationException       
    if (!TransactionSynchronizationManager.hasResource(sessionFactory)) {
        Session session = SessionFactoryUtils.getSession(sessionFactory, true);
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
    }//from ww  w. j  a  v  a2 s. co m
}

From source file:hsa.awp.common.util.EntityManagerUtil.java

/**
 * Finds the current {@link EntityManager} which is bound to the current Thread. If there is no {@link EntityManager} mapped,
 * <code>null</code> will be returned.
 *
 * @return the bound {@link EntityManager} or <code>null</code> if no one can be found.
 *///from   ww  w  .j av a 2  s . c  o  m
public static EntityManager getEntityManagerFromThread() {

    if (TransactionSynchronizationManager.hasResource(emf)) {
        EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.getResource(emf);
        return emHolder.getEntityManager();
    } else {
        return null;
    }
}

From source file:com.mobileman.filter.OpenSessionFilter.java

public static void setWebApplicationContext(WebApplicationContext webApplicationContext) {
    OpenSessionFilter.webApplicationContext = webApplicationContext;

    // initialize session...
    SessionFactory sessionFactory = lookupSessionFactory();

    if (sessionFactory != null) {
        if (isSingleSession()) {
            // single session mode
            if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
            } else {
                Session session = getSession(sessionFactory);
                TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
            }//  w  w  w  . j a  v a  2  s .  co  m
        } else {
            // deferred close mode
            if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) {
            } else {
                SessionFactoryUtils.initDeferredClose(sessionFactory);
            }
        }
    }
}

From source file:org.csc.phynixx.spring.jta.JtaConnectionHolderSupportAspect.java

@Before("@annotation(org.csc.phynixx.spring.jta.JtaConnectionHolderSupport)")
public void before() throws Throwable {

    System.out.println("Actual Transaction Active for JtaConnectionHolderSynchronization"
            + TransactionSynchronizationManager.isActualTransactionActive());

    if (!TransactionSynchronizationManager.isActualTransactionActive()) {
        return;/*from  ww w.j av  a 2 s.  co  m*/
    }
    Object connectionFactory = this.getConnectionFactory();
    if (TransactionSynchronizationManager.hasResource(connectionFactory)) {
        return;
    }
    ResourceHolder connectionHolder = this.getConnectionHolder();
    TransactionSynchronizationManager.bindResource(connectionFactory, connectionHolder);

    /**
     * support die LifeCycle of then binding
     */
    JtaConnectionHolderSynchronization<H, K> synchronization = new JtaConnectionHolderSynchronization(
            connectionHolder, connectionFactory);
    TransactionSynchronizationManager.registerSynchronization(synchronization);

    // Hole aktuelle Connection

    System.out.println("*************************");
}

From source file:org.springextensions.neodatis.NeoDatisTransactionManagerTest.java

@Test
public void testTransactionCommit() throws Exception {
    final ODB odb = Mockito.mock(ODB.class);
    PlatformTransactionManager tm = new NeoDatisTransactionManager(odb);
    TransactionTemplate tmpl = new TransactionTemplate(tm);

    Assert.assertFalse("Should not have a resource", TransactionSynchronizationManager.hasResource(odb));
    Assert.assertFalse("There should no active synchronizations",
            TransactionSynchronizationManager.isSynchronizationActive());

    tmpl.execute(new TransactionCallbackWithoutResult() {
        @Override/* ww w .jav  a2  s  .c  om*/
        protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
            Assert.assertTrue(TransactionSynchronizationManager.hasResource(odb));
            NeoDatisTemplate neoDatisTemplate = new NeoDatisTemplate(odb);
            neoDatisTemplate.store(null);
        }
    });

    Assert.assertFalse("Should not have a resource", TransactionSynchronizationManager.hasResource(odb));
    Assert.assertFalse("There should no active synchronizations",
            TransactionSynchronizationManager.isSynchronizationActive());

    Mockito.verify(odb, Mockito.times(1)).commit();

}

From source file:corner.orm.hibernate.impl.SpringSessionManagerImpl.java

public SpringSessionManagerImpl(HibernateSessionSource source) {
    sessionFactory = source.getSessionFactory();// single session mode
    if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
        // Do not modify the Session: just set the participate flag.
        participate = true;//  ww  w  .  ja va  2  s  . c  om
    } else {
        logger.debug("Opening single Hibernate Session in OpenSessionInViewFilter");
        Session session = getSession(sessionFactory);
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
    }
}

From source file:org.springextensions.db4o.Db4oTransactionManagerTest.java

@Test
public void testTransactionCommit() throws Exception {
    final ExtObjectContainer container = mock(ExtObjectContainer.class);
    when(container.identity()).thenReturn(null);

    PlatformTransactionManager tm = new Db4oTransactionManager(container);
    TransactionTemplate tt = new TransactionTemplate(tm);

    Assert.assertTrue(!TransactionSynchronizationManager.hasResource(container), "Has no container");
    Assert.assertTrue(!TransactionSynchronizationManager.isSynchronizationActive(),
            "JTA synchronizations not active");

    tt.execute(new TransactionCallbackWithoutResult() {
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            Assert.assertTrue(TransactionSynchronizationManager.hasResource(container), "Has thread session");
            Db4oTemplate template = new Db4oTemplate(container);
            template.identity();/* w ww. j av  a 2s.c o m*/
        }
    });

    Assert.assertTrue(!TransactionSynchronizationManager.hasResource(container), "Has no container");
    Assert.assertTrue(!TransactionSynchronizationManager.isSynchronizationActive(),
            "JTA synchronizations not active");

    verify(container).commit();
}

From source file:org.openeos.services.ui.vaadin.internal.OpenSessionInViewListener.java

@Override
public void onTransactionStart(IUnoVaadinApplication application) {
    if (sessionFactory != null) {
        if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
            LOG.debug("Participating in existing open session.");
        } else {/*  w  w  w . j  av a  2  s .co  m*/
            LOG.debug("Opening session in View...");
            Session session = SessionFactoryUtils.openSession(sessionFactory);
            session.setFlushMode(FlushMode.MANUAL);
            TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
            tlSession.set(session);
        }
    }
}

From source file:org.parancoe.web.ParancoeOpenSessionInViewInterceptor.java

@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
    logger.debug("Opening session and beginning transaction");
    Session session = sessionFactory.openSession();
    session.beginTransaction();// w ww . j  a v a2 s  .  c o m
    if (!TransactionSynchronizationManager.hasResource(sessionFactory)) {
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
        TransactionSynchronizationManager.initSynchronization();
    }
    return true;
}

From source file:corner.orm.gae.impl.EntityManagerSourceImpl.java

public EntityManagerSourceImpl(@Inject @Symbol(SymbolConstants.PRODUCTION_MODE) boolean product,
        EntityManagerFactory entityManagerFactory, Logger logger, Delegate delegate) {
    this.entityManagerFactory = entityManagerFactory;
    this.logger = logger;
    if (!product) {
        ApiProxy.setEnvironmentForCurrentThread(new TestEnvironment());
        ApiProxy.setDelegate(delegate);//from ww  w  .j av a 2  s.c  om
    }

    if (TransactionSynchronizationManager.hasResource(entityManagerFactory)) {
        // Do not modify the Session: just set the participate flag.
        participate = true;
    } else {
        logger.debug("Opening single entity manager  in EntityManagerSourceImpl");
        EntityManager em = entityManagerFactory.createEntityManager();
        TransactionSynchronizationManager.bindResource(entityManagerFactory, new EntityManagerHolder(em));
    }

}