Java Code Examples for org.springframework.transaction.support.DefaultTransactionDefinition#setIsolationLevel()

The following examples show how to use org.springframework.transaction.support.DefaultTransactionDefinition#setIsolationLevel() . 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: SpringJdbcTransactionOperations.java    From micronaut-data with Apache License 2.0 6 votes vote down vote up
@Override
public <R> R execute(@NonNull TransactionDefinition definition, @NonNull TransactionCallback<Connection, R> callback) {
    ArgumentUtils.requireNonNull("callback", callback);
    ArgumentUtils.requireNonNull("definition", definition);

    final DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setReadOnly(definition.isReadOnly());
    def.setIsolationLevel(definition.getIsolationLevel().getCode());
    def.setPropagationBehavior(definition.getPropagationBehavior().ordinal());
    def.setName(definition.getName());
    final Duration timeout = definition.getTimeout();
    if (!timeout.isNegative()) {
        def.setTimeout((int) timeout.getSeconds());
    }
    TransactionTemplate template = new TransactionTemplate(transactionManager, def);
    return template.execute(status -> {
                try {
                    return callback.call(new JdbcTransactionStatus(status));
                } catch (RuntimeException | Error ex) {
                    throw ex;
                } catch (Exception e) {
                    throw new UndeclaredThrowableException(e, "TransactionCallback threw undeclared checked exception");
                }
            }
    );
}
 
Example 2
Source File: SpringHibernateTransactionOperations.java    From micronaut-data with Apache License 2.0 6 votes vote down vote up
@Override
public <R> R execute(@NonNull TransactionDefinition definition, @NonNull TransactionCallback<Connection, R> callback) {
    ArgumentUtils.requireNonNull("callback", callback);
    ArgumentUtils.requireNonNull("definition", definition);

    final DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setReadOnly(definition.isReadOnly());
    def.setIsolationLevel(definition.getIsolationLevel().getCode());
    def.setPropagationBehavior(definition.getPropagationBehavior().ordinal());
    def.setName(definition.getName());
    final Duration timeout = definition.getTimeout();
    if (!timeout.isNegative()) {
        def.setTimeout((int) timeout.getSeconds());
    }
    TransactionTemplate template = new TransactionTemplate(transactionManager, def);
    return template.execute(status -> {
                try {
                    return callback.call(new JpaTransactionStatus(status));
                } catch (RuntimeException | Error ex) {
                    throw ex;
                } catch (Exception e) {
                    throw new UndeclaredThrowableException(e, "TransactionCallback threw undeclared checked exception");
                }
            }
    );
}
 
Example 3
Source File: ManualTransactionIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
void givenAPayment_WhenUsingTxManager_ThenShouldCommit() {
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
    definition.setTimeout(3);

    TransactionStatus status = transactionManager.getTransaction(definition);
    try {
        Payment payment = new Payment();
        payment.setReferenceNumber("Ref-1");
        payment.setState(Payment.State.SUCCESSFUL);

        entityManager.persist(payment);
        transactionManager.commit(status);
    } catch (Exception ex) {
        transactionManager.rollback(status);
    }

    assertThat(entityManager
            .createQuery("select p from Payment p", Payment.class)
            .getResultList()).hasSize(1);
}
 
Example 4
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 6 votes vote down vote up
public Object execGroovyRetObjByDbTranNest(String groovyName, String methodName, Integer nestDef,
			Object... paramArray) throws Exception{
/*		MicroMetaDao microDao=MicroMetaDao.getInstance(dbName,dbType);
		DataSource dataSource=microDao.getMicroDataSource();
		PlatformTransactionManager  transactionManager=new DataSourceTransactionManager(dataSource);*/
		PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
	    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
	    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
	    if(nestDef==null){
	    	nestDef=TransactionDefinition.PROPAGATION_REQUIRED;
	    }
	    def.setPropagationBehavior(nestDef);
	    TransactionStatus status=transactionManager.getTransaction(def);
	    try
	    {
	    	Object retObj= GroovyExecUtil.execGroovyRetObj(groovyName, methodName, paramArray);
	    	transactionManager.commit(status);
	    	return retObj;
	    }
	    catch(Exception ex)
	    {
	    	transactionManager.rollback(status);
	        throw ex;
	    }
		
	}
 
Example 5
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 6 votes vote down vote up
public Object execGroovyRetObjByDbTran(String groovyName, String methodName,
			Object... paramArray) throws Exception{
/*		MicroMetaDao microDao=MicroMetaDao.getInstance(dbName,dbType);
		DataSource dataSource=microDao.getMicroDataSource();
		PlatformTransactionManager  transactionManager=new DataSourceTransactionManager(dataSource);*/
		PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
	    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
	    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
	    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	    TransactionStatus status=transactionManager.getTransaction(def);
	    try
	    {
	    	Object retObj= GroovyExecUtil.execGroovyRetObj(groovyName, methodName, paramArray);
	    	transactionManager.commit(status);
	    	return retObj;
	    }
	    catch(Exception ex)
	    {
	    	transactionManager.rollback(status);
	        throw ex;
	    }
		
	}
 
