本文整理匯總了Java中org.springframework.transaction.support.TransactionSynchronizationManager.isSynchronizationActive方法的典型用法代碼示例。如果您正苦於以下問題:Java TransactionSynchronizationManager.isSynchronizationActive方法的具體用法?Java TransactionSynchronizationManager.isSynchronizationActive怎麽用?Java TransactionSynchronizationManager.isSynchronizationActive使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.transaction.support.TransactionSynchronizationManager
的用法示例。
在下文中一共展示了TransactionSynchronizationManager.isSynchronizationActive方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getTransactionReadState
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
* @return Returns the read-write state of the current transaction
* @since 2.1.4
*/
public static TxnReadState getTransactionReadState()
{
if (!TransactionSynchronizationManager.isSynchronizationActive())
{
return TxnReadState.TXN_NONE;
}
// Find the read-write state of the txn
if (getResource(RESOURCE_KEY_TXN_COMPLETING) != null)
{
// Transaction is completing. For all intents and purposes, we are not in a transaction.
return TxnReadState.TXN_NONE;
}
else if (TransactionSynchronizationManager.isCurrentTransactionReadOnly())
{
return TxnReadState.TXN_READ_ONLY;
}
else
{
return TxnReadState.TXN_READ_WRITE;
}
}
示例2: intercept
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
public Object intercept(Invocation invocation) throws Throwable {
boolean synchronizationActive = TransactionSynchronizationManager.isSynchronizationActive();
//如果沒有事務
if (!synchronizationActive) {
Object[] objects = invocation.getArgs();
MappedStatement ms = (MappedStatement) objects[0];
DynamicDataSourceGlobal dynamicDataSourceGlobal;
if ((dynamicDataSourceGlobal = CACHE_MAP.get(ms.getId())) == null) {
dynamicDataSourceGlobal = getDynamicDataSource(ms, objects[1]);
LOGGER.warn("設置方法[{}] use [{}] Strategy, SqlCommandType [{}]..", ms.getId(), dynamicDataSourceGlobal.name(), ms.getSqlCommandType().name());
CACHE_MAP.put(ms.getId(), dynamicDataSourceGlobal);
}
DynamicDataSourceHolder.putDataSource(dynamicDataSourceGlobal);
}
return invocation.proceed();
}
示例3: getTransactionId
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
* Get the transaction identifier used to store it in the transaction map.
*
* @param tx Transaction
* @param storeRef StoreRef
* @return - the transaction id
*/
@SuppressWarnings("unchecked")
private String getTransactionId(Transaction tx, StoreRef storeRef)
{
if (tx instanceof SimpleTransaction)
{
SimpleTransaction simpleTx = (SimpleTransaction) tx;
return simpleTx.getGUID();
}
else if (TransactionSynchronizationManager.isSynchronizationActive())
{
Map<StoreRef, LuceneIndexer> indexers = (Map<StoreRef, LuceneIndexer>) AlfrescoTransactionSupport.getResource(indexersKey);
if (indexers != null)
{
LuceneIndexer indexer = indexers.get(storeRef);
if (indexer != null)
{
return indexer.getDeltaId();
}
}
}
return null;
}
示例4: getTransactionStartTime
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
* @return Returns the system time when the transaction started, or -1 if there is no current transaction.
*/
public static long getTransactionStartTime()
{
/*
* This method can be called outside of a transaction, so we can go direct to the synchronizations.
*/
TransactionSynchronizationImpl txnSynch =
(TransactionSynchronizationImpl) TransactionSynchronizationManager.getResource(RESOURCE_KEY_TXN_SYNCH);
if (txnSynch == null)
{
if (TransactionSynchronizationManager.isSynchronizationActive())
{
// need to lazily register synchronizations
return registerSynchronizations().getTransactionStartTime();
}
else
{
return -1; // not in a transaction
}
}
else
{
return txnSynch.getTransactionStartTime();
}
}
示例5: doGetConnection
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
* Actually obtain a CCI Connection from the given ConnectionFactory.
* Same as {@link #getConnection}, but throwing the original ResourceException.
* <p>Is aware of a corresponding Connection bound to the current thread, for example
* when using {@link CciLocalTransactionManager}. Will bind a Connection to the thread
* if transaction synchronization is active (e.g. if in a JTA transaction).
* <p>Directly accessed by {@link TransactionAwareConnectionFactoryProxy}.
* @param cf the ConnectionFactory to obtain Connection from
* @return a CCI Connection from the given ConnectionFactory
* @throws ResourceException if thrown by CCI API methods
* @see #doReleaseConnection
*/
public static Connection doGetConnection(ConnectionFactory cf) throws ResourceException {
Assert.notNull(cf, "No ConnectionFactory specified");
ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(cf);
if (conHolder != null) {
return conHolder.getConnection();
}
logger.debug("Opening CCI Connection");
Connection con = cf.getConnection();
if (TransactionSynchronizationManager.isSynchronizationActive()) {
logger.debug("Registering transaction synchronization for CCI Connection");
conHolder = new ConnectionHolder(con);
conHolder.setSynchronizedWithTransaction(true);
TransactionSynchronizationManager.registerSynchronization(new ConnectionSynchronization(conHolder, cf));
TransactionSynchronizationManager.bindResource(cf, conHolder);
}
return con;
}
示例6: getTxMap
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
* Returns a transactionally scoped Map that is used to provide repeatable lock store queries
* for a given NodeRef. If no transaction is present, then null is returned.
*
* @return Transactional Map or null if not available.
*/
protected Map<NodeRef, LockState> getTxMap()
{
if (!TransactionSynchronizationManager.isSynchronizationActive())
{
return null;
}
Map<NodeRef, LockState> map = TransactionalResourceHelper.getMap(getClass().getName()+".repeatableReadMap");
return map;
}
示例7: getTransactionId
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
* Get a unique identifier associated with each transaction of each thread. Null is returned if
* no transaction is currently active.
*
* @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();
}
}
示例8: registerSynchronizations
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
* Binds the Alfresco-specific to the transaction resources
*
* @return Returns the current or new synchronization implementation
*/
private static TransactionSynchronizationImpl registerSynchronizations()
{
/*
* No thread synchronization or locking required as the resources are all threadlocal
*/
if (!TransactionSynchronizationManager.isSynchronizationActive())
{
Thread currentThread = Thread.currentThread();
throw new AlfrescoRuntimeException("Transaction must be active and synchronization is required: " + currentThread);
}
TransactionSynchronizationImpl txnSynch =
(TransactionSynchronizationImpl) TransactionSynchronizationManager.getResource(RESOURCE_KEY_TXN_SYNCH);
if (txnSynch != null)
{
// synchronization already registered
return txnSynch;
}
// we need a unique ID for the transaction
String txnId = GUID.generate();
// register the synchronization
txnSynch = new TransactionSynchronizationImpl(txnId);
TransactionSynchronizationManager.registerSynchronization(txnSynch);
// register the resource that will ensure we don't duplication the synchronization
TransactionSynchronizationManager.bindResource(RESOURCE_KEY_TXN_SYNCH, txnSynch);
// done
if (logger.isDebugEnabled())
{
logger.debug("Bound txn synch: " + txnSynch);
}
return txnSynch;
}
示例9: intercept
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
public Object intercept(Invocation invocation) throws Throwable {
boolean synchronizationActive = TransactionSynchronizationManager.isSynchronizationActive();
if (!synchronizationActive) {
Object[] objects = invocation.getArgs();
MappedStatement ms = (MappedStatement) objects[0];
DynamicDataSourceType dynamicDataSourceType;
if ((dynamicDataSourceType = cacheMap.get(ms.getId())) == null) {
//讀方法
if (ms.getSqlCommandType().equals(SqlCommandType.SELECT)) {
//!selectKey 為自增id查詢主鍵(SELECT LAST_INSERT_ID() )方法,使用主庫
if (ms.getId().contains(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
dynamicDataSourceType = DynamicDataSourceType.WRITE;
} else {
BoundSql boundSql = ms.getSqlSource().getBoundSql(objects[1]);
String sql = boundSql.getSql().toLowerCase(Locale.CHINA).replaceAll("[\\t\\n\\r]", " ");
if (sql.matches(REGEX)) {
dynamicDataSourceType = DynamicDataSourceType.WRITE;
} else {
dynamicDataSourceType = DynamicDataSourceType.READ;
}
}
} else {
dynamicDataSourceType = DynamicDataSourceType.WRITE;
}
log.warn("設置方法[{}] use [{}] Strategy, SqlCommandType [{}]..", ms.getId(),
dynamicDataSourceType.name(), ms.getSqlCommandType().name());
cacheMap.put(ms.getId(), dynamicDataSourceType);
}
DynamicDataSourceHolder.putDataSource(dynamicDataSourceType);
}
return invocation.proceed();
}
示例10: currentSession
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
public Session currentSession() throws HibernateException {
Object value = TransactionSynchronizationManager
.getResource(this.sessionFactory);
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()) {
TransactionSynchronizationManager
.registerSynchronization(new SpringSessionSynchronization(
sessionHolder, this.sessionFactory));
sessionHolder.setSynchronizedWithTransaction(true);
// Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
// with FlushMode.MANUAL, which needs to allow flushing within the transaction.
FlushMode flushMode = session.getFlushMode();
if (FlushMode.isManualFlushMode(flushMode)
&& !TransactionSynchronizationManager
.isCurrentTransactionReadOnly()) {
session.setFlushMode(FlushMode.AUTO);
sessionHolder.setPreviousFlushMode(flushMode);
}
}
return session;
} else {
throw new HibernateException("No Session found for current thread");
}
}
示例11: doGetConnection
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
* Actually obtain a JDBC Connection from the given DataSource.
* Same as {@link #getConnection}, but throwing the original SQLException.
* <p>Is aware of a corresponding Connection bound to the current thread, for example
* when using {@link DataSourceTransactionManager}. Will bind a Connection to the thread
* if transaction synchronization is active (e.g. if in a JTA transaction).
* <p>Directly accessed by {@link TransactionAwareDataSourceProxy}.
* @param dataSource the DataSource to obtain Connections from
* @return a JDBC Connection from the given DataSource
* @throws SQLException if thrown by JDBC methods
* @see #doReleaseConnection
*/
public static Connection doGetConnection(DataSource dataSource) throws SQLException {
Assert.notNull(dataSource, "No DataSource specified");
ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) {
conHolder.requested();
if (!conHolder.hasConnection()) {
logger.debug("Fetching resumed JDBC Connection from DataSource");
conHolder.setConnection(dataSource.getConnection());
}
return conHolder.getConnection();
}
// Else we either got no holder or an empty thread-bound holder here.
logger.debug("Fetching JDBC Connection from DataSource");
Connection con = dataSource.getConnection();
if (TransactionSynchronizationManager.isSynchronizationActive()) {
logger.debug("Registering transaction synchronization for JDBC Connection");
// Use same Connection for further JDBC actions within the transaction.
// Thread-bound object will get removed by synchronization at transaction completion.
ConnectionHolder holderToUse = conHolder;
if (holderToUse == null) {
holderToUse = new ConnectionHolder(con);
}
else {
holderToUse.setConnection(con);
}
holderToUse.requested();
TransactionSynchronizationManager.registerSynchronization(
new ConnectionSynchronization(holderToUse, dataSource));
holderToUse.setSynchronizedWithTransaction(true);
if (holderToUse != conHolder) {
TransactionSynchronizationManager.bindResource(dataSource, holderToUse);
}
}
return con;
}
示例12: registerTransactionSynchronization
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
* Register a transaction synchronization for closing the given LobCreator,
* preferring Spring transaction synchronization and falling back to
* plain JTA transaction synchronization.
* @param lobCreator the LobCreator to close after transaction completion
* @param jtaTransactionManager the JTA TransactionManager to fall back to
* when no Spring transaction synchronization is active (may be {@code null})
* @throws IllegalStateException if there is neither active Spring transaction
* synchronization nor active JTA transaction synchronization
*/
public static void registerTransactionSynchronization(
LobCreator lobCreator, TransactionManager jtaTransactionManager) throws IllegalStateException {
if (TransactionSynchronizationManager.isSynchronizationActive()) {
logger.debug("Registering Spring transaction synchronization for LobCreator");
TransactionSynchronizationManager.registerSynchronization(
new SpringLobCreatorSynchronization(lobCreator));
}
else {
if (jtaTransactionManager != null) {
try {
int jtaStatus = jtaTransactionManager.getStatus();
if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
logger.debug("Registering JTA transaction synchronization for LobCreator");
jtaTransactionManager.getTransaction().registerSynchronization(
new JtaLobCreatorSynchronization(lobCreator));
return;
}
}
catch (Throwable ex) {
throw new TransactionSystemException(
"Could not register synchronization with JTA TransactionManager", ex);
}
}
throw new IllegalStateException("Active Spring transaction synchronization or active " +
"JTA transaction with specified [javax.transaction.TransactionManager] required");
}
}
示例13: put
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
public void put(final Object key, final Object value) {
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
targetCache.put(key, value);
}
});
}
else {
this.targetCache.put(key, value);
}
}
示例14: evict
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
public void evict(final Object key) {
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
targetCache.evict(key);
}
});
}
else {
this.targetCache.evict(key);
}
}
示例15: doJoinTransaction
import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
* Join an existing transaction, if not already joined.
* @param enforce whether to enforce the transaction
* (i.e. whether failure to join is considered fatal)
*/
private void doJoinTransaction(boolean enforce) {
if (this.jta) {
// Let's try whether we're in a JTA transaction.
try {
this.target.joinTransaction();
logger.debug("Joined JTA transaction");
}
catch (TransactionRequiredException ex) {
if (!enforce) {
logger.debug("No JTA transaction to join: " + ex);
}
else {
throw ex;
}
}
}
else {
if (TransactionSynchronizationManager.isSynchronizationActive()) {
if (!TransactionSynchronizationManager.hasResource(this.target) &&
!this.target.getTransaction().isActive()) {
enlistInCurrentTransaction();
}
logger.debug("Joined local transaction");
}
else {
if (!enforce) {
logger.debug("No local transaction to join");
}
else {
throw new TransactionRequiredException("No local transaction to join");
}
}
}
}