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

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

Introduction

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

Prototype

@Override
    public void afterPropertiesSet() 

Source Link

Usage

From source file:org.apache.camel.component.hibernate.DefaultTransactionStrategy.java

public static DefaultTransactionStrategy newInstance(SessionFactory sessionFactory,
        HibernateTemplate template) {/* w w  w .  j  ava  2s.  c  om*/
    HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
    transactionManager.afterPropertiesSet();

    TransactionTemplate tranasctionTemplate = new TransactionTemplate(transactionManager);
    tranasctionTemplate.afterPropertiesSet();

    return new DefaultTransactionStrategy(template, tranasctionTemplate);
}

From source file:com.hypersocket.netty.Main.java

protected void createApplicationContext() {

    if (log.isInfoEnabled()) {
        log.info("Creating spring application context");
    }/*  w ww  .  ja  va  2  s  . c o  m*/

    applicationContext = new ClassPathXmlApplicationContext("classpath*:/applicationContext.xml");

    if (log.isInfoEnabled()) {
        log.info("Obtaining platform transaction manager");
    }

    PlatformTransactionManager transactionManager = (PlatformTransactionManager) applicationContext
            .getBean("transactionManager");

    if (log.isInfoEnabled()) {
        log.info("Creating transaction template");
    }

    TransactionTemplate txnTemplate = new TransactionTemplate(transactionManager);

    if (log.isInfoEnabled()) {
        log.info("Calling TransactionTemplate.afterPropertiesSet");
    }

    txnTemplate.afterPropertiesSet();

    if (log.isInfoEnabled()) {
        log.info("Creating transaction for upgrade");
    }

    txnTemplate.execute(new TransactionCallback<Object>() {
        public Object doInTransaction(TransactionStatus status) {
            UpgradeService upgradeService = (UpgradeService) applicationContext.getBean("upgradeService");
            try {
                if (log.isInfoEnabled()) {
                    log.info("Starting upgrade");
                }
                upgradeService.upgrade();

                if (log.isInfoEnabled()) {
                    log.info("Completed upgrade");
                }
            } catch (Throwable e) {
                log.error("Failed to upgrade", e);
                throw new IllegalStateException("Errors upgrading database");
            }
            return null;
        }
    });

}

From source file:org.fcrepo.camel.FcrepoTransactionManagerTest.java

@Test(expected = CannotCreateTransactionException.class)
public void testTransactionBeginError() throws FcrepoOperationFailedException {
    final String baseUrl = "http://localhost:8080/rest";
    final String tx = "tx:1234567890";
    final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION);
    final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
    txMgr.setBaseUrl(baseUrl);/*from  w  w w  .jav a 2 s .  c o m*/
    TestUtils.setField(txMgr, "fcrepoClient", mockClient);

    final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr);
    final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(
            TransactionDefinition.PROPAGATION_REQUIRED);

    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    transactionTemplate.afterPropertiesSet();

    when(mockPostBuilder.perform()).thenThrow(new FcrepoOperationFailedException(beginUri, 400, "Bad Request"));

    txMgr.getTransaction(txDef);
}

From source file:org.fcrepo.camel.FcrepoTransactionManagerTest.java

@Test(expected = CannotCreateTransactionException.class)
public void testTransactionBeginNoLocationError() throws FcrepoOperationFailedException {
    final String baseUrl = "http://localhost:8080/rest";
    final String tx = "tx:1234567890";
    final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION);
    final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
    txMgr.setBaseUrl(baseUrl);//from w w  w  .  ja  va 2s  .  c  o  m
    TestUtils.setField(txMgr, "fcrepoClient", mockClient);

    final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr);
    final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(
            TransactionDefinition.PROPAGATION_REQUIRED);

    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    transactionTemplate.afterPropertiesSet();

    when(mockPostBuilder.perform()).thenReturn(new FcrepoResponse(beginUri, 201, emptyMap(), null));

    txMgr.getTransaction(txDef);
}