Example 6
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 6 votes vote down vote up
public Object execGroovyRetObjByDbTranNest(String groovyName, String methodName, Integer nestDef,
			Object... paramArray) throws Exception{
/*		MicroMetaDao microDao=MicroMetaDao.getInstance(dbName,dbType);
		DataSource dataSource=microDao.getMicroDataSource();
		PlatformTransactionManager  transactionManager=new DataSourceTransactionManager(dataSource);*/
		PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
	    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
	    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
	    if(nestDef==null){
	    	nestDef=TransactionDefinition.PROPAGATION_REQUIRED;
	    }
	    def.setPropagationBehavior(nestDef);
	    TransactionStatus status=transactionManager.getTransaction(def);
	    try
	    {
	    	Object retObj= GroovyExecUtil.execGroovyRetObj(groovyName, methodName, paramArray);
	    	transactionManager.commit(status);
	    	return retObj;
	    }
	    catch(Exception ex)
	    {
	    	transactionManager.rollback(status);
	        throw ex;
	    }
		
	}
 
Example 7
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 6 votes vote down vote up
public Object execGroovyRetObjByDbTran(String groovyName, String methodName,
			Object... paramArray) throws Exception{
/*		MicroMetaDao microDao=MicroMetaDao.getInstance(dbName,dbType);
		DataSource dataSource=microDao.getMicroDataSource();
		PlatformTransactionManager  transactionManager=new DataSourceTransactionManager(dataSource);*/
		PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
	    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
	    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
	    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	    TransactionStatus status=transactionManager.getTransaction(def);
	    try
	    {
	    	Object retObj= GroovyExecUtil.execGroovyRetObj(groovyName, methodName, paramArray);
	    	transactionManager.commit(status);
	    	return retObj;
	    }
	    catch(Exception ex)
	    {
	    	transactionManager.rollback(status);
	        throw ex;
	    }
		
	}
 
Example 8
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 5 votes vote down vote up
public void dbTranNestRollbackAndThrow() throws Exception{
	PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    TransactionStatus status=transactionManager.getTransaction(def);
    transactionManager.rollback(status);
    throw new RuntimeException("dbTranNestRollbackAndThrow");

}
 
Example 9
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 5 votes vote down vote up
public void dbTranNestRollbackAndThrow() throws Exception{
	PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    TransactionStatus status=transactionManager.getTransaction(def);
    transactionManager.rollback(status);
    throw new RuntimeException("dbTranNestRollbackAndThrow");

}
 
Example 10
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 5 votes vote down vote up
public void dbTranNestRollback() throws Exception{
	PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    TransactionStatus status=transactionManager.getTransaction(def);
    transactionManager.rollback(status);

}
 
Example 11
Source File: DefaultJpaDialectTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultTransactionDefinition() throws Exception {
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);

	try {
		dialect.beginTransaction(null, definition);
		fail("expected exception");
	}
	catch (TransactionException e) {
		// ok
	}
}
 
Example 12
Source File: DefaultJpaDialectTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testDefaultTransactionDefinition() throws Exception {
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);

	try {
		dialect.beginTransaction(null, definition);
		fail("expected exception");
	}
	catch (TransactionException e) {
		// ok
	}
}
 
Example 13
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 5 votes vote down vote up
public void dbTranNestRollback() throws Exception{
	PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    TransactionStatus status=transactionManager.getTransaction(def);
    transactionManager.rollback(status);

}
 
Example 14
Source File: AbstractAsgardConsumer.java    From choerodon-starters with Apache License 2.0 5 votes vote down vote up
protected TransactionStatus createTransactionStatus(final PlatformTransactionManager transactionManager,
                                                    final int isolationLevel) {
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    def.setIsolationLevel(isolationLevel);
    return transactionManager.getTransaction(def);
}
 
Example 15
Source File: TransactionalProducerImpl.java    From choerodon-starters with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final StartSagaBuilder builder, final Consumer<StartSagaBuilder> consumer) {
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED);
    def.setIsolationLevel(ISOLATION_DEFAULT);
    apply(builder, consumer, def);
}
 
Example 16
Source File: TransactionalProducerImpl.java    From choerodon-starters with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T applyAndReturn(final StartSagaBuilder builder, final Function<StartSagaBuilder, T> function) {
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED);
    def.setIsolationLevel(ISOLATION_DEFAULT);
    return applyAndReturn(builder, function, def);
}
 
