Java Code Examples for org.springframework.transaction.TransactionDefinition#ISOLATION_DEFAULT
The following examples show how to use
org.springframework.transaction.TransactionDefinition#ISOLATION_DEFAULT .
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: EclipseLinkJpaDialect.java From spring-analysis-note with MIT License | 9 votes |
@Override @Nullable public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { // Pass custom isolation level on to EclipseLink's DatabaseLogin configuration // (since Spring 4.1.2) UnitOfWork uow = entityManager.unwrap(UnitOfWork.class); uow.getLogin().setTransactionIsolation(definition.getIsolationLevel()); } entityManager.getTransaction().begin(); if (!definition.isReadOnly() && !this.lazyDatabaseTransaction) { // Begin an early transaction to force EclipseLink to get a JDBC Connection // so that Spring can manage transactions with JDBC as well as EclipseLink. entityManager.unwrap(UnitOfWork.class).beginEarlyTransaction(); } return null; }
Example 2
Source File: OpenJpaDialect.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { OpenJPAEntityManager openJpaEntityManager = getOpenJPAEntityManager(entityManager); if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { // Pass custom isolation level on to OpenJPA's JDBCFetchPlan configuration FetchPlan fetchPlan = openJpaEntityManager.getFetchPlan(); if (fetchPlan instanceof JDBCFetchPlan) { IsolationLevel isolation = IsolationLevel.fromConnectionConstant(definition.getIsolationLevel()); ((JDBCFetchPlan) fetchPlan).setIsolation(isolation); } } entityManager.getTransaction().begin(); if (!definition.isReadOnly()) { // Like with EclipseLink, make sure to start the logic transaction early so that other // participants using the connection (such as JdbcTemplate) run in a transaction. openJpaEntityManager.beginStore(); } // Custom implementation for OpenJPA savepoint handling return new OpenJpaTransactionData(openJpaEntityManager); }
Example 3
Source File: EclipseLinkJpaDialect.java From lams with GNU General Public License v2.0 | 6 votes |
@Override public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { // Pass custom isolation level on to EclipseLink's DatabaseLogin configuration // (since Spring 4.1.2) UnitOfWork uow = entityManager.unwrap(UnitOfWork.class); uow.getLogin().setTransactionIsolation(definition.getIsolationLevel()); } entityManager.getTransaction().begin(); if (!definition.isReadOnly() && !this.lazyDatabaseTransaction) { // Begin an early transaction to force EclipseLink to get a JDBC Connection // so that Spring can manage transactions with JDBC as well as EclipseLink. entityManager.unwrap(UnitOfWork.class).beginEarlyTransaction(); } return null; }
Example 4
Source File: EclipseLinkJpaDialect.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { // Pass custom isolation level on to EclipseLink's DatabaseLogin configuration // (since Spring 4.1.2) UnitOfWork uow = entityManager.unwrap(UnitOfWork.class); uow.getLogin().setTransactionIsolation(definition.getIsolationLevel()); } entityManager.getTransaction().begin(); if (!definition.isReadOnly() && !this.lazyDatabaseTransaction) { // Begin an early transaction to force EclipseLink to get a JDBC Connection // so that Spring can manage transactions with JDBC as well as EclipseLink. entityManager.unwrap(UnitOfWork.class).beginEarlyTransaction(); } return null; }
Example 5
Source File: DefaultJpaDialect.java From spring-analysis-note with MIT License | 5 votes |
/** * This implementation invokes the standard JPA {@code Transaction.begin} * method. Throws an InvalidIsolationLevelException if a non-default isolation * level is set. * <p>This implementation does not return any transaction data Object, since there * is no state to be kept for a standard JPA transaction. Hence, subclasses do not * have to care about the return value ({@code null}) of this implementation * and are free to return their own transaction data Object. * @see javax.persistence.EntityTransaction#begin * @see org.springframework.transaction.InvalidIsolationLevelException * @see #cleanupTransaction */ @Override @Nullable public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { throw new InvalidIsolationLevelException(getClass().getSimpleName() + " does not support custom isolation levels due to limitations in standard JPA. " + "Specific arrangements may be implemented in custom JpaDialect variants."); } entityManager.getTransaction().begin(); return null; }
Example 6
Source File: DefaultJpaDialect.java From lams with GNU General Public License v2.0 | 5 votes |
/** * This implementation invokes the standard JPA {@code Transaction.begin} * method. Throws an InvalidIsolationLevelException if a non-default isolation * level is set. * <p>This implementation does not return any transaction data Object, since there * is no state to be kept for a standard JPA transaction. Hence, subclasses do not * have to care about the return value ({@code null}) of this implementation * and are free to return their own transaction data Object. * @see javax.persistence.EntityTransaction#begin * @see org.springframework.transaction.InvalidIsolationLevelException * @see #cleanupTransaction */ @Override public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { throw new InvalidIsolationLevelException(getClass().getSimpleName() + " does not support custom isolation levels due to limitations in standard JPA. " + "Specific arrangements may be implemented in custom JpaDialect variants."); } entityManager.getTransaction().begin(); return null; }
Example 7
Source File: DatastoreTransactionManager.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
@Override protected void doBegin(Object transactionObject, TransactionDefinition transactionDefinition) throws TransactionException { if (transactionDefinition .getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT && transactionDefinition .getIsolationLevel() != TransactionDefinition.ISOLATION_SERIALIZABLE) { throw new IllegalStateException( "DatastoreTransactionManager supports only isolation level " + "TransactionDefinition.ISOLATION_DEFAULT or ISOLATION_SERIALIZABLE"); } if (transactionDefinition .getPropagationBehavior() != TransactionDefinition.PROPAGATION_REQUIRED) { throw new IllegalStateException( "DatastoreTransactionManager supports only propagation behavior " + "TransactionDefinition.PROPAGATION_REQUIRED"); } Tx tx = (Tx) transactionObject; if (transactionDefinition.isReadOnly()) { tx.transaction = tx.datastore.newTransaction(READ_ONLY_OPTIONS); } else { tx.transaction = tx.datastore.newTransaction(); } TransactionSynchronizationManager.bindResource(tx.datastore, tx); }
Example 8
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 9
Source File: SpringAwareUserTransactionTest.java From alfresco-core with GNU Lesser General Public License v3.0 | 5 votes |
private UserTransaction getFailingTxn() { return new SpringAwareUserTransaction( failingTransactionManager, false, TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.TIMEOUT_DEFAULT); }
Example 10
Source File: SpringAwareUserTransactionTest.java From alfresco-core with GNU Lesser General Public License v3.0 | 5 votes |
private UserTransaction getTxn() { return new SpringAwareUserTransaction( transactionManager, false, TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.TIMEOUT_DEFAULT); }
Example 11
Source File: DefaultJpaDialect.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * This implementation invokes the standard JPA {@code Transaction.begin} * method. Throws an InvalidIsolationLevelException if a non-default isolation * level is set. * <p>This implementation does not return any transaction data Object, since there * is no state to be kept for a standard JPA transaction. Hence, subclasses do not * have to care about the return value ({@code null}) of this implementation * and are free to return their own transaction data Object. * @see javax.persistence.EntityTransaction#begin * @see org.springframework.transaction.InvalidIsolationLevelException * @see #cleanupTransaction */ @Override public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { throw new InvalidIsolationLevelException(getClass().getSimpleName() + " does not support custom isolation levels due to limitations in standard JPA. " + "Specific arrangements may be implemented in custom JpaDialect variants."); } entityManager.getTransaction().begin(); return null; }
Example 12
Source File: DefaultJpaDialect.java From java-technology-stack with MIT License | 5 votes |
/** * This implementation invokes the standard JPA {@code Transaction.begin} * method. Throws an InvalidIsolationLevelException if a non-default isolation * level is set. * <p>This implementation does not return any transaction data Object, since there * is no state to be kept for a standard JPA transaction. Hence, subclasses do not * have to care about the return value ({@code null}) of this implementation * and are free to return their own transaction data Object. * @see javax.persistence.EntityTransaction#begin * @see org.springframework.transaction.InvalidIsolationLevelException * @see #cleanupTransaction */ @Override @Nullable public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { throw new InvalidIsolationLevelException(getClass().getSimpleName() + " does not support custom isolation levels due to limitations in standard JPA. " + "Specific arrangements may be implemented in custom JpaDialect variants."); } entityManager.getTransaction().begin(); return null; }
Example 13
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 14
Source File: DataSourceUtils.java From spring-analysis-note with MIT License | 4 votes |
/** * Prepare the given Connection with the given transaction semantics. * @param con the Connection to prepare * @param definition the transaction definition to apply * @return the previous isolation level, if any * @throws SQLException if thrown by JDBC methods * @see #resetConnectionAfterTransaction */ @Nullable public static Integer prepareConnectionForTransaction(Connection con, @Nullable TransactionDefinition definition) throws SQLException { Assert.notNull(con, "No Connection specified"); // Set read-only flag. if (definition != null && definition.isReadOnly()) { try { if (logger.isDebugEnabled()) { logger.debug("Setting JDBC Connection [" + con + "] read-only"); } con.setReadOnly(true); } catch (SQLException | RuntimeException ex) { Throwable exToCheck = ex; while (exToCheck != null) { if (exToCheck.getClass().getSimpleName().contains("Timeout")) { // Assume it's a connection timeout that would otherwise get lost: e.g. from JDBC 4.0 throw ex; } exToCheck = exToCheck.getCause(); } // "read-only not supported" SQLException -> ignore, it's just a hint anyway logger.debug("Could not set JDBC Connection read-only", ex); } } // Apply specific isolation level, if any. Integer previousIsolationLevel = null; if (definition != null && definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { if (logger.isDebugEnabled()) { logger.debug("Changing isolation level of JDBC Connection [" + con + "] to " + definition.getIsolationLevel()); } int currentIsolation = con.getTransactionIsolation(); if (currentIsolation != definition.getIsolationLevel()) { previousIsolationLevel = currentIsolation; con.setTransactionIsolation(definition.getIsolationLevel()); } } return previousIsolationLevel; }
Example 15
Source File: SpringTransactionManager.java From dalesbred with MIT License | 4 votes |
static int springIsolationCode(@NotNull Isolation isolation) { if (isolation == Isolation.DEFAULT) return TransactionDefinition.ISOLATION_DEFAULT; else return isolation.getJdbcLevel(); }
Example 16
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 17
Source File: IsolationLevelDataSourceAdapter.java From java-technology-stack with MIT License | 3 votes |
/** * Specify the default isolation level to use for Connection retrieval, * according to the JDBC {@link java.sql.Connection} constants * (equivalent to the corresponding Spring * {@link org.springframework.transaction.TransactionDefinition} constants). * <p>If not specified, the target DataSource's default will be used. * Note that a transaction-specific isolation value will always override * any isolation setting specified at the DataSource level. * @see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED * @see java.sql.Connection#TRANSACTION_READ_COMMITTED * @see java.sql.Connection#TRANSACTION_REPEATABLE_READ * @see java.sql.Connection#TRANSACTION_SERIALIZABLE * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED * @see org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ * @see org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE * @see org.springframework.transaction.TransactionDefinition#getIsolationLevel() * @see org.springframework.transaction.support.TransactionSynchronizationManager#getCurrentTransactionIsolationLevel() */ public void setIsolationLevel(int isolationLevel) { if (!constants.getValues(DefaultTransactionDefinition.PREFIX_ISOLATION).contains(isolationLevel)) { throw new IllegalArgumentException("Only values of isolation constants allowed"); } this.isolationLevel = (isolationLevel != TransactionDefinition.ISOLATION_DEFAULT ? isolationLevel : null); }
Example 18
Source File: IsolationLevelDataSourceAdapter.java From effectivejava with Apache License 2.0 | 3 votes |
/** * Specify the default isolation level to use for Connection retrieval, * according to the JDBC {@link java.sql.Connection} constants * (equivalent to the corresponding Spring * {@link org.springframework.transaction.TransactionDefinition} constants). * <p>If not specified, the target DataSource's default will be used. * Note that a transaction-specific isolation value will always override * any isolation setting specified at the DataSource level. * @see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED * @see java.sql.Connection#TRANSACTION_READ_COMMITTED * @see java.sql.Connection#TRANSACTION_REPEATABLE_READ * @see java.sql.Connection#TRANSACTION_SERIALIZABLE * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED * @see org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ * @see org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE * @see org.springframework.transaction.TransactionDefinition#getIsolationLevel() * @see org.springframework.transaction.support.TransactionSynchronizationManager#getCurrentTransactionIsolationLevel() */ public void setIsolationLevel(int isolationLevel) { if (!constants.getValues(DefaultTransactionDefinition.PREFIX_ISOLATION).contains(isolationLevel)) { throw new IllegalArgumentException("Only values of isolation constants allowed"); } this.isolationLevel = (isolationLevel != TransactionDefinition.ISOLATION_DEFAULT ? isolationLevel : null); }
Example 19
Source File: JtaTransactionManager.java From spring-analysis-note with MIT License | 3 votes |
/** * Apply the given transaction isolation level. The default implementation * will throw an exception for any level other than ISOLATION_DEFAULT. * <p>To be overridden in subclasses for specific JTA implementations, * as alternative to overriding the full {@link #doJtaBegin} method. * @param txObject the JtaTransactionObject containing the UserTransaction * @param isolationLevel isolation level taken from transaction definition * @throws InvalidIsolationLevelException if the given isolation level * cannot be applied * @throws SystemException if thrown by the JTA implementation * @see #doJtaBegin * @see JtaTransactionObject#getUserTransaction() * @see #getTransactionManager() */ protected void applyIsolationLevel(JtaTransactionObject txObject, int isolationLevel) throws InvalidIsolationLevelException, SystemException { if (!this.allowCustomIsolationLevels && isolationLevel != TransactionDefinition.ISOLATION_DEFAULT) { throw new InvalidIsolationLevelException( "JtaTransactionManager does not support custom isolation levels by default - " + "switch 'allowCustomIsolationLevels' to 'true'"); } }
Example 20
Source File: JtaTransactionManager.java From spring4-understanding with Apache License 2.0 | 3 votes |
/** * Apply the given transaction isolation level. The default implementation * will throw an exception for any level other than ISOLATION_DEFAULT. * <p>To be overridden in subclasses for specific JTA implementations, * as alternative to overriding the full {@link #doJtaBegin} method. * @param txObject the JtaTransactionObject containing the UserTransaction * @param isolationLevel isolation level taken from transaction definition * @throws InvalidIsolationLevelException if the given isolation level * cannot be applied * @throws SystemException if thrown by the JTA implementation * @see #doJtaBegin * @see JtaTransactionObject#getUserTransaction() * @see #getTransactionManager() */ protected void applyIsolationLevel(JtaTransactionObject txObject, int isolationLevel) throws InvalidIsolationLevelException, SystemException { if (!this.allowCustomIsolationLevels && isolationLevel != TransactionDefinition.ISOLATION_DEFAULT) { throw new InvalidIsolationLevelException( "JtaTransactionManager does not support custom isolation levels by default - " + "switch 'allowCustomIsolationLevels' to 'true'"); } }