From source file:org.fcrepo.camel.FcrepoTransactionManagerTest.java

@Test(expected = CannotCreateTransactionException.class)
public void testTransactionNullResponseError() throws FcrepoOperationFailedException {
    final String baseUrl = "http://localhost:8080/rest";
    final String tx = "tx:1234567890";
    final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION);
    final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
    txMgr.setBaseUrl(baseUrl);/*from w w w. j  a v  a  2  s.  c o m*/
    TestUtils.setField(txMgr, "fcrepoClient", mockClient);

    final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr);
    final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(
            TransactionDefinition.PROPAGATION_REQUIRED);

    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    transactionTemplate.afterPropertiesSet();

    when(mockPostBuilder.perform()).thenReturn(null);

    txMgr.getTransaction(txDef);
}

From source file:org.fcrepo.camel.FcrepoTransactionManagerTest.java

@Test(expected = TransactionSystemException.class)
public void testTransactionCommitError() throws FcrepoOperationFailedException {
    final String baseUrl = "http://localhost:8080/rest";
    final String tx = "tx:1234567890";
    final URI commitUri = URI.create(baseUrl + "/" + tx + FcrepoConstants.COMMIT);
    final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION);
    final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
    txMgr.setBaseUrl(baseUrl);// w  w w. j  a va 2  s.c o m
    TestUtils.setField(txMgr, "fcrepoClient", mockClient);

    final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr);
    final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(
            TransactionDefinition.PROPAGATION_REQUIRED);

    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    transactionTemplate.afterPropertiesSet();

    when(mockClient.post(eq(beginUri))).thenReturn(mockPostBuilder);
    when(mockClient.post(eq(commitUri))).thenReturn(mockPostBuilder2);
    when(mockPostBuilder.perform()).thenReturn(new FcrepoResponse(beginUri, 201,
            singletonMap("Location", singletonList(baseUrl + "/" + tx)), null));
    when(mockPostBuilder2.perform())
            .thenThrow(new FcrepoOperationFailedException(commitUri, 400, "Bad Request"));

    DefaultTransactionStatus status = (DefaultTransactionStatus) txMgr.getTransaction(txDef);

    final FcrepoTransactionObject txObj = (FcrepoTransactionObject) status.getTransaction();
    assertEquals(tx, txObj.getSessionId());
    assertFalse(status.isCompleted());

    status = (DefaultTransactionStatus) txMgr.getTransaction(txDef);
    txMgr.commit(status);
}

From source file:org.fcrepo.camel.FcrepoTransactionManagerTest.java

@Test(expected = TransactionSystemException.class)
public void testTransactionRollbackError() throws FcrepoOperationFailedException {
    final String baseUrl = "http://localhost:8080/rest";
    final String tx = "tx:1234567890";
    final URI rollbackUri = URI.create(baseUrl + "/" + tx + FcrepoConstants.ROLLBACK);
    final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION);
    final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
    txMgr.setBaseUrl(baseUrl);/*from   ww w. j ava 2  s  .co  m*/
    TestUtils.setField(txMgr, "fcrepoClient", mockClient);

    final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr);
    final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(
            TransactionDefinition.PROPAGATION_REQUIRED);

    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    transactionTemplate.afterPropertiesSet();

    when(mockClient.post(eq(beginUri))).thenReturn(mockPostBuilder);
    when(mockClient.post(eq(rollbackUri))).thenReturn(mockPostBuilder2);
    when(mockPostBuilder.perform()).thenReturn(new FcrepoResponse(beginUri, 201,
            singletonMap("Location", singletonList(baseUrl + "/" + tx)), null));
    when(mockPostBuilder2.perform())
            .thenThrow(new FcrepoOperationFailedException(rollbackUri, 400, "Bad Request"));

    DefaultTransactionStatus status = (DefaultTransactionStatus) txMgr.getTransaction(txDef);

    final FcrepoTransactionObject txObj = (FcrepoTransactionObject) status.getTransaction();
    assertEquals(tx, txObj.getSessionId());
    assertFalse(status.isCompleted());

    status = (DefaultTransactionStatus) txMgr.getTransaction(txDef);
    txMgr.rollback(status);
}