Example 17
Source File: DefaultJpaDialectTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testDefaultTransactionDefinition() throws Exception {
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);

	try {
		dialect.beginTransaction(null, definition);
		fail("expected exception");
	}
	catch (TransactionException e) {
		// ok
	}
}
 
Example 18
Source File: PersistHelper.java    From bulbasaur with Apache License 2.0 4 votes vote down vote up
private TransactionStatus getTransactionStatus() {
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
	definition.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
	return transactionManager.getTransaction(definition);
}
 
Example 19
Source File: SpringTransactionManager.java    From dalesbred with MIT License 4 votes vote down vote up
private static @NotNull DefaultTransactionDefinition settingsToSpringDefinition(@NotNull TransactionSettings settings) {
    DefaultTransactionDefinition df = new DefaultTransactionDefinition();
    df.setIsolationLevel(springIsolationCode(settings.getIsolation()));
    df.setPropagationBehavior(springPropagationCode(settings.getPropagation()));
    return df;
}
 
Example 20
Source File: TicketReservationManager.java    From alf.io with GNU General Public License v3.0 4 votes vote down vote up
public TicketReservationManager(EventRepository eventRepository,
                                OrganizationRepository organizationRepository,
                                TicketRepository ticketRepository,
                                TicketReservationRepository ticketReservationRepository,
                                TicketCategoryRepository ticketCategoryRepository,
                                TicketCategoryDescriptionRepository ticketCategoryDescriptionRepository,
                                ConfigurationManager configurationManager,
                                PaymentManager paymentManager,
                                PromoCodeDiscountRepository promoCodeDiscountRepository,
                                SpecialPriceRepository specialPriceRepository,
                                TransactionRepository transactionRepository,
                                NotificationManager notificationManager,
                                MessageSourceManager messageSourceManager,
                                TemplateManager templateManager,
                                PlatformTransactionManager transactionManager,
                                WaitingQueueManager waitingQueueManager,
                                TicketFieldRepository ticketFieldRepository,
                                AdditionalServiceRepository additionalServiceRepository,
                                AdditionalServiceItemRepository additionalServiceItemRepository,
                                AdditionalServiceTextRepository additionalServiceTextRepository,
                                InvoiceSequencesRepository invoiceSequencesRepository,
                                AuditingRepository auditingRepository,
                                UserRepository userRepository,
                                ExtensionManager extensionManager, TicketSearchRepository ticketSearchRepository,
                                GroupManager groupManager,
                                BillingDocumentRepository billingDocumentRepository,
                                NamedParameterJdbcTemplate jdbcTemplate,
                                Json json,
                                PromoCodeDiscountRepository promoCodeRepository,
                                BillingDocumentManager billingDocumentManager) {
    this.eventRepository = eventRepository;
    this.organizationRepository = organizationRepository;
    this.ticketRepository = ticketRepository;
    this.ticketReservationRepository = ticketReservationRepository;
    this.ticketCategoryRepository = ticketCategoryRepository;
    this.ticketCategoryDescriptionRepository = ticketCategoryDescriptionRepository;
    this.configurationManager = configurationManager;
    this.paymentManager = paymentManager;
    this.promoCodeDiscountRepository = promoCodeDiscountRepository;
    this.specialPriceRepository = specialPriceRepository;
    this.transactionRepository = transactionRepository;
    this.notificationManager = notificationManager;
    this.messageSourceManager = messageSourceManager;
    this.templateManager = templateManager;
    this.waitingQueueManager = waitingQueueManager;
    this.requiresNewTransactionTemplate = new TransactionTemplate(transactionManager, new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW));
    DefaultTransactionDefinition serialized = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    serialized.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
    this.serializedTransactionTemplate = new TransactionTemplate(transactionManager, serialized);
    this.nestedTransactionTemplate = new TransactionTemplate(transactionManager, new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_NESTED));
    this.ticketFieldRepository = ticketFieldRepository;
    this.additionalServiceRepository = additionalServiceRepository;
    this.additionalServiceItemRepository = additionalServiceItemRepository;
    this.additionalServiceTextRepository = additionalServiceTextRepository;
    this.invoiceSequencesRepository = invoiceSequencesRepository;
    this.auditingRepository = auditingRepository;
    this.userRepository = userRepository;
    this.extensionManager = extensionManager;
    this.ticketSearchRepository = ticketSearchRepository;
    this.groupManager = groupManager;
    this.billingDocumentRepository = billingDocumentRepository;
    this.jdbcTemplate = jdbcTemplate;
    this.json = json;
    this.promoCodeRepository = promoCodeRepository;
    this.billingDocumentManager = billingDocumentManager;
}