List of usage examples for org.springframework.transaction.support TransactionSynchronizationManager getResource
@Nullable public static Object getResource(Object key)
From source file:org.codehaus.groovy.grails.plugins.acegi.GrailsWebApplicationObjectSupport.java
/** * set up hibernate session// w w w. java2 s . com */ protected void setUpSession() { try { sessionFactory = (SessionFactory) getWebApplicationContext().getBean("sessionFactory"); if (TransactionSynchronizationManager.hasResource(sessionFactory)) { if (logger.isDebugEnabled()) logger.debug("Session already has transaction attached"); containerManagedSession = true; this.session = ((SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory)) .getSession(); } else { if (logger.isDebugEnabled()) logger.debug("Session does not have transaction attached... Creating new one"); containerManagedSession = false; session = SessionFactoryUtils.getSession(sessionFactory, true); SessionHolder sessionHolder = new SessionHolder(session); TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder); } } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); } }
From source file:corner.orm.gae.impl.EntityManagerSourceImpl.java
/** * @see lichen.common.services.jpa.EntityManagerSource#getEntityManager() *///w ww .j a v a2 s . co m @Override public EntityManager getEntityManager() { EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager .getResource(entityManagerFactory); return emHolder.getEntityManager(); }
From source file:org.grails.datastore.mapping.web.support.OpenSessionInViewInterceptor.java
protected boolean hasSessionBound() { return TransactionSynchronizationManager.getResource(getDatastore()) != null; }
From source file:com.github.rholder.spring.transaction.TransactionBindingSupport.java
/** * Get a unique identifier associated with each transaction of each thread. Null is returned if * no transaction is currently active./*from w w w. java2 s.c o m*/ * * @return Returns the transaction ID, or null if no transaction is present */ public static String getTransactionId() { /* * Go direct to the synchronizations as we don't want to register a resource if one doesn't exist. * This method is heavily used, so the simple Map lookup on the ThreadLocal is the fastest. */ TransactionSynchronizationImpl txnSynch = (TransactionSynchronizationImpl) TransactionSynchronizationManager .getResource(RESOURCE_KEY_TXN_SYNCH); if (txnSynch == null) { if (TransactionSynchronizationManager.isSynchronizationActive()) { // need to lazily register synchronizations return registerSynchronizations().getTransactionId(); } else { return null; // not in a transaction } } else { return txnSynch.getTransactionId(); } }
From source file:org.springextensions.db4o.Db4oTransactionManager.java
protected Object doGetTransaction() throws TransactionException { Db4oTransactionObject txObject = new Db4oTransactionObject(); ObjectContainerHolder containerHolder = (ObjectContainerHolder) TransactionSynchronizationManager .getResource(getObjectContainer()); txObject.setObjectContainerHolder(containerHolder); return txObject; }
From source file:com.avaje.ebean.springsupport.txn.SpringAwareJdbcTransactionManager.java
/** * Looks for a current Spring managed transaction and wraps/returns that as a Ebean transaction. * <p>/*ww w .j a va 2 s.c om*/ * Returns null if there is no current spring transaction (lazy loading outside a spring txn etc). * </p> */ public Object getCurrentTransaction() { // Get the current Spring ConnectionHolder associated to the current spring managed transaction ConnectionHolder holder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource); if (holder == null || !holder.isSynchronizedWithTransaction()) { // no current Spring transaction SpiTransaction currentEbeanTransaction = DefaultTransactionThreadLocal.get(serverName); if (currentEbeanTransaction != null) { // NOT expecting this so log WARNING String msg = "SpringTransaction - no current spring txn BUT using current Ebean one " + currentEbeanTransaction.getId(); logger.log(Level.WARNING, msg); } else if (logger.isLoggable(Level.FINEST)) { logger.log(Level.FINEST, "Spring Txn - no current transaction "); } return currentEbeanTransaction; } SpringTxnListener springTxnLister = getSpringTxnListener(); if (springTxnLister != null) { // we have already seen this transaction return springTxnLister.getTransaction(); } else { // This is a new spring transaction that we have not seen before. // "wrap" it in a SpringJdbcTransaction for use with Ebean SpringJdbcTransaction newTrans = new SpringJdbcTransaction(holder, transactionManager); // Create and register a Spring TransactionSynchronization for this transaction springTxnLister = createSpringTxnListener(newTrans); TransactionSynchronizationManager.registerSynchronization(springTxnLister); // also put in Ebean ThreadLocal DefaultTransactionThreadLocal.set(serverName, newTrans); return newTrans; } }
From source file:com._4dconcept.springframework.data.marklogic.datasource.ContentSourceUtils.java
/** * Actually obtain a XDBC Session from the given ContentSource. * Same as {@link #getSession}, but throwing the original XccException. * <p>Is aware of a corresponding Session bound to the current thread, for example * when using {@link ContentSourceTransactionManager}. Will bind a Session to the thread * if transaction synchronization is active (e.g. if in a JTA transaction). * <p>Directly accessed by {@link TransactionAwareContentSourceProxy}. * @param contentSource the ContentSource to obtain Sessions from * @return a XDBC Session from the given ContentSource * @throws XccException if thrown by XDBC methods * @see #doReleaseSession/*from ww w . j ava2s. co m*/ */ public static Session doGetSession(ContentSource contentSource) throws XccException { Assert.notNull(contentSource, "No ContentSource specified"); SessionHolder sesHolder = (SessionHolder) TransactionSynchronizationManager.getResource(contentSource); if (sesHolder != null && (sesHolder.hasSession() || sesHolder.isSynchronizedWithTransaction())) { sesHolder.requested(); if (!sesHolder.hasSession()) { logger.debug("Fetching resumed XDBC Session from ContentSource"); sesHolder.setSession(contentSource.newSession()); } return sesHolder.getSession(); } // Else we either got no holder or an empty thread-bound holder here. logger.debug("Fetching XDBC Session from ContentSource"); Session ses = contentSource.newSession(); if (TransactionSynchronizationManager.isSynchronizationActive()) { logger.debug("Registering transaction synchronization for XDBC Session"); // Use same Session for further XDBC actions within the transaction. // Thread-bound object will get removed by synchronization at transaction completion. SessionHolder holderToUse = sesHolder; if (holderToUse == null) { holderToUse = new SessionHolder(ses); } else { holderToUse.setSession(ses); } holderToUse.requested(); TransactionSynchronizationManager .registerSynchronization(new SessionSynchronization(holderToUse, contentSource)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != sesHolder) { TransactionSynchronizationManager.bindResource(contentSource, holderToUse); } } return ses; }
From source file:org.grails.datastore.mapping.core.DatastoreUtils.java
/** * Get a Datastore Session for the given Datastore. Is aware of and will * return any existing corresponding Session bound to the current thread, for * example when using {@link org.grails.datastore.mapping.transactions.DatastoreTransactionManager}. Will create a new * Session otherwise, if "allowCreate" is <code>true</code>. * * @param datastore Datastore to create the session with * Session on transaction synchronization (may be <code>null</code>) * @param allowCreate whether a non-transactional Session should be created * when no transactional Session can be found for the current thread * @return the Datastore Session//from w ww. j a v a 2s. c om * @throws IllegalStateException if no thread-bound Session found and * "allowCreate" is <code>false</code> */ public static Session doGetSession(Datastore datastore, boolean allowCreate) { Assert.notNull(datastore, "No Datastore specified"); SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(datastore); if (sessionHolder != null && !sessionHolder.isEmpty()) { // pre-bound Datastore Session Session session; if (TransactionSynchronizationManager.isSynchronizationActive() && sessionHolder.doesNotHoldNonDefaultSession()) { // Spring transaction management is active -> // register pre-bound Session with it for transactional flushing. session = sessionHolder.getValidatedSession(); if (session != null && !sessionHolder.isSynchronizedWithTransaction()) { logger.debug("Registering Spring transaction synchronization for existing Datastore Session"); TransactionSynchronizationManager.registerSynchronization( new SpringSessionSynchronization(sessionHolder, datastore, false)); sessionHolder.setSynchronizedWithTransaction(true); } if (session != null) { return session; } } else { session = sessionHolder.getValidatedSession(); if (session != null) { return session; } } } logger.debug("Opening Datastore Session"); Session session = datastore.connect(); // Use same Session for further Datastore actions within the transaction. // Thread object will get removed by synchronization at transaction completion. if (TransactionSynchronizationManager.isSynchronizationActive()) { // We're within a Spring-managed transaction, possibly from JtaTransactionManager. logger.debug("Registering Spring transaction synchronization for new Datastore Session"); SessionHolder holderToUse = sessionHolder; if (holderToUse == null) { holderToUse = new SessionHolder(session); } else { holderToUse.addSession(session); } TransactionSynchronizationManager .registerSynchronization(new SpringSessionSynchronization(holderToUse, datastore, true)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != sessionHolder) { TransactionSynchronizationManager.bindResource(datastore, holderToUse); } } // Check whether we are allowed to return the Session. if (!allowCreate && !isSessionTransactional(session, datastore)) { closeSession(session); throw new IllegalStateException("No Datastore Session bound to thread, " + "and configuration does not allow creation of non-transactional one here"); } return session; }
From source file:org.openeos.hibernate.internal.SessionFactoryProxyHandler.java
private Object currentSession(Object proxy) { Object value = TransactionSynchronizationManager.getResource(proxy); if (value instanceof Session) { return (Session) value; } else if (value instanceof SessionHolder) { SessionHolder sessionHolder = (SessionHolder) value; Session session = sessionHolder.getSession(); if (TransactionSynchronizationManager.isSynchronizationActive() && !sessionHolder.isSynchronizedWithTransaction()) { throw new UnsupportedOperationException(); }/* w ww .j av a2 s .co m*/ return session; } else { throw new HibernateException("No Session found for current thread"); } }