Search in sources :

Example 1 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project jOOQ by jOOQ.

the class SpringTransactionProvider method begin.

@Override
public void begin(TransactionContext ctx) {
    log.info("Begin transaction");
    // This TransactionProvider behaves like jOOQ's DefaultTransactionProvider,
    // which supports nested transactions using Savepoints
    TransactionStatus tx = txMgr.getTransaction(new DefaultTransactionDefinition(PROPAGATION_NESTED));
    ctx.transaction(new SpringTransaction(tx));
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionStatus(org.springframework.transaction.TransactionStatus)

Example 2 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project jOOQ by jOOQ.

the class TransactionTest method testExplicitTransactions.

@Test
public void testExplicitTransactions() {
    boolean rollback = false;
    TransactionStatus tx = txMgr.getTransaction(new DefaultTransactionDefinition());
    try {
        // constraint violation exception
        for (int i = 0; i < 2; i++) dsl.insertInto(BOOK).set(BOOK.ID, 5).set(BOOK.AUTHOR_ID, 1).set(BOOK.TITLE, "Book 5").execute();
        Assert.fail();
    }// Upon the constraint violation, we explicitly roll back the transaction.
     catch (DataAccessException e) {
        txMgr.rollback(tx);
        rollback = true;
    }
    assertEquals(4, dsl.fetchCount(BOOK));
    assertTrue(rollback);
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionStatus(org.springframework.transaction.TransactionStatus) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 3 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project spring-framework by spring-projects.

the class SchedulerAccessor method registerJobsAndTriggers.

/**
	 * Register jobs and triggers (within a transaction, if possible).
	 */
protected void registerJobsAndTriggers() throws SchedulerException {
    TransactionStatus transactionStatus = null;
    if (this.transactionManager != null) {
        transactionStatus = this.transactionManager.getTransaction(new DefaultTransactionDefinition());
    }
    try {
        if (this.jobSchedulingDataLocations != null) {
            ClassLoadHelper clh = new ResourceLoaderClassLoadHelper(this.resourceLoader);
            clh.initialize();
            XMLSchedulingDataProcessor dataProcessor = new XMLSchedulingDataProcessor(clh);
            for (String location : this.jobSchedulingDataLocations) {
                dataProcessor.processFileAndScheduleJobs(location, getScheduler());
            }
        }
        // Register JobDetails.
        if (this.jobDetails != null) {
            for (JobDetail jobDetail : this.jobDetails) {
                addJobToScheduler(jobDetail);
            }
        } else {
            // Create empty list for easier checks when registering triggers.
            this.jobDetails = new LinkedList<>();
        }
        // Register Calendars.
        if (this.calendars != null) {
            for (String calendarName : this.calendars.keySet()) {
                Calendar calendar = this.calendars.get(calendarName);
                getScheduler().addCalendar(calendarName, calendar, true, true);
            }
        }
        // Register Triggers.
        if (this.triggers != null) {
            for (Trigger trigger : this.triggers) {
                addTriggerToScheduler(trigger);
            }
        }
    } catch (Throwable ex) {
        if (transactionStatus != null) {
            try {
                this.transactionManager.rollback(transactionStatus);
            } catch (TransactionException tex) {
                logger.error("Job registration exception overridden by rollback exception", ex);
                throw tex;
            }
        }
        if (ex instanceof SchedulerException) {
            throw (SchedulerException) ex;
        }
        if (ex instanceof Exception) {
            throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage(), ex);
        }
        throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage());
    }
    if (transactionStatus != null) {
        this.transactionManager.commit(transactionStatus);
    }
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) SchedulerException(org.quartz.SchedulerException) XMLSchedulingDataProcessor(org.quartz.xml.XMLSchedulingDataProcessor) Calendar(org.quartz.Calendar) TransactionStatus(org.springframework.transaction.TransactionStatus) ClassLoadHelper(org.quartz.spi.ClassLoadHelper) SchedulerException(org.quartz.SchedulerException) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException) TransactionException(org.springframework.transaction.TransactionException) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) TransactionException(org.springframework.transaction.TransactionException)

Example 4 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project spring-framework by spring-projects.

the class DataSourceTransactionManagerTests method testParticipatingTransactionWithRollbackOnlyAndInnerSynch.

@Test
public void testParticipatingTransactionWithRollbackOnlyAndInnerSynch() throws Exception {
    tm.setTransactionSynchronization(DataSourceTransactionManager.SYNCHRONIZATION_NEVER);
    DataSourceTransactionManager tm2 = new DataSourceTransactionManager(ds);
    // tm has no synch enabled (used at outer level), tm2 has synch enabled (inner level)
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    final TestTransactionSynchronization synch = new TestTransactionSynchronization(ds, TransactionSynchronization.STATUS_UNKNOWN);
    try {
        assertTrue("Is new transaction", ts.isNewTransaction());
        final TransactionTemplate tt = new TransactionTemplate(tm2);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                assertTrue("Is existing transaction", !status.isNewTransaction());
                assertFalse("Is not rollback-only", status.isRollbackOnly());
                tt.execute(new TransactionCallbackWithoutResult() {

                    @Override
                    protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                        assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
                        assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
                        assertTrue("Is existing transaction", !status.isNewTransaction());
                        status.setRollbackOnly();
                    }
                });
                assertTrue("Is existing transaction", !status.isNewTransaction());
                assertTrue("Is rollback-only", status.isRollbackOnly());
                TransactionSynchronizationManager.registerSynchronization(synch);
            }
        });
        tm.commit(ts);
        fail("Should have thrown UnexpectedRollbackException");
    } catch (UnexpectedRollbackException ex) {
    // expected
    }
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertFalse(synch.beforeCommitCalled);
    assertTrue(synch.beforeCompletionCalled);
    assertFalse(synch.afterCommitCalled);
    assertTrue(synch.afterCompletionCalled);
    verify(con).rollback();
    verify(con).close();
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 5 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project spring-framework by spring-projects.

the class JmsTransactionManagerTests method testTransactionCommit.

@Test
public void testTransactionCommit() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    final Session session = mock(Session.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    JmsTemplate jt = new JmsTemplate(cf);
    jt.execute(new SessionCallback<Void>() {

        @Override
        public Void doInJms(Session sess) {
            assertTrue(sess == session);
            return null;
        }
    });
    tm.commit(ts);
    verify(session).commit();
    verify(session).close();
    verify(con).close();
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(javax.jms.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) JmsTemplate(org.springframework.jms.core.JmsTemplate) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)45 TransactionStatus (org.springframework.transaction.TransactionStatus)32 Test (org.junit.Test)28 TransactionCallback (org.springframework.transaction.support.TransactionCallback)10 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)8 Connection (javax.jms.Connection)7 ConnectionFactory (javax.jms.ConnectionFactory)7 Session (javax.jms.Session)7 JmsTemplate (org.springframework.jms.core.JmsTemplate)7 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)7 UserTransaction (javax.transaction.UserTransaction)5 SessionCallback (org.springframework.jms.core.SessionCallback)4 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)4 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)4 IllegalTransactionStateException (org.springframework.transaction.IllegalTransactionStateException)3 UOWException (com.ibm.wsspi.uow.UOWException)2 DataAccessException (org.springframework.dao.DataAccessException)2 ExpectedLookupTemplate (org.springframework.tests.mock.jndi.ExpectedLookupTemplate)2 NestedTransactionNotSupportedException (org.springframework.transaction.NestedTransactionNotSupportedException)2 TransactionDefinition (org.springframework.transaction.TransactionDefinition)2