List of usage examples for org.springframework.transaction.support TransactionSynchronizationManager getResource
@Nullable public static Object getResource(Object key)
From source file:org.springframework.data.document.mongodb.MongoDbUtils.java
public static DB doGetDB(Mongo mongo, String databaseName, String username, char[] password, boolean allowCreate) { Assert.notNull(mongo, "No Mongo instance specified"); DbHolder dbHolder = (DbHolder) TransactionSynchronizationManager.getResource(mongo); if (dbHolder != null && !dbHolder.isEmpty()) { // pre-bound Mongo DB DB db = null;/* w w w . j a v a 2 s . c o m*/ if (TransactionSynchronizationManager.isSynchronizationActive() && dbHolder.doesNotHoldNonDefaultDB()) { // Spring transaction management is active -> db = dbHolder.getDB(); if (db != null && !dbHolder.isSynchronizedWithTransaction()) { LOGGER.debug("Registering Spring transaction synchronization for existing Mongo DB"); TransactionSynchronizationManager .registerSynchronization(new MongoSynchronization(dbHolder, mongo)); dbHolder.setSynchronizedWithTransaction(true); } } if (db != null) { return db; } } LOGGER.trace("Getting Mongo Database name=[" + databaseName + "]"); DB db = mongo.getDB(databaseName); boolean credentialsGiven = username != null && password != null; if (credentialsGiven && !db.isAuthenticated()) { //Note, can only authenticate once against the same com.mongodb.DB object. if (!db.authenticate(username, password)) { throw new CannotGetMongoDbConnectionException("Failed to authenticate to database [" + databaseName + "], username = [" + username + "], password = [" + new String(password) + "]", databaseName, username, password); } } // Use same Session for further Mongo 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 Hibernate Session"); DbHolder holderToUse = dbHolder; if (holderToUse == null) { holderToUse = new DbHolder(db); } else { holderToUse.addDB(db); } TransactionSynchronizationManager.registerSynchronization(new MongoSynchronization(holderToUse, mongo)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != dbHolder) { TransactionSynchronizationManager.bindResource(mongo, holderToUse); } } // Check whether we are allowed to return the DB. if (!allowCreate && !isDBTransactional(db, mongo)) { throw new IllegalStateException("No Mongo DB bound to thread, " + "and configuration does not allow creation of non-transactional one here"); } return db; }
From source file:org.springframework.data.document.mongodb.MongoDbUtils.java
/** * Return whether the given DB instance is transactional, that is, * bound to the current thread by Spring's transaction facilities. * * @param db the DB to check/*from w ww. j av a 2 s.co m*/ * @param mongo the Mongo instance that the DB was created with * (may be <code>null</code>) * @return whether the DB is transactional */ public static boolean isDBTransactional(DB db, Mongo mongo) { if (mongo == null) { return false; } DbHolder dbHolder = (DbHolder) TransactionSynchronizationManager.getResource(mongo); return (dbHolder != null && dbHolder.containsDB(db)); }
From source file:org.springframework.data.keyvalue.redis.core.RedisConnectionUtils.java
/** * Gets a Redis connection. Is aware of and will return any existing corresponding connections bound to the current thread, * for example when using a transaction manager. Will create a new Connection otherwise, if {@code allowCreate} is <tt>true</tt>. * /*from ww w .j a va 2 s.c om*/ * @param factory connection factory for creating the connection * @param allowCreate whether a new (unbound) connection should be created when no connection can be found for the current thread * @param bind binds the connection to the thread, in case one was created * @return an active Redis connection */ public static RedisConnection doGetConnection(RedisConnectionFactory factory, boolean allowCreate, boolean bind) { Assert.notNull(factory, "No RedisConnectionFactory specified"); RedisConnectionHolder connHolder = (RedisConnectionHolder) TransactionSynchronizationManager .getResource(factory); //TODO: investigate tx synchronization if (connHolder != null) return connHolder.getConnection(); if (!allowCreate) { throw new IllegalArgumentException("No connection found and allowCreate = false"); } if (log.isDebugEnabled()) log.debug("Opening RedisConnection"); RedisConnection conn = factory.getConnection(); boolean synchronizationActive = TransactionSynchronizationManager.isSynchronizationActive(); if (bind || synchronizationActive) { connHolder = new RedisConnectionHolder(conn); if (synchronizationActive) { TransactionSynchronizationManager .registerSynchronization(new RedisConnectionSynchronization(connHolder, factory, true)); } TransactionSynchronizationManager.bindResource(factory, connHolder); return connHolder.getConnection(); } return conn; }
From source file:org.springframework.data.keyvalue.redis.core.RedisConnectionUtils.java
/** * Return whether the given Redis connection is transactional, that is, bound to the current thread by Spring's transaction facilities. * /*from w ww . jav a 2 s.c om*/ * @param conn Redis connection to check * @param connFactory Redis connection factory that the connection was created with * @return whether the connection is transactional or not */ public static boolean isConnectionTransactional(RedisConnection conn, RedisConnectionFactory connFactory) { if (connFactory == null) { return false; } RedisConnectionHolder connHolder = (RedisConnectionHolder) TransactionSynchronizationManager .getResource(connFactory); return (connHolder != null && conn == connHolder.getConnection()); }
From source file:org.springframework.data.mongodb.core.MongoDbUtils.java
public static DB doGetDB(Mongo mongo, String databaseName, String username, char[] password, boolean allowCreate) { Assert.notNull(mongo, "No Mongo instance specified"); DbHolder dbHolder = (DbHolder) TransactionSynchronizationManager.getResource(mongo); if (dbHolder != null && !dbHolder.isEmpty()) { // pre-bound Mongo DB DB db = null;//from w w w . jav a2 s. c o m if (TransactionSynchronizationManager.isSynchronizationActive() && dbHolder.doesNotHoldNonDefaultDB()) { // Spring transaction management is active -> db = dbHolder.getDB(databaseName); if (db != null && !dbHolder.isSynchronizedWithTransaction()) { LOGGER.debug("Registering Spring transaction synchronization for existing Mongo DB"); TransactionSynchronizationManager .registerSynchronization(new MongoSynchronization(dbHolder, mongo)); dbHolder.setSynchronizedWithTransaction(true); } } if (db != null) { return db; } } LOGGER.trace("Getting Mongo Database name=[" + databaseName + "]"); DB db = mongo.getDB(databaseName); boolean credentialsGiven = username != null && password != null; if (credentialsGiven && !db.isAuthenticated()) { // Note, can only authenticate once against the same com.mongodb.DB object. synchronized (db) { if (!db.authenticate(username, password)) { throw new CannotGetMongoDbConnectionException( "Failed to authenticate to database [" + databaseName + "], username = [" + username + "], password = [" + new String(password) + "]", databaseName, username, password); } } } // Use same Session for further Mongo 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 Hibernate Session"); DbHolder holderToUse = dbHolder; if (holderToUse == null) { holderToUse = new DbHolder(databaseName, db); } else { holderToUse.addDB(databaseName, db); } TransactionSynchronizationManager.registerSynchronization(new MongoSynchronization(holderToUse, mongo)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != dbHolder) { TransactionSynchronizationManager.bindResource(mongo, holderToUse); } } // Check whether we are allowed to return the DB. if (!allowCreate && !isDBTransactional(db, mongo)) { throw new IllegalStateException("No Mongo DB bound to thread, " + "and configuration does not allow creation of non-transactional one here"); } return db; }
From source file:org.springframework.data.mongodb.core.MongoDbUtils.java
/** * Return whether the given DB instance is transactional, that is, bound to the current thread by Spring's transaction * facilities./* ww w.j a va 2s . c o m*/ * * @param db the DB to check * @param mongo the Mongo instance that the DB was created with (may be <code>null</code>) * @return whether the DB is transactional */ public static boolean isDBTransactional(DB db, Mongo mongo) { if (mongo == null) { return false; } DbHolder dbHolder = (DbHolder) TransactionSynchronizationManager.getResource(mongo); return dbHolder != null && dbHolder.containsDB(db); }
From source file:org.springframework.data.neo4j.transaction.SessionFactoryUtils.java
public static Session getSession(SessionFactory sessionFactory) throws IllegalStateException { Assert.notNull(sessionFactory, "No SessionFactory specified"); SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory); if (sessionHolder != null) { if (!sessionHolder.isSynchronizedWithTransaction() && TransactionSynchronizationManager.isSynchronizationActive()) { sessionHolder.setSynchronizedWithTransaction(true); TransactionSynchronizationManager .registerSynchronization(new SessionSynchronization(sessionHolder, sessionFactory, false)); }//from w ww .ja v a 2 s . c om return sessionHolder.getSession(); } if (!TransactionSynchronizationManager.isSynchronizationActive()) { return null; } Session session = sessionFactory.openSession(); if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { System.out.println("hitting read only txn."); } logger.debug("Registering transaction synchronization for Neo4j Session"); // Use same Session for further Neo4j actions within the transaction. // Thread object will get removed by synchronization at transaction completion. sessionHolder = new SessionHolder(session); sessionHolder.setSynchronizedWithTransaction(true); TransactionSynchronizationManager .registerSynchronization(new SessionSynchronization(sessionHolder, sessionFactory, true)); TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder); return session; }
From source file:org.springframework.data.redis.core.RedisConnectionUtils.java
/** * Gets a Redis connection. Is aware of and will return any existing corresponding connections bound to the current * thread, for example when using a transaction manager. Will create a new Connection otherwise, if * {@code allowCreate} is <tt>true</tt>. * // w w w .ja v a2 s . co m * @param factory connection factory for creating the connection * @param allowCreate whether a new (unbound) connection should be created when no connection can be found for the * current thread * @param bind binds the connection to the thread, in case one was created * @param enableTransactionSupport * @return an active Redis connection */ public static RedisConnection doGetConnection(RedisConnectionFactory factory, boolean allowCreate, boolean bind, boolean enableTransactionSupport) { Assert.notNull(factory, "No RedisConnectionFactory specified"); RedisConnectionHolder connHolder = (RedisConnectionHolder) TransactionSynchronizationManager .getResource(factory); if (connHolder != null) { if (enableTransactionSupport) { potentiallyRegisterTransactionSynchronisation(connHolder, factory); } return connHolder.getConnection(); } if (!allowCreate) { throw new IllegalArgumentException("No connection found and allowCreate = false"); } if (log.isDebugEnabled()) { log.debug("Opening RedisConnection"); } RedisConnection conn = factory.getConnection(); if (bind) { RedisConnection connectionToBind = conn; if (enableTransactionSupport && isActualNonReadonlyTransactionActive()) { connectionToBind = createConnectionProxy(conn, factory); } connHolder = new RedisConnectionHolder(connectionToBind); TransactionSynchronizationManager.bindResource(factory, connHolder); if (enableTransactionSupport) { potentiallyRegisterTransactionSynchronisation(connHolder, factory); } return connHolder.getConnection(); } return conn; }
From source file:org.springframework.data.redis.core.RedisConnectionUtils.java
/** * Closes the given connection, created via the given factory if not managed externally (i.e. not bound to the * thread)./* w w w. j av a 2 s.c om*/ * * @param conn the Redis connection to close * @param factory the Redis factory that the connection was created with */ public static void releaseConnection(RedisConnection conn, RedisConnectionFactory factory) { if (conn == null) { return; } RedisConnectionHolder connHolder = (RedisConnectionHolder) TransactionSynchronizationManager .getResource(factory); if (connHolder != null && connHolder.isTransactionSyncronisationActive()) { if (log.isDebugEnabled()) { log.debug("Redis Connection will be closed when transaction finished."); } return; } // release transactional/read-only and non-transactional/non-bound connections. // transactional connections for read-only transactions get no synchronizer registered if (isConnectionTransactional(conn, factory) && TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { unbindConnection(factory); } else if (!isConnectionTransactional(conn, factory)) { if (log.isDebugEnabled()) { log.debug("Closing Redis Connection"); } conn.close(); } }
From source file:org.springframework.data.redis.core.RedisConnectionUtils.java
/** * Return whether the given Redis connection is transactional, that is, bound to the current thread by Spring's * transaction facilities.// ww w.j a v a 2s. co m * * @param conn Redis connection to check * @param connFactory Redis connection factory that the connection was created with * @return whether the connection is transactional or not */ public static boolean isConnectionTransactional(RedisConnection conn, RedisConnectionFactory connFactory) { if (connFactory == null) { return false; } RedisConnectionHolder connHolder = (RedisConnectionHolder) TransactionSynchronizationManager .getResource(connFactory); return (connHolder != null && conn == connHolder.getConnection()); }