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

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

Introduction

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

Prototype

TransactionCallbackWithoutResult

Source Link

Usage

From source file:net.solarnetwork.node.dao.jdbc.JdbcSettingDao.java

@Override
public void storeSetting(final Setting setting) {
    TransactionTemplate tt = getTransactionTemplate();
    if (tt != null) {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override/*w w  w.jav  a  2 s .co m*/
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                storeSettingInternal(setting.getKey(), setting.getType(), setting.getValue(),
                        SettingFlag.maskForSet(setting.getFlags()));
            }
        });
    } else {
        storeSettingInternal(setting.getKey(), setting.getType(), setting.getValue(),
                SettingFlag.maskForSet(setting.getFlags()));
    }
}

From source file:org.springextensions.neodatis.NeoDatisTransactionManagerTest.java

@Test
public void testWrongIsolationLevel() {
    final ODB odb = Mockito.mock(ODB.class);
    Mockito.when(odb.store(Mockito.isNull())).thenThrow(new RuntimeException());
    PlatformTransactionManager tm = new NeoDatisTransactionManager(odb);
    TransactionTemplate tmpl = new TransactionTemplate(tm);

    Assert.assertFalse("Should not have a resource", TransactionSynchronizationManager.hasResource(odb));
    Assert.assertFalse("There should no active synchronizations",
            TransactionSynchronizationManager.isSynchronizationActive());

    tmpl.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
    try {/*from  www. j  av  a2 s  . c o  m*/
        tmpl.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                Assert.assertTrue(TransactionSynchronizationManager.hasResource(odb));
                NeoDatisTemplate neoDatisTemplate = new NeoDatisTemplate(odb);
                neoDatisTemplate.store(null);
                transactionStatus.setRollbackOnly();
            }
        });
        Assert.fail("Should throw an exception.");
    } catch (InvalidIsolationLevelException e) {
        // is ok
    }

    Assert.assertFalse("Should not have a resource", TransactionSynchronizationManager.hasResource(odb));
    Assert.assertFalse("There should no active synchronizations",
            TransactionSynchronizationManager.isSynchronizationActive());

}

From source file:org.web4thejob.web.panel.DefaultEntityHierarchyPanel.java

private void renderChildren(final Treeitem parentNode, final EntityHierarchyParent parentItem) {

    ContextUtil.getTransactionWrapper().execute(new TransactionCallbackWithoutResult() {
        @Override//from   w  ww  .  j  a v  a 2 s  . c o m
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            Set<EntityHierarchy<EntityHierarchyParent, EntityHierarchyItem>> children = ((EntityHierarchyParent) ContextUtil
                    .getDRS().get(parentItem.getEntityType(), parentItem.getIdentifierValue())).getChildren();
            if (!children.isEmpty()) {
                if (parentNode.getTreechildren() == null) {
                    new Treechildren().setParent(parentNode);
                }
                parentNode.getTreechildren().getChildren().clear();
                for (EntityHierarchy hierarchyItem : children) {
                    EntityHierarchyItem childItem = hierarchyItem.getChild();

                    if (!showChildren && !(childItem instanceof EntityHierarchyParent))
                        continue;

                    Treeitem newNode = getNewTreeitem(childItem);
                    newNode.setParent(parentNode.getTreechildren());
                    newNode.setAttribute(ATTRIB_HIERARCHY, hierarchyItem);

                    if (childItem instanceof EntityHierarchyParent) {
                        if (!((EntityHierarchyParent) childItem).getChildren().isEmpty()) {
                            new Treechildren().setParent(newNode);
                            newNode.setOpen(false);
                            newNode.addEventListener(Events.ON_OPEN, DefaultEntityHierarchyPanel.this);
                            newNode.addEventListener(ON_OPEN_ECHO, DefaultEntityHierarchyPanel.this);
                        }
                    }
                }
                if (parentNode.getTreechildren().getItemCount() == 0) {
                    parentNode.getTreechildren().detach();
                }
            }
        }
    });

}

From source file:org.fornax.cartridges.sculptor.framework.web.jpa.JpaFlowExecutionListener.java

public void sessionEnded(RequestContext context, FlowSession session, String outcome, AttributeMap output) {
    if (isPersistenceContext(session.getDefinition())) {
        final EntityManager em = (EntityManager) context.getConversationScope()
                .remove(session.getDefinition().getId() + "/" + PERSISTENCE_CONTEXT_ATTRIBUTE);
        Boolean commitStatus = session.getState().getAttributes().getBoolean("commit");
        if (Boolean.TRUE.equals(commitStatus)) {
            transactionTemplate.execute(new TransactionCallbackWithoutResult() {
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    em.joinTransaction();
                }/*from  w w  w. ja  va 2s  .com*/
            });
        }
        unbind(em);
        em.close();
    }
    if (!session.isRoot()) {
        FlowSession parent = session.getParent();
        if (isPersistenceContext(parent.getDefinition())) {
            bind(getEntityManager(context, parent.getDefinition().getId()));
        }
    }
}

From source file:org.openvpms.archetype.rules.finance.till.TillRules.java

