Java Code Examples for org.springframework.transaction.TransactionDefinition#getTimeout()
The following examples show how to use
org.springframework.transaction.TransactionDefinition#getTimeout() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IbisTransaction.java From iaf with Apache License 2.0 | 6 votes |
public IbisTransaction(PlatformTransactionManager txManager, TransactionDefinition txDef, String object) { this.txManager = txManager; this.txDef = txDef; this.object = object; txClientIsActive = TransactionSynchronizationManager.isActualTransactionActive(); txClientName = TransactionSynchronizationManager.getCurrentTransactionName(); txStatus = txManager.getTransaction(txDef); txIsActive = TransactionSynchronizationManager.isActualTransactionActive(); txIsNew = txStatus.isNewTransaction(); if (txIsNew) { txName = Misc.createSimpleUUID(); TransactionSynchronizationManager.setCurrentTransactionName(txName); int txTimeout = txDef.getTimeout(); log.debug("Transaction manager ["+getRealTransactionManager()+"] created a new transaction ["+txName+"] for " + object + " with timeout [" + (txTimeout<0?"system default(=120s)":""+txTimeout) + "]"); } else { txName = TransactionSynchronizationManager.getCurrentTransactionName(); if (txClientIsActive && !txIsActive) { log.debug("Transaction manager ["+getRealTransactionManager()+"] suspended the transaction [" + txClientName + "] for " + object); } } }
Example 2
Source File: ReactiveRedissonTransactionManager.java From redisson with Apache License 2.0 | 6 votes |
@Override protected Mono<Void> doBegin(TransactionSynchronizationManager synchronizationManager, Object transaction, TransactionDefinition definition) throws TransactionException { ReactiveRedissonTransactionObject tObject = (ReactiveRedissonTransactionObject) transaction; TransactionOptions options = TransactionOptions.defaults(); if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) { options.timeout(definition.getTimeout(), TimeUnit.SECONDS); } RTransactionReactive trans = redissonClient.createTransaction(options); ReactiveRedissonResourceHolder holder = new ReactiveRedissonResourceHolder(); holder.setTransaction(trans); tObject.setResourceHolder(holder); synchronizationManager.bindResource(redissonClient, holder); return Mono.empty(); }
Example 3
Source File: HibernateExtendedJpaDialect.java From bamboobsc with Apache License 2.0 | 6 votes |
@Override public Object beginTransaction(final EntityManager entityManager, final TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { Session session = (Session) entityManager.getDelegate(); if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) { getSession(entityManager).getTransaction().setTimeout(definition.getTimeout()); } entityManager.getTransaction().begin(); logger.debug("Transaction started"); session.doWork(new Work() { @Override public void execute(Connection connection) throws SQLException { logger.debug("The connection instance is " + connection.toString()); logger.debug("The isolation level of the connection is " + connection.getTransactionIsolation() + " and the isolation level set on the transaction is " + definition.getIsolationLevel() ); DataSourceUtils.prepareConnectionForTransaction(connection, definition); } }); return prepareTransaction(entityManager, definition.isReadOnly(), definition.getName()); }
Example 4
Source File: Neo4jTransactionUtils.java From sdn-rx with Apache License 2.0 | 6 votes |
/** * Maps a Spring {@link TransactionDefinition transaction definition} to a native Neo4j driver transaction. * Only the default isolation leven ({@link TransactionDefinition#ISOLATION_DEFAULT}) and * {@link TransactionDefinition#PROPAGATION_REQUIRED propagation required} behaviour are supported. * * @param definition The transaction definition passed to a Neo4j transaction manager * @return A Neo4j native transaction configuration */ static TransactionConfig createTransactionConfigFrom(TransactionDefinition definition) { if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { throw new InvalidIsolationLevelException( "Neo4jTransactionManager is not allowed to support custom isolation levels."); } int propagationBehavior = definition.getPropagationBehavior(); if (!(propagationBehavior == TransactionDefinition.PROPAGATION_REQUIRED || propagationBehavior == TransactionDefinition.PROPAGATION_REQUIRES_NEW)) { throw new IllegalTransactionStateException("Neo4jTransactionManager only supports 'required' or 'requires new' propagation."); } TransactionConfig.Builder builder = TransactionConfig.builder(); if (definition.getTimeout() > 0) { builder = builder.withTimeout(Duration.ofSeconds(definition.getTimeout())); } return builder.build(); }
Example 5
Source File: HibernateJpaDialect.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { Session session = getSession(entityManager); if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) { session.getTransaction().setTimeout(definition.getTimeout()); } boolean isolationLevelNeeded = (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT); Integer previousIsolationLevel = null; Connection preparedCon = null; if (isolationLevelNeeded || definition.isReadOnly()) { if (this.prepareConnection) { preparedCon = HibernateConnectionHandle.doGetConnection(session); previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(preparedCon, definition); } else if (isolationLevelNeeded) { throw new InvalidIsolationLevelException(getClass().getSimpleName() + " does not support custom isolation levels since the 'prepareConnection' flag is off. " + "This is the case on Hibernate 3.6 by default; either switch that flag at your own risk " + "or upgrade to Hibernate 4.x, with 4.2+ recommended."); } } // Standard JPA transaction begin call for full JPA context setup... entityManager.getTransaction().begin(); // Adapt flush mode and store previous isolation level, if any. FlushMode previousFlushMode = prepareFlushMode(session, definition.isReadOnly()); return new SessionTransactionData(session, previousFlushMode, preparedCon, previousIsolationLevel); }
Example 6
Source File: SpringTransactionManager.java From ignite with Apache License 2.0 | 5 votes |
/** {@inheritDoc} */ @Override protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException { if (definition.getIsolationLevel() == TransactionDefinition.ISOLATION_READ_UNCOMMITTED) throw new InvalidIsolationLevelException("Ignite does not support READ_UNCOMMITTED isolation level."); IgniteTransactionObject txObj = (IgniteTransactionObject)transaction; Transaction tx = null; try { if (txObj.getTransactionHolder() == null || txObj.getTransactionHolder().isSynchronizedWithTransaction()) { long timeout = ignite.configuration().getTransactionConfiguration().getDefaultTxTimeout(); if (definition.getTimeout() > 0) timeout = TimeUnit.SECONDS.toMillis(definition.getTimeout()); Transaction newTx = ignite.transactions().txStart(transactionConcurrency, convertToIgniteIsolationLevel(definition.getIsolationLevel()), timeout, 0); if (log.isDebugEnabled()) log.debug("Started Ignite transaction: " + newTx); txObj.setTransactionHolder(new IgniteTransactionHolder(newTx), true); } txObj.getTransactionHolder().setSynchronizedWithTransaction(true); txObj.getTransactionHolder().setTransactionActive(true); tx = txObj.getTransactionHolder().getTransaction(); // Bind the session holder to the thread. if (txObj.isNewTransactionHolder()) TransactionSynchronizationManager.bindResource(this.ignite, txObj.getTransactionHolder()); } catch (Exception ex) { if (tx != null) tx.close(); throw new CannotCreateTransactionException("Could not create Ignite transaction", ex); } }
Example 7
Source File: CustomHibernateJpaDialect.java From spring-boot with Apache License 2.0 | 5 votes |
@Override public Object beginTransaction(final EntityManager entityManager, final TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { Session session = (Session) entityManager.getDelegate(); if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) { getSession(entityManager).getTransaction().setTimeout( definition.getTimeout()); } final TransactionData data = new TransactionData(); session.doWork(new Work() { @Override public void execute(Connection connection) throws SQLException { Integer previousIsolationLevel = DataSourceUtils .prepareConnectionForTransaction(connection, definition); data.setPreviousIsolationLevel(previousIsolationLevel); data.setConnection(connection); } }); entityManager.getTransaction().begin(); Object springTransactionData = prepareTransaction(entityManager, definition.isReadOnly(), definition.getName()); data.setSpringTransactionData(springTransactionData); return data; }
Example 8
Source File: HibernateJpaDialect.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { Session session = getSession(entityManager); if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) { session.getTransaction().setTimeout(definition.getTimeout()); } boolean isolationLevelNeeded = (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT); Integer previousIsolationLevel = null; Connection preparedCon = null; if (isolationLevelNeeded || definition.isReadOnly()) { if (this.prepareConnection) { preparedCon = HibernateConnectionHandle.doGetConnection(session); previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(preparedCon, definition); } else if (isolationLevelNeeded) { throw new InvalidIsolationLevelException(getClass().getSimpleName() + " does not support custom isolation levels since the 'prepareConnection' flag is off. " + "This is the case on Hibernate 3.6 by default; either switch that flag at your own risk " + "or upgrade to Hibernate 4.x, with 4.2+ recommended."); } } // Standard JPA transaction begin call for full JPA context setup... entityManager.getTransaction().begin(); // Adapt flush mode and store previous isolation level, if any. FlushMode previousFlushMode = prepareFlushMode(session, definition.isReadOnly()); return new SessionTransactionData(session, previousFlushMode, preparedCon, previousIsolationLevel); }
Example 9
Source File: HibernateJpaDialect.java From java-technology-stack with MIT License | 4 votes |
@Override public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { Session session = getSession(entityManager); if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) { session.getTransaction().setTimeout(definition.getTimeout()); } boolean isolationLevelNeeded = (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT); Integer previousIsolationLevel = null; Connection preparedCon = null; if (isolationLevelNeeded || definition.isReadOnly()) { if (this.prepareConnection) { preparedCon = HibernateConnectionHandle.doGetConnection(session); previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(preparedCon, definition); } else if (isolationLevelNeeded) { throw new InvalidIsolationLevelException(getClass().getSimpleName() + " does not support custom isolation levels since the 'prepareConnection' flag is off."); } } // Standard JPA transaction begin call for full JPA context setup... entityManager.getTransaction().begin(); // Adapt flush mode and store previous isolation level, if any. FlushMode previousFlushMode = prepareFlushMode(session, definition.isReadOnly()); if (definition instanceof ResourceTransactionDefinition && ((ResourceTransactionDefinition) definition).isLocalResource()) { // As of 5.1, we explicitly optimize for a transaction-local EntityManager, // aligned with native HibernateTransactionManager behavior. previousFlushMode = null; if (definition.isReadOnly()) { session.setDefaultReadOnly(true); } } return new SessionTransactionData(session, previousFlushMode, preparedCon, previousIsolationLevel); }
Example 10
Source File: CubaEclipseLinkJpaDialect.java From cuba with Apache License 2.0 | 4 votes |
@Override public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { Object result = super.beginTransaction(entityManager, definition); Preconditions.checkState(result == null, "Transactional data should be null for EclipseLink dialect"); // Read default timeout every time - may be somebody wants to change it on the fly int defaultTimeout = 0; String defaultTimeoutProp = AppContext.getProperty("cuba.defaultQueryTimeoutSec"); if (!"0".equals(defaultTimeoutProp) && !StringUtils.isBlank(defaultTimeoutProp)) { try { defaultTimeout = Integer.parseInt(defaultTimeoutProp); } catch (NumberFormatException e) { log.error("Invalid cuba.defaultQueryTimeoutSec value", e); } } int timeoutSec = 0; if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) timeoutSec = definition.getTimeout(); else if (defaultTimeout != 0) timeoutSec = defaultTimeout; if (timeoutSec != 0) { log.trace("Applying query timeout {} sec", timeoutSec); if (entityManager instanceof JpaEntityManager) { UnitOfWork unitOfWork = ((JpaEntityManager) entityManager).getUnitOfWork(); if (unitOfWork != null) { //setup delay in seconds on unit of work unitOfWork.setQueryTimeoutDefault(timeoutSec); } } String s = DbmsSpecificFactory.getDbmsFeatures().getTransactionTimeoutStatement(); if (s != null) { Connection connection = entityManager.unwrap(Connection.class); try (Statement statement = connection.createStatement()) { statement.execute(String.format(s, timeoutSec * 1000)); } } } return new CubaEclipseLinkTransactionData(entityManager); }
Example 11
Source File: WebSphereUowTransactionManager.java From spring-analysis-note with MIT License | 4 votes |
@Override @Nullable public <T> T execute(@Nullable TransactionDefinition definition, TransactionCallback<T> callback) throws TransactionException { // Use defaults if no transaction definition given. TransactionDefinition def = (definition != null ? definition : TransactionDefinition.withDefaults()); if (def.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) { throw new InvalidTimeoutException("Invalid transaction timeout", def.getTimeout()); } UOWManager uowManager = obtainUOWManager(); int pb = def.getPropagationBehavior(); boolean existingTx = (uowManager.getUOWStatus() != UOWSynchronizationRegistry.UOW_STATUS_NONE && uowManager.getUOWType() != UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION); int uowType = UOWSynchronizationRegistry.UOW_TYPE_GLOBAL_TRANSACTION; boolean joinTx = false; boolean newSynch = false; if (existingTx) { if (pb == TransactionDefinition.PROPAGATION_NEVER) { throw new IllegalTransactionStateException( "Transaction propagation 'never' but existing transaction found"); } if (pb == TransactionDefinition.PROPAGATION_NESTED) { throw new NestedTransactionNotSupportedException( "Transaction propagation 'nested' not supported for WebSphere UOW transactions"); } if (pb == TransactionDefinition.PROPAGATION_SUPPORTS || pb == TransactionDefinition.PROPAGATION_REQUIRED || pb == TransactionDefinition.PROPAGATION_MANDATORY) { joinTx = true; newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER); } else if (pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) { uowType = UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION; newSynch = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS); } else { newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER); } } else { if (pb == TransactionDefinition.PROPAGATION_MANDATORY) { throw new IllegalTransactionStateException( "Transaction propagation 'mandatory' but no existing transaction found"); } if (pb == TransactionDefinition.PROPAGATION_SUPPORTS || pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED || pb == TransactionDefinition.PROPAGATION_NEVER) { uowType = UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION; newSynch = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS); } else { newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER); } } boolean debug = logger.isDebugEnabled(); if (debug) { logger.debug("Creating new transaction with name [" + def.getName() + "]: " + def); } SuspendedResourcesHolder suspendedResources = (!joinTx ? suspend(null) : null); UOWActionAdapter<T> action = null; try { if (def.getTimeout() > TransactionDefinition.TIMEOUT_DEFAULT) { uowManager.setUOWTimeout(uowType, def.getTimeout()); } if (debug) { logger.debug("Invoking WebSphere UOW action: type=" + uowType + ", join=" + joinTx); } action = new UOWActionAdapter<>( def, callback, (uowType == UOWManager.UOW_TYPE_GLOBAL_TRANSACTION), !joinTx, newSynch, debug); uowManager.runUnderUOW(uowType, joinTx, action); if (debug) { logger.debug("Returned from WebSphere UOW action: type=" + uowType + ", join=" + joinTx); } return action.getResult(); } catch (UOWException | UOWActionException ex) { TransactionSystemException tse = new TransactionSystemException("UOWManager transaction processing failed", ex); Throwable appEx = action.getException(); if (appEx != null) { logger.error("Application exception overridden by rollback exception", appEx); tse.initApplicationException(appEx); } throw tse; } finally { if (suspendedResources != null) { resume(null, suspendedResources); } } }
Example 12
Source File: HibernateJpaDialect.java From spring-analysis-note with MIT License | 4 votes |
@Override public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { Session session = getSession(entityManager); if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) { session.getTransaction().setTimeout(definition.getTimeout()); } boolean isolationLevelNeeded = (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT); Integer previousIsolationLevel = null; Connection preparedCon = null; if (isolationLevelNeeded || definition.isReadOnly()) { if (this.prepareConnection) { preparedCon = HibernateConnectionHandle.doGetConnection(session); previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(preparedCon, definition); } else if (isolationLevelNeeded) { throw new InvalidIsolationLevelException(getClass().getSimpleName() + " does not support custom isolation levels since the 'prepareConnection' flag is off."); } } // Standard JPA transaction begin call for full JPA context setup... entityManager.getTransaction().begin(); // Adapt flush mode and store previous isolation level, if any. FlushMode previousFlushMode = prepareFlushMode(session, definition.isReadOnly()); if (definition instanceof ResourceTransactionDefinition && ((ResourceTransactionDefinition) definition).isLocalResource()) { // As of 5.1, we explicitly optimize for a transaction-local EntityManager, // aligned with native HibernateTransactionManager behavior. previousFlushMode = null; if (definition.isReadOnly()) { session.setDefaultReadOnly(true); } } return new SessionTransactionData(session, previousFlushMode, preparedCon, previousIsolationLevel); }
Example 13
Source File: AbstractPlatformTransactionManager.java From lams with GNU General Public License v2.0 | 3 votes |
/** * Determine the actual timeout to use for the given definition. * Will fall back to this manager's default timeout if the * transaction definition doesn't specify a non-default value. * @param definition the transaction definition * @return the actual timeout to use * @see org.springframework.transaction.TransactionDefinition#getTimeout() * @see #setDefaultTimeout */ protected int determineTimeout(TransactionDefinition definition) { if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) { return definition.getTimeout(); } return this.defaultTimeout; }
Example 14
Source File: DefaultTransactionDefinition.java From lams with GNU General Public License v2.0 | 3 votes |
/** * Copy constructor. Definition can be modified through bean property setters. * @see #setPropagationBehavior * @see #setIsolationLevel * @see #setTimeout * @see #setReadOnly * @see #setName */ public DefaultTransactionDefinition(TransactionDefinition other) { this.propagationBehavior = other.getPropagationBehavior(); this.isolationLevel = other.getIsolationLevel(); this.timeout = other.getTimeout(); this.readOnly = other.isReadOnly(); this.name = other.getName(); }
Example 15
Source File: DefaultTransactionDefinition.java From java-technology-stack with MIT License | 3 votes |
/** * Copy constructor. Definition can be modified through bean property setters. * @see #setPropagationBehavior * @see #setIsolationLevel * @see #setTimeout * @see #setReadOnly * @see #setName */ public DefaultTransactionDefinition(TransactionDefinition other) { this.propagationBehavior = other.getPropagationBehavior(); this.isolationLevel = other.getIsolationLevel(); this.timeout = other.getTimeout(); this.readOnly = other.isReadOnly(); this.name = other.getName(); }
Example 16
Source File: AbstractPlatformTransactionManager.java From java-technology-stack with MIT License | 3 votes |
/** * Determine the actual timeout to use for the given definition. * Will fall back to this manager's default timeout if the * transaction definition doesn't specify a non-default value. * @param definition the transaction definition * @return the actual timeout to use * @see org.springframework.transaction.TransactionDefinition#getTimeout() * @see #setDefaultTimeout */ protected int determineTimeout(TransactionDefinition definition) { if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) { return definition.getTimeout(); } return this.defaultTimeout; }
Example 17
Source File: AbstractPlatformTransactionManager.java From spring4-understanding with Apache License 2.0 | 3 votes |
/** * Determine the actual timeout to use for the given definition. * Will fall back to this manager's default timeout if the * transaction definition doesn't specify a non-default value. * @param definition the transaction definition * @return the actual timeout to use * @see org.springframework.transaction.TransactionDefinition#getTimeout() * @see #setDefaultTimeout */ protected int determineTimeout(TransactionDefinition definition) { if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) { return definition.getTimeout(); } return this.defaultTimeout; }
Example 18
Source File: DefaultTransactionDefinition.java From spring4-understanding with Apache License 2.0 | 3 votes |
/** * Copy constructor. Definition can be modified through bean property setters. * @see #setPropagationBehavior * @see #setIsolationLevel * @see #setTimeout * @see #setReadOnly * @see #setName */ public DefaultTransactionDefinition(TransactionDefinition other) { this.propagationBehavior = other.getPropagationBehavior(); this.isolationLevel = other.getIsolationLevel(); this.timeout = other.getTimeout(); this.readOnly = other.isReadOnly(); this.name = other.getName(); }
Example 19
Source File: DefaultTransactionDefinition.java From spring-analysis-note with MIT License | 3 votes |
/** * Copy constructor. Definition can be modified through bean property setters. * @see #setPropagationBehavior * @see #setIsolationLevel * @see #setTimeout * @see #setReadOnly * @see #setName */ public DefaultTransactionDefinition(TransactionDefinition other) { this.propagationBehavior = other.getPropagationBehavior(); this.isolationLevel = other.getIsolationLevel(); this.timeout = other.getTimeout(); this.readOnly = other.isReadOnly(); this.name = other.getName(); }
Example 20
Source File: AbstractPlatformTransactionManager.java From spring-analysis-note with MIT License | 3 votes |
/** * Determine the actual timeout to use for the given definition. * Will fall back to this manager's default timeout if the * transaction definition doesn't specify a non-default value. * @param definition the transaction definition * @return the actual timeout to use * @see org.springframework.transaction.TransactionDefinition#getTimeout() * @see #setDefaultTimeout */ protected int determineTimeout(TransactionDefinition definition) { if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) { return definition.getTimeout(); } return getDefaultTimeout(); }