Example usage for org.springframework.transaction TransactionDefinition ISOLATION_REPEATABLE_READ

List of usage examples for org.springframework.transaction TransactionDefinition ISOLATION_REPEATABLE_READ

Introduction

In this page you can find the example usage for org.springframework.transaction TransactionDefinition ISOLATION_REPEATABLE_READ.

Prototype

int ISOLATION_REPEATABLE_READ

To view the source code for org.springframework.transaction TransactionDefinition ISOLATION_REPEATABLE_READ.

Click Source Link

Document

Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur.

Usage

From source file:com._4dconcept.springframework.data.marklogic.datasource.DatabaseConnectorJtaTransactionTest.java

@Test
public void testJtaTransactionWithIsolationLevelContentSourceAdapter() throws Exception {
    given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE,
            Status.STATUS_ACTIVE, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);

    final IsolationLevelContentSourceAdapter dsToUse = new IsolationLevelContentSourceAdapter();
    dsToUse.setTargetContentSource(contentSource);
    dsToUse.afterPropertiesSet();/*from www .  jav  a 2  s. com*/

    JtaTransactionManager ptm = new JtaTransactionManager(userTransaction);
    ptm.setAllowCustomIsolationLevels(true);

    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            Session c = ContentSourceUtils.getSession(dsToUse);
            assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse));
            assertSame(session, c);
            ContentSourceUtils.releaseSession(c, dsToUse);
        }
    });

    tt.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
    tt.setReadOnly(true);
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            Session c = ContentSourceUtils.getSession(dsToUse);
            assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse));
            assertSame(session, c);
            ContentSourceUtils.releaseSession(c, dsToUse);
        }
    });

    verify(userTransaction, times(2)).begin();
    verify(userTransaction, times(2)).commit();
    verify(session).setTransactionMode(Session.TransactionMode.QUERY);
    verify(session, times(2)).close();
}

From source file:com._4dconcept.springframework.data.marklogic.datasource.DatabaseConnectorJtaTransactionTest.java

private void doTestJtaTransactionWithIsolationLevelContentSourceRouter() throws Exception {
    given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE,
            Status.STATUS_ACTIVE, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);

    final ContentSource contentSource1 = mock(ContentSource.class);
    final Session session1 = mock(Session.class);
    given(contentSource1.newSession()).willReturn(session1);

    final ContentSource contentSource2 = mock(ContentSource.class);
    final Session session2 = mock(Session.class);
    given(contentSource2.newSession()).willReturn(session2);

    final IsolationLevelContentSourceRouter dsToUse = new IsolationLevelContentSourceRouter();
    Map<Object, Object> targetContentSources = new HashMap<Object, Object>();

    targetContentSources.put("ISOLATION_REPEATABLE_READ", contentSource2);
    dsToUse.setDefaultTargetContentSource(contentSource1);

    dsToUse.setTargetContentSources(targetContentSources);
    dsToUse.afterPropertiesSet();/*from   w ww .  ja  v  a  2  s.com*/

    JtaTransactionManager ptm = new JtaTransactionManager(userTransaction);
    ptm.setAllowCustomIsolationLevels(true);

    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            Session c = ContentSourceUtils.getSession(dsToUse);
            assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse));
            assertSame(session1, c);
            ContentSourceUtils.releaseSession(c, dsToUse);
        }
    });

    tt.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            Session c = ContentSourceUtils.getSession(dsToUse);
            assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse));
            assertSame(session2, c);
            ContentSourceUtils.releaseSession(c, dsToUse);
        }
    });

    verify(userTransaction, times(2)).begin();
    verify(userTransaction, times(2)).commit();
    verify(session1).close();
    verify(session2).close();
}