From source file:org.fcrepo.camel.FcrepoTransactionManagerTest.java

@Test
public void testTransactionCommit() throws FcrepoOperationFailedException {
    final String baseUrl = "http://localhost:8080/rest";
    final String tx = "tx:1234567890";
    final URI commitUri = URI.create(baseUrl + "/" + tx + FcrepoConstants.COMMIT);
    final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION);
    final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
    txMgr.setBaseUrl(baseUrl);/*  w ww . ja  v  a  2 s. c  o  m*/
    TestUtils.setField(txMgr, "fcrepoClient", mockClient);

    final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr);
    final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(
            TransactionDefinition.PROPAGATION_REQUIRED);

    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    transactionTemplate.afterPropertiesSet();

    when(mockClient.post(eq(beginUri))).thenReturn(mockPostBuilder);
    when(mockClient.post(eq(commitUri))).thenReturn(mockPostBuilder2);
    when(mockPostBuilder.perform()).thenReturn(new FcrepoResponse(beginUri, 201,
            singletonMap("Location", singletonList(baseUrl + "/" + tx)), null));
    when(mockPostBuilder2.perform()).thenReturn(new FcrepoResponse(commitUri, 201, emptyMap(), null));

    DefaultTransactionStatus status = (DefaultTransactionStatus) txMgr.getTransaction(txDef);
    FcrepoTransactionObject txObj = (FcrepoTransactionObject) status.getTransaction();

    assertEquals(tx, txObj.getSessionId());
    assertFalse(status.isCompleted());

    status = (DefaultTransactionStatus) txMgr.getTransaction(txDef);

    txMgr.commit(status);

    txObj = (FcrepoTransactionObject) status.getTransaction();

    assertNull(txObj.getSessionId());
    assertTrue(status.isCompleted());
}

From source file:org.fcrepo.camel.FcrepoTransactionManagerTest.java

@Test
public void testTransactionRollback() throws FcrepoOperationFailedException {
    final String baseUrl = "http://localhost:8080/rest";
    final String tx = "tx:1234567890";
    final URI commitUri = URI.create(baseUrl + "/" + tx + FcrepoConstants.COMMIT);
    final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION);
    final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
    txMgr.setBaseUrl(baseUrl);/*from   www. ja  va  2  s  .  c o  m*/
    TestUtils.setField(txMgr, "fcrepoClient", mockClient);

    final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr);
    final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(
            TransactionDefinition.PROPAGATION_REQUIRED);

    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    transactionTemplate.afterPropertiesSet();

    when(mockClient.post(eq(beginUri))).thenReturn(mockPostBuilder);
    when(mockClient.post(eq(commitUri))).thenReturn(mockPostBuilder2);
    when(mockPostBuilder.perform()).thenReturn(new FcrepoResponse(beginUri, 201,
            singletonMap("Location", singletonList(baseUrl + "/" + tx)), null));
    when(mockPostBuilder2.perform()).thenReturn(new FcrepoResponse(commitUri, 201, emptyMap(), null));

    DefaultTransactionStatus status = (DefaultTransactionStatus) txMgr.getTransaction(txDef);
    FcrepoTransactionObject txObj = (FcrepoTransactionObject) status.getTransaction();

    assertEquals(tx, txObj.getSessionId());
    assertFalse(status.isCompleted());

    status = (DefaultTransactionStatus) txMgr.getTransaction(txDef);

    txMgr.rollback(status);

    txObj = (FcrepoTransactionObject) status.getTransaction();

    assertNull(txObj.getSessionId());
    assertTrue(status.isCompleted());
}