/**
 * Start clearing the till.// ww w.jav a 2s  .co  m
 * <p/>
 * This sets the status of the till balance to IN_PROGRESS, so that any new payments or refunds don't affect it.
 * <p/>
 * If the cash float is different to the existing cash float for the till, an adjustment will be created.
 *
 * @param balance   the till balance
 * @param cashFloat the amount remaining in the till
 * @throws TillRuleException         if the balance is not UNCLEARED
 * @throws ArchetypeServiceException for any archetype service error
 */
public void startClearTill(final FinancialAct balance, final BigDecimal cashFloat) {
    if (!balance.getStatus().equals(TillBalanceStatus.UNCLEARED)) {
        throw new TillRuleException(InvalidStatusForStartClear, balance.getStatus());
    }
    TransactionTemplate template = new TransactionTemplate(transactionManager);
    template.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            if (isClearInProgress(getTillRef(balance, service))) {
                throw new TillRuleException(ClearInProgress);
            }
            balance.setStatus(TillBalanceStatus.IN_PROGRESS);
            Till till = getTillBean(balance);
            addAdjustment(balance, cashFloat, till);
            service.save(balance);
            till.setTillFloat(cashFloat);
            till.setLastCleared(new Date());
            till.save();
        }
    });
}

From source file:ch.tatool.app.service.impl.UserAccountServiceImpl.java

/**
  * Opens an account data object.//  w w w . ja va2s  .c  o  m
  * 
  * The account object is backed by the database. The password is conveniently used
  * as database password, which would fail in case of incorrect password.
  */
public UserAccount loadAccount(Info info, String password) {
    UserAccountImpl.InfoImpl infoImpl = (UserAccountImpl.InfoImpl) info;

    // load the database support objects (spring does all the wiring work for us
    Properties properties = new Properties();
    String dataPath = new File(infoImpl.getFolder(), "data").getAbsolutePath();
    properties.setProperty("account.data.folder", dataPath);
    if (password != null && password.length() > 0) {
        properties.setProperty("account.password", password);
    } else {
        properties.setProperty("account.password", "");
    }

    BeanFactory beanFactory = ContextUtils.createBeanFactory(configurationFilePath, properties);

    // create an account object, and bind it to the database
    final UserAccountImpl userAccount = new UserAccountImpl();
    userAccount.setFolder(infoImpl.getFolder());
    userAccount.setBeanFactory(beanFactory);
    userAccount.setPassword(password);
    userAccount.setName(infoImpl.getName());
    userAccount.setId(infoImpl.getId());

    // initialize transaction management
    PlatformTransactionManager transactionManager = (PlatformTransactionManager) beanFactory
            .getBean("userAccountTxManager");
    TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
    userAccount.setTransactionTemplate(transactionTemplate);

    // load the account in a transaction
    try {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            public void doInTransactionWithoutResult(TransactionStatus status) {
                UserAccountDAO userAccountDAO = userAccount.getUserAccountDAO();
                userAccountDAO.loadAccount(userAccount);
            }
        });

    } catch (org.hibernate.ObjectNotFoundException onfe) {
        logger.warn("Databasee entry for account does not exist anymore. Got the database deleted?");
        throw new RuntimeException("Unable to load user account due to missing data");
    }

    changeLocale(userAccount);

    return userAccount;
}

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

public void renameMailbox(final Mailbox source, final String targetName) {
    getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {
        public void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                MailboxDao dao = DaoFactory.getMailboxDao();
                dao.renameMailbox(source, targetName);
            } catch (DataAccessException ex) {
                status.setRollbackOnly();
                throw ex;
            }/*from   w w  w.j  a  va2 s.  c  o m*/
        }
    });
}

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

@Test
public void testInvalidIsolation() throws Exception {
    final ExtObjectContainer container = mock(ExtObjectContainer.class);

    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");

    tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
    try {//from   ww  w  .  ja  v  a  2 s.c  om
        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) {
                        return null;
                    }
                });
            }
        });
        Assert.fail("Should have thrown InvalidIsolationLevelException");
    } catch (InvalidIsolationLevelException e) {
        // it's okay
    }

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

    // TODO verify(container)....; exception thrown?
}

From source file:edu.northwestern.bioinformatics.studycalendar.osgi.felixcm.internal.PscFelixPersistenceManager.java

@SuppressWarnings({ "unchecked" })
public void store(final String pid, final Dictionary values) {
    log.debug("Storing configuration dictionary for {}", pid);

    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
        @Override/*w w w. j a va2 s.  c o  m*/
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            doStore(pid, values);
        }
    });
}

From source file:ar.edu.utn.sigmaproject.webflow.Hibernate4FlowExecutionListener.java

public void sessionEnding(RequestContext context, FlowSession session, String outcome,
        MutableAttributeMap output) {/*www . j a v a  2 s  . c om*/
    if (isParentPersistenceContext(session) && !needsNewSession(session)) {
        return;
    }
    if (isPersistenceContext(session.getDefinition())) {
        final Session hibernateSession = getHibernateSession(session);
        Boolean commitStatus = session.getState().getAttributes().getBoolean("commit");
        if (Boolean.TRUE.equals(commitStatus)) {
            transactionTemplate.execute(new TransactionCallbackWithoutResult() {
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    sessionFactory.getCurrentSession();
                    // nothing to do; a flush will happen on commit automatically as this is a read-write
                    // transaction
                }
            });
        }
        unbind(hibernateSession);
        hibernateSession.close();
    }
}