Example usage for org.springframework.transaction.support TransactionTemplate TransactionTemplate

List of usage examples for org.springframework.transaction.support TransactionTemplate TransactionTemplate

Introduction

In this page you can find the example usage for org.springframework.transaction.support TransactionTemplate TransactionTemplate.

Prototype

public TransactionTemplate(PlatformTransactionManager transactionManager) 

Source Link

Document

Construct a new TransactionTemplate using the given transaction manager.

Usage

From source file:com.mothsoft.alexis.engine.numeric.StockQuoteDataSetImporter.java

@Override
public void importData() {
    if (this.transactionTemplate == null) {
        this.transactionTemplate = new TransactionTemplate(this.transactionManager);
    }/*  w w w  . j a  v a2s. com*/

    final String base = "http://download.finance.yahoo.com/d/quotes.csv";
    final String s;
    try {
        s = URLEncoder.encode(StringUtils.join(this.stockSymbols, ","), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        // if UTF-8 is not supported, we have big issues
        throw new IllegalStateException(e);
    }

    final String url = String.format("%s?s=%s&f=snl1", base, s);

    logger.info("Importing stock quote activity from web service URL: " + url);

    try {
        final HttpClientResponse response = NetworkingUtil.get(new URL(url), null, null);
        importStockQuotes(response);
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.brekka.pegasus.core.services.impl.DownloadServiceImpl.java

@Override
public InputStream download(final AllocationFile file, final ProgressCallback progressCallback,
        final boolean captureDownloadEvent, final boolean incrementCounter) {
    // Has its own transaction
    final FileDownloadEvent event = captureDownloadEvent ? eventService.beginFileDownloadEvent(file) : null;

    // Perform in its own readonly transaction. This ensures that the thread is only using one connection at a time.
    TransactionTemplate tt = new TransactionTemplate(transactionManager);
    tt.setReadOnly(true);/*w  ww.  j a  v a 2s  .  c  om*/
    return tt.execute(new TransactionCallback<EventInputStream>() {
        @Override
        public EventInputStream doInTransaction(final TransactionStatus status) {
            FileType fileType = file.getXml();
            CryptedFile cryptedFile = pavewayService.retrieveCryptedFileById(file.getCryptedFile().getId());
            CryptoProfile cryptoProfile = cryptoProfileService.retrieveProfile(cryptedFile.getProfile());
            SecretKey secretKey = symmetricCryptoService.toSecretKey(fileType.getKey(), cryptoProfile);
            cryptedFile.setSecretKey(secretKey);
            InputStream is = pavewayService.download(cryptedFile);
            return new EventInputStream(is, file, event, cryptedFile.getOriginalLength(), progressCallback,
                    incrementCounter);
        }
    });
}

From source file:org.jasig.schedassist.impl.DefaultAvailableScheduleReflectionServiceImpl.java

/**
 * @param platformTransactionManager the platformTransactionManager to set
 *///from  w w w  . j  av a 2  s .  co  m
@Autowired
public void setPlatformTransactionManager(PlatformTransactionManager platformTransactionManager) {
    this.transactionTemplate = new TransactionTemplate(platformTransactionManager);
    this.transactionTemplate.setIsolationLevel(Isolation.READ_COMMITTED.value());
}

From source file:com.mothsoft.alexis.engine.numeric.TopicActivityDataSetImporter.java

@Override
public void importData() {
    if (this.transactionTemplate == null) {
        this.transactionTemplate = new TransactionTemplate(this.transactionManager);
    }//from  w  w w .  j a  v  a2s.com

    final List<Long> userIds = listUserIds();

    final GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    int minute = calendar.get(Calendar.MINUTE);

    if (minute >= 45) {
        calendar.set(Calendar.MINUTE, 30);
    } else if (minute >= 30) {
        calendar.set(Calendar.MINUTE, 15);
    } else if (minute >= 15) {
        calendar.set(Calendar.MINUTE, 0);
    } else if (minute >= 0) {
        calendar.set(Calendar.MINUTE, 45);
        calendar.add(Calendar.HOUR_OF_DAY, -1);
    }

    final Date startDate = calendar.getTime();

    calendar.add(Calendar.MINUTE, 15);
    calendar.add(Calendar.MILLISECOND, -1);
    final Date endDate = calendar.getTime();

    for (final Long userId : userIds) {
        importTopicDataForUser(userId, startDate, endDate);
    }
}

From source file:org.springextensions.db4o.Db4oTransactionManagerTest.java

@Test
public void testTransactionRollback() throws Exception {
    final ExtObjectContainer container = mock(ExtObjectContainer.class);
    when(container.identity()).thenReturn(null);
    when(container.ext()).thenReturn(container);

    PlatformTransactionManager tm = new Db4oTransactionManager(container);
    TransactionTemplate tt = new TransactionTemplate(tm);

    Assert.assertTrue(!TransactionSynchronizationManager.hasResource(container), "Has no container");
    Assert.assertTrue(!TransactionSynchronizationManager.isSynchronizationActive(),
            "JTA synchronizations not active");

    try {//from www  .  java  2 s. c o  m
        tt.execute(new TransactionCallbackWithoutResult() {
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                Assert.assertTrue(TransactionSynchronizationManager.hasResource(container),
                        "Has thread session");
                Db4oTemplate template = new Db4oTemplate(container);
                template.execute(new Db4oCallback() {
                    public Object doInDb4o(ObjectContainer cont) {
                        cont.ext().identity();
                        throw new RuntimeException();
                    }

                });
            }
        });
    } catch (RuntimeException e) {
        // it's okay
    }

    Assert.assertTrue(!TransactionSynchronizationManager.hasResource(container), "Has no container");
    Assert.assertTrue(!TransactionSynchronizationManager.isSynchronizationActive(),
            "JTA synchronizations not active");

    verify(container).rollback();
}

From source file:org.zlogic.vogon.web.security.UserService.java

/**
 * Returns true if the username is already in use
 *
 * @param username the username to check
 * @return true if the username is already in use
 *//*from  w w w  .  j  av  a  2 s  . co  m*/
private boolean isUsernameExists(final String username) {
    TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
    transactionTemplate.setReadOnly(true);
    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    return transactionTemplate.execute(new TransactionCallback<Boolean>() {

        @Override
        public Boolean doInTransaction(TransactionStatus ts) {
            return userRepository.findByUsernameIgnoreCase(username) != null;
        }
    });
}

From source file:org.fcrepo.camel.integration.FcrepoTransactionIT.java

@Before
public void setUp() throws Exception {
    super.setUp();

    txTemplate = new TransactionTemplate(txMgr);
    txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    txTemplate.afterPropertiesSet();/*from  w ww  .j av  a 2  s. com*/
}

From source file:ca.uhn.fhir.jpa.search.StaleSearchDeletingSvcImpl.java

protected void deleteSearch(final Search next) {
    TransactionTemplate tt = new TransactionTemplate(myTransactionManager);
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override/*  w w w.  jav a2  s . c  o m*/
        protected void doInTransactionWithoutResult(TransactionStatus theArg0) {
            Search searchToDelete = mySearchDao.findOne(next.getId());
            ourLog.info("Expiring stale search {} / {}", searchToDelete.getId(), searchToDelete.getUuid());
            mySearchIncludeDao.deleteForSearch(searchToDelete.getId());
            mySearchResultDao.deleteForSearch(searchToDelete.getId());
            mySearchDao.delete(searchToDelete);
        }
    });
}

From source file:net.solarnetwork.node.dao.jdbc.test.JdbcSettingsDaoTests.java

@Before
public void setup() {
    DatabaseSetup setup = new DatabaseSetup();
    setup.setDataSource(dataSource);//w w w . ja va  2s. c om
    setup.init();

    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(Include.NON_NULL);

    dao = new JdbcSettingDao();
    dao.setDataSource(dataSource);
    dao.setTransactionTemplate(new TransactionTemplate(txManager));

    eventAdminMock = EasyMock.createMock(EventAdmin.class);
    dao.setEventAdmin(new StaticOptionalService<EventAdmin>(eventAdminMock));
    settingDao = dao;
}

From source file:com.hs.mail.imap.mailbox.DefaultMailboxManager.java

public void setTransactionManager(PlatformTransactionManager transactionManager) {
    Assert.notNull(transactionManager, "The 'transactionManager' argument must not be null.");
    this.transactionTemplate = new TransactionTemplate(transactionManager);
}